1

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());
user246392
  • 2,661
  • 11
  • 54
  • 96
  • You would have to manually handle notifications yourself deciding to show it or not – tyczj Feb 13 '20 at 18:59
  • Correct, but I'm looking for the code to do it. I built my notification via `NotificationCompat.Builder` and tried out different things (such as lowering the importance or setting no sound) with no luck. – user246392 Feb 13 '20 at 19:03
  • all you have to do is store a flag indicating your app is running and in every `onStart` set it to true then in every `onPause` set it to false. Then check the value when you get a push notification – tyczj Feb 13 '20 at 19:05
  • Possibly this is you want: https://stackoverflow.com/questions/36555653/push-silent-notification-through-gcm-to-android-ios – Mkhytar Mkhoian Feb 13 '20 at 19:38
  • Messages with a `notification` child will be handled by the system when the app is not actively being used, and the system will display the notification. Messages with (only) a `data` child will only be delivered to your application code, which can then choose on whether to display the notification or not. – Frank van Puffelen Feb 14 '20 at 00:21
  • @FrankvanPuffelen The other question is not similar. I'm aware that the client has the responsibility to display (or not) a notification with a `data` payload in `onMessageReceived()`. I need to find out if there is a way to send the `data` notification to the system's tray without a giant popup. – user246392 Feb 14 '20 at 00:34
  • Please edit your question to show how you're currently sending the message that shows up as that giant popup. – Frank van Puffelen Feb 14 '20 at 03:44
  • @FrankvanPuffelen Done. – user246392 Feb 14 '20 at 04:09
  • 1
    Made this work on our end by using a `LifecycleObserver` and having an `isForeground` flag on app, where the Messaging service can access it. Works smoothly. – AL. Feb 18 '20 at 10:24

3 Answers3

4

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

Angus H
  • 404
  • 3
  • 8
  • You touched on an important point. I created my channel with max importance. I will try lowering that. Here's another question though.. What if I want some notifications to be visually intruding while others to be non-intruding? Should I create two different channels (not sure if this is possible)? – user246392 Feb 17 '20 at 19:26
  • That's right, you want another notification channel. Notification channels appear as 'Categories' in the [notification settings for your app](https://developer.android.com/training/notify-user/channels), 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 – Angus H Feb 17 '20 at 22:49
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/25384778) – SRack Feb 18 '20 at 10:24
  • 1
    @SRack I've edited my answer to include the further detail from talking in the comments, it does now provide an answer to the question – Angus H Feb 19 '20 at 14:32
  • Your suggestion to create a separate channel with a low priority worked like a charm. Thanks a lot! – user246392 Feb 21 '20 at 05:21
0

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

Naitik Soni
  • 700
  • 8
  • 16
  • The problem with passing both data and notification is that, if notification payload is delivered, then I have no way of assigning an ID to the notification. It seems to be created by Android. Thus, I cannot remove a delivered `StatusBarNotification` by its ID because I don't know it. Actually, the ID keeps returning 0 for all notification payloads. When I try to `Cancel()` it, the cancellation does not work. – user246392 Feb 17 '20 at 21:17
  • So, you want to cancel particular notification after opening app?? not all... Right?! – Naitik Soni Feb 18 '20 at 06:18
  • Correct. I want to iterate through the delivered notifications and cancel specific ones due to business requirements. – user246392 Feb 18 '20 at 18:18
0

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

Prakash Reddy
  • 944
  • 10
  • 20