joprocorp
joprocorp

Reputation: 346

Firebase Cloud Messaging (FCM) sends push notification to System tray when app is foregrounded

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.

enter image description here

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):

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:

My question (finally)

What I tried

Upvotes: 1

Views: 218

Answers (1)

Jinesh Francis
Jinesh Francis

Reputation: 3665

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
}

Upvotes: -1

Related Questions