React-native-notifications: Notification data not reaching app when in the background (Android) 3.0.0

Created on 17 Jan 2020  路  15Comments  路  Source: wix/react-native-notifications

Enviroment

"react": "16.8.6", "react-native": "0.60.5", "react-native-notifications": "^3.0.0",

Problem

I am not receiving any notification data when the app is in the background.

expected behaviour

  • user taps on notification in the notification tray the app launches and the notification data appears in the 'RemoteNotificationOpened' event

actual behaviour

  • user taps on notification in the notification tray the app launches and the notification data does not appear in the 'RemoteNotificationOpened' event

Note.

When the app is in the foreground the notification data comes as expected

Most helpful comment

@nates-dennis could you please expand on this? Do you mean pushNotification: true as part of your notification payload or something of this sort?

I can validate that adding this works. I had the same issue and added the pushNotification: true key inside the data object and it's all working as expected.

All 15 comments

I noticed same behaviour on android, in my case getInitialNotification() is not triggered either. And as events().registerNotificationReceivedBackground as well.

Looks like i found the problem while debugging the library code i found that i need pushNotification key in my notification object so that canHandleIntent() in NotificationIntentAdapter returns true. Once i added this it was fine.

can someone explain the PUSH_NOTIFICATION_EXTRA_NAME constant and why it is needed. i have not seen anywhere where this is defined and explained to be part of the notification

@nates-dennis could you please expand on this? Do you mean pushNotification: true as part of your notification payload or something of this sort?

@nates-dennis could you please expand on this? Do you mean pushNotification: true as part of your notification payload or something of this sort?

Yes But i am still waiting on confirmation on what it is about. From studying the code it will not handle the Intent Extras (push notification data) when notification is tapped if the notification does not contain a key "pushNotification" the value being anything.

@nates-dennis could you please expand on this? Do you mean pushNotification: true as part of your notification payload or something of this sort?

I can validate that adding this works. I had the same issue and added the pushNotification: true key inside the data object and it's all working as expected.

For me adding pushNotification: true also works as expected

closing as it is shown this the solution but it will be nice to why it was done this way

I have the same issue. @nates-dennis / @RickWoltheus / @eduardopelitti Is it possible to send an example of the whole payload including the pushNotification: true you're sending?

@rnnyrk just send it along with your payload

it will look like something like this:

{
    ...myPayload etc
    date: new Date()
    type: NotificationType.news
    pushNotification: true
}

@RickWoltheus Thanks. Using Amazon SNS to send out push notifications. Will try to add the pushNotification: true, but since the casing is different as well I'm curious if it will work. Also I see no reference to it on FCM (https://firebase.google.com/docs/cloud-messaging/concept-options), or is this a specific key for this package? My current payload is:

{
    'priority': 'normal',
    'notification': {
      'type': 'redirect',
      'title': message.title,
      'body': message.body,
      'view_data': message.data
  }
}

@rnnyrk i am not exactly sure why you need to add this to your payload, but it works. Refer to this comment https://github.com/wix/react-native-notifications/issues/445#issuecomment-576767257

I'd try different variants of sending the pushNotifications: true payload to my app. Unfortunately I can't get it working. Sending the following payload via Amazon SNS:

{
  "GCM": "{
    \"notification\": { 
        \"title\": \"title\", 
        \"body\": \"Sample message for Android endpoints\", 
        \"view_data\": {
            \"chat_id\": \"12345678\"
        },
        \"pushNotification\": true,
    } 
}"
}

Tried different variant, with the pushNotifications key outside the notification object, in the view_data (crashed the app) etc. but none are triggering the correct events from a background notification. Foreground notifications are working _without_ the pushNotifications key.

Any suggestions or anyone knows if something is wrong with my payload? Thanks in advance.

I am not getting push notifications when my app is killed. But I do get the correct notification when my app is in the foreground and in the background. Anyone know why? :/

I have the same issue. @nates-dennis / @RickWoltheus / @eduardopelitti Is it possible to send an example of the whole payload including the pushNotification: true you're sending?

This is the object I'm using to send a message from the fcm-push node library.

  const message = {
    to: config.android.deviceToken,
    data: {
      title: 'xxx,
      message: 'xxxxxxx xxx',
      // ....some more key-value pairs specific to my setup,
      pushNotification: true,
    },
  }

In the past I've had issues with the name of key for the data object (i.e.: internally the notifications library was expecting a specific key to contain the data in order to _compose_ the notification on background).

Notice that mine is called data, it could also be payload.

I'm not sure if this is your issue though, and I'm also not sure if this happened on iOS or Android. I'd suggest debugging from the native side in Android Studio and trying to follow the breadcrumb of the notification on background.

Let me know how it went!


Edit, looking at the native code:

```public static boolean canHandleIntent(Intent intent) {
if (intent != null) {
Bundle notificationData = intent.getExtras();
if (notificationData != null && intent.hasExtra(PUSH_NOTIFICATION_EXTRA_NAME)) {
return true;
}
}

    return false;
}
This code is run on the `onNewIntent` listener:

@Override
public void onNewIntent(Intent intent) {
    if (NotificationIntentAdapter.canHandleIntent(intent)) {
        Bundle notificationData = intent.getExtras();
        final IPushNotification notification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationData);
        if (notification != null) {
            notification.onOpened();
        }
    }
}

```

So having the pushNotification key seems to be necessary when launching the app, if I'm understanding this code properly.

So perhaps your issue is that when the library is building the notificationData via the intent.getExtras(), it doesn't find the proper notification object and never launches.

This is all a crazy conjecture though, but I hope it helps point you in the right direction!

I'd try different variants of sending the pushNotifications: true payload to my app. Unfortunately I can't get it working. Sending the following payload via Amazon SNS:

{
  "GCM": "{
    \"notification\": { 
        \"title\": \"title\", 
        \"body\": \"Sample message for Android endpoints\", 
        \"view_data\": {
            \"chat_id\": \"12345678\"
        },
        \"pushNotification\": true,
    } 
}"
}

Tried different variant, with the pushNotifications key outside the notification object, in the view_data (crashed the app) etc. but none are triggering the correct events from a background notification. Foreground notifications are working _without_ the pushNotifications key.

Any suggestions or anyone knows if something is wrong with my payload? Thanks in advance.

Did you find any solution for this?

Was this page helpful?
0 / 5 - 0 ratings