react-native-push-notification: Android: 'onNotification' not executing when app is closed and clicked to notification

[android] Hello, when I put app in the background and I get push notification then onNotification is executed as I expect. But when I close app (using android back button) and I get push notification then onNotification is not executed.

I know onNotification is fired from Java in onNewIntent method. But I really need to pass some data from notification into JavaScript when application was closed before and I click on notification.

How to do it?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 11
  • Comments: 39 (15 by maintainers)

Most upvoted comments

shouldnt some of this be in the documentation ? it ended up working for me but its fairly non trivial to understand that you need a specific message format to get a notification displayed and clicked with the app closed to trigger onNotification

Hey @kfiroo @amrdraz I’ve no idea why now is working 😐

This is what I did, I was using firebase endpoint (not working), then I changed to test using the code of @amrdraz (using GCM endpoint), start working, rollback and test again with firebase endpoint, works… 😕

Is possible that switching between endpoint could fix something behind scenes?

Another note is that data nested inside data is not required, the next request works on both (GCM, FCM):

curl -X POST -H "Authorization: key=<MY_KEY>" -H "Content-Type: application/json" -d '
{
  "data": {
      "title": "Hello there",
      "message": "some message text",
  },
  "to" : "<DEVICE_TOKEN>"
}' 'https://fcm.googleapis.com/fcm/send'

And from React native:

module.exports = React.createClass({
    componentWillMount() {
        PushNotification.configure({
            // popInitialNotification: true,
            onNotification: (notification) => {
                console.log(notification)
            }
        })
    }
})

Thank’s guys for the help!

If you are overriding onNewIntent in MainActivity.java and you do not call super on onNewIntent then that may be preventing onNotification from firing.

This could happen if you were to, say, use react-native-branch-deep-linking and follow their setup instructions you would override onNewIntent like this

    @Override
    public void onNewIntent(Intent intent) {
        setIntent(intent);
    }

A possible fix to get onNotification to fire is to add super.onNewIntent(intent):

    @Override
    public void onNewIntent(Intent intent) {
        setIntent(intent);
        super.onNewIntent(intent)
    }

Sending a notification with only the “data” field set (remove the “notification” field) works. Clicking on a notification triggers onNotification once the app opens. It also gets triggered if the app is in the foreground.

@frangeris When I think about it I had some trouble finding the right structure of the notification I send from my server, that might be your problem!

my GCM payload looks like this:

JSON.stringify({
    data: {
        message: 'some message text',
        sound: 'default',
        // custom data goes here
        data: JSON.stringify({
            key: 'value'
        })
    }
})

After some debugging it looks like the onMessageReceived method from the RNPushNotificationListenerService class gets called when receiving the notification while the app is closed/in the background. But clicking on the notification itself only launches the app and does not pass its data to React.

@zo0r : any ideas on this? iOS working fine with clicking on notifications.

UPDATE: It seems that onNotification is called after all when opening the app from the notification, but for some reason the YellowBox was not appearing when calling console.warn(). Alert.alert() does the trick though.