FCM Notification in System Tray when App is in Foreground
Asked Answered
H

3

7

I'm able to see the notification in system tray when the app is in Background.

But, I would like the notification to be visible in system tray even if the app is in Foreground.

I tried the following on Android:

  • Sending notification from Firebase console: I can see the notification in system tray only when the app is in background.
  • Sending a post request to https://fcm.googleapis.com/fcm/send as described here with both data and notification paylods: Again, I can only see the notification in system tray when the app is in background.

This document mentions:

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

What am I missing?

I'm using Android and Cordova/Ionic.

Horrified answered 17/12, 2017 at 19:1 Comment(1)
was you able to resolve this? We have the same behavior which is as expected. We can send data payload with the same information then we have in notification payload and can sent to a LocalNotifcation. But the Other problem we have, LocalNotification has an ID, FCM Notification has an TAG, so we can not overwrite the FCM Notification with a LocalNotification or the other way around. Some idea?Ifni
R
2

If you want a notification to appear when the app is in the foreground, you will have to send a message with a data payload, then write code to handle that in your app, as described by the documentation you referenced.

When you send a message with a notification payload, FCM works exactly as you described. When then app is in the background, it will display a notification for you. When in the foreground, it will not.

Please note that data and notification payloads behave differently, as described in the documentation.

Rebellious answered 17/12, 2017 at 19:50 Comment(4)
I tried sending a notification using only data payload, but I still don't see it in the tray area. Apparently, this is not possible: github.com/fechanique/cordova-plugin-fcm/issues/…Horrified
Please read the documentation I referenced in my answer. Data payloads have different behavior than notification payloads.Rebellious
@DougStevenson The doc is clear for notification messages, but it's also wrong: "FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs." Unfortunately, what it says, and what it actually does, are two different things.Legra
@JamesMoore - the doc also says "Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by a callback function." Indeed, that is what it does. The assumption is that when the app is in use, the app designer may want to display the "notification" within the context of their app, e.g. not as a standard system-tray notification. See other answers re "local notification", if you really do want a notification.Bally
A
0

At foreground you can use Local Notification :

https://ionicframework.com/docs/native/local-notifications/

Arterialize answered 2/8, 2018 at 8:35 Comment(0)
F
0

It is Unity issue but I think the FCM core is the same.

Showing FCM push notification in tray when app is in foreground

The answer is

You have to redirect this to a local notification manually

Fustic answered 18/2, 2019 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.