wix / react-native-notifications

React Native Notifications
MIT License
3.22k stars 763 forks source link

Notification data not reaching app when in the background (Android) 3.0.0 #445

Closed nates-dennis closed 4 years ago

nates-dennis commented 4 years ago

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

actual behaviour

Note.

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

xzilja commented 4 years ago

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

nates-dennis commented 4 years ago

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

xzilja commented 4 years ago

@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 commented 4 years ago

@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.

eduardopelitti commented 4 years ago

@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.

RickWoltheus commented 4 years ago

For me adding pushNotification: true also works as expected

nates-dennis commented 4 years ago

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

rnnyrk commented 4 years ago

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?

RickWoltheus commented 4 years ago

@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
}
rnnyrk commented 4 years ago

@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
  }
}
RickWoltheus commented 4 years ago

@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

rnnyrk commented 4 years ago

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.

ShaneMatthias commented 4 years ago

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? :/

eduardopelitti commented 4 years ago

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:

        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!

santoshvarma4u commented 4 years ago

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?

arnabadhikari2009 commented 2 years ago

the same issue When the app is killed after clicking the notification from the tray it not triggered Notifications.events().registerNotificationOpened.. is there any way to trigger this event to route specific screens from notification?