rubenlop88
rubenlop88

Reputation: 4221

Firebase Cloud Messaging not sending data payload

From the Firebase Cloud Messaging documentation at Defining message payload:

You can specify one or both message types by creating an object with the data and / or notification keys.

The documentation gives the example of a Combined Message:

var payload = {
  notification: {
    title: "$GOOG up 1.43% on the day",
    body: "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."
  },
  data: {
    stock: "GOOG",
    open: 829.62,
    close: "635.67"
  }
};

Also from the documentation at Handle notification messages in a backgrounded app:

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, 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.

I'm sending a notification with this payload:

const payload = {
    notification: {
        title: '...',
        body: '...',
    },
    data: {
        test: "test"
    },
};
admin.messaging().sendToDevice(tokens, payload).then(...)

But extras is always null:

Intent intent = getIntent();
if (intent != null) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        // do something
    }
}

What am I doing wrong?

Upvotes: 3

Views: 1861

Answers (1)

Anoop M Maddasseri
Anoop M Maddasseri

Reputation: 10569

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.

  • When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

  • When in the foreground, your app receives a message object with both payloads available.

So, basically if the app is in foreground the intent wouldn't have any notification extras. if that's not the case your doing something wrong in configuration.

Upvotes: 1

Related Questions