Firebase Onmessagereceived Not Called When App in Background

Firebase onMessageReceived not called when app in background

When app is in background or killed FCM onMessageReceived() not called

If the push notification has a notification part in the payload and your app is in the background, onMessageReceived will never be called. The notification is automatically created and displayed by firebase. There is no way to intercept this and modify the notification or do any other operation for that matter. Your app will only get control once the notification is clicked.

The only way to ensure that onMessageReceived is called in all situations(foreground, background and app killed) is to send a data-only payload from firebase.

Please check my answer to a similar question for more details.

Firebase onMessageReceived not called

  1. tl;dr

Your messages, that are send from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.

Notification messages contain a predefined set of user-visible keys. Data messages, by contrast, contain only your user-defined custom key-value pairs. Notification messages can contain an optional data payload.

("Notification messages can contain an optional data payload", this will only get submitted to your service, if your application is in foreground!)

  • Messages created from the console will always be notification-messages!

  • You can't send plain data-messages, that will consistently arrive at your service through the firebase-console. This is only possible via the REST-API (FCM protocols) or the Admin SDK.

One more important thing:

If you send Notification Messages with a data payload, that you wish to handle, you must handle not only it via the service if the app is in foreground, but also gathering the data from the Intent that your Launch-Activity will receive, which will be opened, if the user clicks the notification! The data payload will be inside of the intent!




  1. Types of messages:

Notification Messages and Data Messages.

  1. Notification messages contain a predefined set of user-visible keys. Notification messages have an optional data payload.
  • If the app is in foreground, they will call onMessageReceived (you need to handle the display of the notification on your own)

  • If the application is background, the notification will be displayed (user system tray) and if there is a data payload, it is delivered in the extras of the intent of your launcher Activity.


  1. Data Messages contain only your user-defined custom key-value pairs. (explained below). And if you want that the message consistently call the onMessageReceived, they should not contain a notification payload, but only a data payload.

From the docs:

Client app is responsible for processing data messages. Data messages
have only custom key-value pairs. (! once again - only custom key-value pairs)

You can read about the message types here: Fcm Message Types




  1. Data Payload

The data payload is a simple json entry, additionally in your payload
and can have simple key, value pairs.

Example:

  "data": {
"eventId" : "1",
"flavors" : "alpha",
"minFcmVersion" : "3",
"showFallbackOnLowVersion" : "false"
}

Inside of the console, you can add the data payload, if you enter something below the "extended options" field:

Sample Image

Messages created from the console will always be notification-messages!

At first, send a firebase cloud message via the console, directly to your
device (use the fcm token) and add a simple data-payload with example values.
Now check if your services onMessageReceived is called. (You app must be in foreground!)

Backend

With the REST API you can send Data-Messages, that only contain custom key-value pairs and will always arrive to your service!

Your backend should compose the message itself and add a data payload.
This is somewhat described here: HTTP protocol data payload

You can also test a backend wise sent message via one of the several
http request builder you find online. I like: hurl.it and

Post to:

https://fcm.googleapis.com/fcm/send


  1. Gradle

com.google.firebase:firebase-messaging:20.0.0 <- use this, or higher

That said, there was a bug in versions lower then '11.0.4'.


  1. Links and good read

Further good to read documentation here: Receive Messages Documentation
And a good explanation on message types as an SOF Answer: SOF - Answer - Message Types

onMessageReceived is not executing when the app is in background

There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether
the app is in the foreground or background. Data messages are the type
traditionally used with GCM. Notification messages are only received
here in onMessageReceived when the app is in the foreground. When the
app is in the background an automatically generated notification is
displayed. When the user taps on the notification they are returned to
the app. Messages containing both notification and data payloads are
treated as notification messages. The Firebase console always sends
notification messages.

Example code :- You need to specify icon in your notification payload like this

$notification = array
(
'icon' => 'icon here',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'action here'
);

Note:- you need to add these in manifest to use default notification icon like this

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher" />

// optional if required
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/notification_color" />

Firebase onMessageReceived not called when app is in the background

Remove the notification payload from your FCM messages in order to have the data payload delivered to the onMessageReceived method.

Read this and this carefully.

When your app is in the background, data payload delivered to the onMessageReceived method only if there is no notification payload. (Mark the words)

In case both payloads exist then system automatically handles the notification part (system tray) and your app gets the data payload in the extras of the intent of launcher Activity (after the user tap on the notification).

In order to be able to serve both platforms successfully, Android and iOS, you may have to send different FCM messages according to client's OS.



Related Topics



Leave a reply



Submit