Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Android: 'onNotification' not executing when app is closed and clicked to notification #72

Closed
rkostrab opened this issue May 16, 2016 · 39 comments

Comments

@rkostrab
Copy link

rkostrab commented May 16, 2016

[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?

@j0hn
Copy link

j0hn commented May 19, 2016

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

@eduardb
Copy link

eduardb commented May 24, 2016

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

@eduardb
Copy link

eduardb commented May 25, 2016

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.

@j0hn
Copy link

j0hn commented May 25, 2016

@eduardb even when your application is completely closed?

@eduardb
Copy link

eduardb commented May 25, 2016

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

@j0hn
Copy link

j0hn commented May 25, 2016

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
#72 (comment)

@eduardb
Copy link

eduardb commented May 25, 2016

Have a look over this PR: novoda/spikes#57

@florindobre
Copy link

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

@florindobre
Copy link

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.

@rskull
Copy link
Contributor

rskull commented Jun 8, 2016

if revert this commit, It work?

1aa5c96

@npomfret
Copy link
Contributor

npomfret commented Jun 8, 2016

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

#93

@rskull
Copy link
Contributor

rskull commented Jun 9, 2016

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

@florindobre
Copy link

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.

@kfiroo
Copy link
Contributor

kfiroo commented Jul 11, 2016

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

@frangeris
Copy link

frangeris commented Jul 20, 2016

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?

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

@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

@frangeris
Copy link

@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) => {
                // ...
            }
        })
        // ...
    }
})

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

@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 :)

@frangeris
Copy link

@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

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

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

@frangeris
Copy link

  • Android 4.2.2
  • 2.0.1
  • Closed
  • Yep, everything is working fine, except when app is entirely closed

Thanks

@zo0r
Copy link
Owner

zo0r commented Jul 20, 2016

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

@amrdraz
Copy link

amrdraz commented Jul 20, 2016

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

@frangeris
Copy link

Thanks @zo0r, valuable information!

What would you suggest for approach this?

@zo0r
Copy link
Owner

zo0r commented Jul 20, 2016

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

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

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

@zo0r
Copy link
Owner

zo0r commented Jul 20, 2016

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

@frangeris
Copy link

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?

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

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

@amrdraz
Copy link

amrdraz commented Jul 20, 2016

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

@amrdraz
Copy link

amrdraz commented Jul 20, 2016

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

@frangeris
Copy link

frangeris commented Jul 20, 2016

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

Thanks @kfiroo

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

@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

@frangeris
Copy link

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!

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

@frangeris Great to hear :)
Good luck

@kfiroo
Copy link
Contributor

kfiroo commented Jul 20, 2016

@zo0r I think we can close this

@frangeris
Copy link

Yep, go ahead...

@zo0r zo0r closed this as completed Jul 20, 2016
@didil
Copy link

didil commented Apr 25, 2017

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

@lucask42
Copy link

lucask42 commented Sep 5, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests