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.