Context
As per the FCM documentation on Android Message Handling, push notifications will only be displayed if the App is backgrounded. It is not possible for a notification to appear in the System Tray while the App is foregrounded.
However! I have an app built with Capacitor. The app is currently on beta using the Internal Testing tool suite of the playstore. On it, when sending push notifications, I receive them through the onMessageReceived
callback AND the System Tray even when the app is foregrounded.
According to the documentation, that shouldn’t be possible. So, I thought maybe (without being able to confirm):
- Did I read the FCM documentation wrong?
- The Internal Testing is preventing FCM from correctly detecting if an app is foregrounded (although I don’t think so, since
onMessageReceived
get triggered) - Something is wrong with Capacitor (I am using an old and unsupported version, so I wouldn’t be surprised of unintended behaviour)
- Something wrong about the FCM message format I’m sending (see below)?
An example of the FCM message package I’m sending using the firebase-admin
SDK (latest version). This is a notification message with the optional data
payload provided:
{
data: { // data },
notification: { title: "My title", body: 'My body' },
android: { notification: { priority: 'max', visibility: 'public' } },
apns: { payload: { aps: [Object] } },
tokens: // token
}
Some observations:
- On iOS (Capacitor being cross-platform), I get the same behaviour with a notification in the System Tray and the callback triggered
- On Android, notifications that appear when the app is foregrounded are not clickable
- When the app is backgrounded, I get notifications in the System Tray as expected. Those work as intended and are clickable (eg. they open the app and trigger the correct callbacks)
- I just updated from a very old
firebase-admin
SDK (that was using the now-closed legacy APIs) to the latest version of the SDK. Before, I didn’t have this kind of issues.
My question (finally)
- Is this issue caused by the Internal Testing? If so, that would indicate to me that the live version will work as expected
- If not, did you ever encounter this issue? If so, did you fix it and how?
- If not, do you have an idea what could be the issue?
What I tried
- I’ve tried removing the
android.notification.priority
and.visibility
props, but I get the same issue. - All the research I’ve done point to topics and posts discussing if it was possible to send a notification to the System Tray while the app is foregrounded. While I am in that situation, but want to get out of it (truly ironic).
If you need to get onMessageReceived
when the app is in foreground and background, You should send notification object as null and use data object for sending data as follows
{
data: { title: "My title", body: 'My body' },
notification: null,
android: { notification: { priority: 'max', visibility: 'public' } },
apns: { payload: { aps: [Object] } },
tokens: // token
}
3