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

Created on 17 May 2016  Â·  39Comments  Â·  Source: zo0r/react-native-push-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?

Most helpful comment

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

All 39 comments

I'm having this same issue. I'm using node-gcm to send the notification to the app.

Same here. Some help on how to overcome this issue would be great.

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.

@eduardb even when your application is completely closed?

Completely, as in swiped away from the recents screen, yes.

Awesome, i couldn't achieve that. Would you be so kind to share some
example on how to make it work?

On Wed, May 25, 2016 at 1:43 PM, Eduard Bolos [email protected]
wrote:

Completely, as in swiped away from the recents screen, yes.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/zo0r/react-native-push-notification/issues/72#issuecomment-221631388

Have a look over this PR: https://github.com/novoda/spikes/pull/57

@eduardb how exactly do you have the Alert firing? I have an alert on the first line in onNotification, and it's working fine when the app is closed for iOS, but for Android it's not triggering.

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.

I don't know if its related but I never get onNotification, weather the app is in the foreground or background.

https://github.com/zo0r/react-native-push-notification/issues/93

@npomfret https://github.com/rskull/react-native-push-notification
I was solved with this repository.

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.

Hey all,
I think I'm having a similar issue,

When the app is closed (swiped from recent on android and swiped up in iOS) and I receive a notification, it is displayed properly by the OS but when I click it the popInitialNotification callback returns wrong data.

In iOS I receive:
{foreground:false}

In Android I receive:
null

Other then that everything seems to work perfectly!

Any idea what is going on?

Thanks,
kfir

I'm facing the same issue, the approach of @eduardb is not working for me, nothing is triggered when the app is entirely closed, I tried using the Alert, but nothing call onNotification method.

@florindobre Could you please provide a simple example snippet of you test?

Sending a notification with only the "data" field set (remove the "notification" field) works

How can we solve this?

@frangeris This pull request was supposed to resolve this issue 137

Not sure @zo0r published a new version yet but you can checkout this version and see if that works for you

@kfiroo Thanks for the info.

Not sure if I'm correct, Am I missing something?

module.exports = React.createClass({
    componentWillMount() {
        PushNotification.configure({
            popInitialNotification: (notification) => {
                // when app is entirely closed and click on push notification
                console.log('received', notification)
            },
            onNotification: (notification) => {
                // ...
            }
        })
        // ...
    }
})

@frangeris Looks like you might have misread the docs

When calling PushNotification.configure the popInitialNotification field is a boolean flag indicating whether to pop the initial notification or not, if true, it will try to pop the notification then normalize it and call the onNotification function you passed to the configure.

Hope that makes sense to you :)

@kfiroo Thanks for the aclaration, about the value of the property as function instead of boolean, I was just testing a different approach, because neither of both is working for me, but ok using true (default) still not working.

Thanks

@frangeris I think we need more information to be able to help you, like:

  • Android / iOS?
  • Are you using the latest code for this library?
  • Is the app closed or in the background?
  • Do you receive the notification in your phone at all?
  • Android 4.2.2
  • 2.0.1
  • Closed
  • Yep, everything is working fine, except when app is entirely closed

Thanks

@frangeris @kfiroo when the app is entirely closed (Android) the callback onNotification is never called, its how RN works (the bridge java->node->react is dead at this point), the onNotification will be called when the user tap the notification again.

@zo0r then how do we trigger the notification message when the app is closed, or is this not possible?

Thanks @zo0r, valuable information!

What would you suggest for approach this?

@frangeris @amrdraz its not possible for now (2.0.1), I think a way to capture and execute the callback is re-open the activity in background (open and moveTaskToBack?) (if dead) and then execute onNotification again.

As its not an user-intended behavior, (I think) sending the 'forcesReactNativeDelivery': bool prop in the push data to re-open (if dead) the activity and execute the onNotification callback.

Note that forcesReactNativeDelivery thing is not implemented yet, just a suggestion.

@zo0r @amrdraz @frangeris Actually my PR made it possible, I'm using it in my app on production.

If I understand it correctly, this is the case in every Android app, the app is closed so you cannot handle the initial notification right away.
This is why the notification data is stored on an Intent that is available on the Activity.

In RNPushNotificationHelper.java#L213 we create a PendingIntent that wraps the notification Intent and later when the app starts and the JS code calls popInitialNotification this code in
RNPushNotification.getInitialNotification we get the current Activity from it we get the Intent which holds the notification data which we return to the client.

What do you think?

@kfiroo you're right, but as I understand what @frangeris want is theonNotification trigger immediately after the notification is received even if the app is closed.

@frangeris Am I right?

Not exactly that case, this is the perfect scenario:

  1. App is entirely closed
  2. Push notification on phone
  3. User click on that notification
  4. Receive that data in onNotification

If I could get onNotification when user click on notification when the app is closed, that will be fine...

That's what I can't get @kfiroo, could you provide an example snippet please?

@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'
        })
    }
})

@kfiroo I confirm that when I sent myself a push notification with the correct format it worked

so the problem is that I test push notifications using this script

curl -X POST -H "Authorization: key=MYAPIKEY" -H "Content-Type: application/json" -d '
{
  "data": {
      "Title": "Hello there",
      "Breif": "I got this"
  },
  "to" : "REGISTRATIONID"
}' 'https://gcm-http.googleapis.com/gcm/send'

changed it to

curl -X POST -H "Authorization: key=MYAPIKEY" -H "Content-Type: application/json" -d '
{
  "data": {
      "title": "Hello there",
      "message": "some message text",
      "data": {
         "Title": "Hello there",
         "Breif": "I got this"
      }
  },
  "to" : "REGISTRATIONID"
}' 'https://gcm-http.googleapis.com/gcm/send'

@kfiroo @zo0r
Do you believe there should be a more flexible way of not conforming to a certain format?

and the second question is what are the available options? do I get to pass all the other otpions available to me in the localNotification method?

Either way this is enough for now thank you for your time
@frangeris I hope this solves your problem as well

@amrdraz I'm using Firebase Cloud Messaging, so checking docs...

Thanks @kfiroo

@amrdraz Great to hear that!
We could probably tweak the code to be less strict regarding the remote notification format, but I'm not an Android or iOS developer so I can't say if there is a standard that we should conform to.

Currently, as I can see from the code, the only restrictions are that the top level object will have a data field and that inside this data object we need a message fields and it has to be a string.

Here we look for the data field on the root object - RNPushNotificationListenerService.java#L18 - not sure it's mandatory.
Here we simply return if we have no message field - RNPushNotificationHelper.java#L92

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!

@frangeris Great to hear :)
Good luck

@zo0r I think we can close this

Yep, go ahead...

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

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)
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cookienawer picture cookienawer  Â·  3Comments

anativ picture anativ  Â·  3Comments

Benzer1406 picture Benzer1406  Â·  3Comments

nidzovito picture nidzovito  Â·  3Comments

edwinharly picture edwinharly  Â·  3Comments