Parth Bhoiwala
Parth Bhoiwala

Reputation: 1322

Don't invoke push notification when app is being used (running in foreground)

In my android app, I receive push notifications via Firebase Cloud Messaging. The notifications work completely fine, the only problem is that I don't want to receive notifications when the app is running. When the user is using the ap, I am already displaying the notification internally so the push notification is redundant and annoying. Below is the code on how I invoke push notification:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

I tried the solution on this link, and it won't work in my case because I do want the notification when the app is in the background or killed. Also setting a boolean to true when the app starts and false when the app is closed does not seem like an ideal solution. There has to be a better way but I can't find it. Any help would be appreciated.

Upvotes: 0

Views: 130

Answers (1)

Debanjan
Debanjan

Reputation: 2836

What I do is use ActivityLifeCycleCallbacks in your Application class

class MyApplication:Application(){
companion object {
        private var activitiesOpen = 0

        var displayActivity:String?= null
        private set

        fun isAppOpen() = activitiesOpen > 0
    }
    private val activitiesListener = object : ActivityLifecycleCallbacks{
        override fun onActivityPaused(activity: Activity?) {
            --activitiesOpen
            displayActivity = null
        }

        override fun onActivityResumed(activity: Activity?) {
            activitiesOpen++
            displayActivity = if(activity != null)
                activity::class.simpleName else null
        }

        override fun onActivityStarted(activity: Activity?) {
        }

        override fun onActivityDestroyed(activity: Activity?) {
        }

        override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
        }

        override fun onActivityStopped(activity: Activity?) {
        }

        override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        }
    }

}

Now in your onMessageReceived you can check it out like this.

    if (MyApplication.isAppOpen() &&
                DashboardActivity::class.simpleName?
.equals(MyApplication.displayActivity) ?: false) {
// you can eliminate the last condition if you wish

            playAlertTone(context) // or whatever
        } else {
           // your notification building code
        }

The code is in kotlin, I hope you'll have no problem with it.

Upvotes: 2

Related Questions