user246392
user246392

Reputation: 3019

Android: Prevent data notifications from popping up on the screen when app is in the background

I'm sending data payloads through FCM and handling them in the onMessageReceived() method of FirebaseMessagingService. The problem is, when the app is in the background, the push notifications seem to be popping up each time the device handles them. I'd like them to be silent similar to how Android delivers notification payloads.

enter image description here

How do I make sure the notifications do not create this popup effect? I'm on Android 9.

Here's how the notification is generated on the client:

var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, myChannelId)
                              .SetContentTitle(title)
                              .SetContentText(body)
                              .SetSmallIcon(Resource.Mipmap.ic_launcher)
                              .SetAutoCancel(true)
                              .SetContentIntent(pendingIntent);

and this is how it's notified:

if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
   notificationBuilder.SetChannelId(myChannelId);

var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(myId, notificationBuilder.Build());

Upvotes: 1

Views: 2655

Answers (3)

Prakash Reddy
Prakash Reddy

Reputation: 1002

Use android lifecyclehandler and check whether app is in foreground or background and don't show the notification if app is in foreground lifecyclerhandler

Upvotes: 0

Angus H
Angus H

Reputation: 408

Notifications will appear with a popup effect ('Heads Up') view like shown in the screenshot if you set the notification channel priority to IMPORTANCE_HIGH or above, so to stop that happening, you should ensure the channel priority is IMPORTANCE_DEFAULT or lower.

If you have other notifications that you do want to use the heads up view, then you should create a separate NotificationChannel for this notification. Note that notification channels appear as 'Categories' in the notification settings for your app, allowing the user to choose which kinds of notifications they want to see from you, and two notifications wanting a different priority is a good indicator that they should be in different categories

Upvotes: 4

Naitik Soni
Naitik Soni

Reputation: 770

When app is killed / in background...

  • When only 'data' payload is passed in request, notification will be generated with popup, and handled by user in 'onMessageReceived'

  • When only 'notification' payload is passed in request, notification will be generated without popup, and handled by android OS

  • When both 'data' and 'notification' payload are passed in request, notification will be generated without popup, and handled by android OS and data will be available for handling in extras of the intent.


So, in our case (don't want to show notification as popup) pass both 'data' and 'notification' payloads in request params. (Here title and body will be shown, what is passed in notification payload)

Refer official link: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

Upvotes: 0

Related Questions