Giter Club home page Giter Club logo

Comments (39)

didil avatar didil commented on May 29, 2024 7

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024 4

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!

from react-native-push-notification.

florindobre avatar florindobre commented on May 29, 2024 3

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.

from react-native-push-notification.

lucask42 avatar lucask42 commented on May 29, 2024 3

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

from react-native-push-notification.

eduardb avatar eduardb commented on May 29, 2024 1

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.

from react-native-push-notification.

florindobre avatar florindobre commented on May 29, 2024 1

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.

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024 1

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

from react-native-push-notification.

j0hn avatar j0hn commented on May 29, 2024

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

from react-native-push-notification.

eduardb avatar eduardb commented on May 29, 2024

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

from react-native-push-notification.

j0hn avatar j0hn commented on May 29, 2024

@eduardb even when your application is completely closed?

from react-native-push-notification.

eduardb avatar eduardb commented on May 29, 2024

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

from react-native-push-notification.

j0hn avatar j0hn commented on May 29, 2024

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)

from react-native-push-notification.

eduardb avatar eduardb commented on May 29, 2024

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

from react-native-push-notification.

florindobre avatar florindobre commented on May 29, 2024

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

from react-native-push-notification.

rskull avatar rskull commented on May 29, 2024

if revert this commit, It work?

1aa5c96

from react-native-push-notification.

npomfret avatar npomfret commented on May 29, 2024

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

#93

from react-native-push-notification.

rskull avatar rskull commented on May 29, 2024

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

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

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?

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

@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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

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

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

@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

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024
  • Android 4.2.2
  • 2.0.1
  • Closed
  • Yep, everything is working fine, except when app is entirely closed

Thanks

from react-native-push-notification.

zo0r avatar zo0r commented on May 29, 2024

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

from react-native-push-notification.

amrdraz avatar amrdraz commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

Thanks @zo0r, valuable information!

What would you suggest for approach this?

from react-native-push-notification.

zo0r avatar zo0r commented on May 29, 2024

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

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

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

from react-native-push-notification.

zo0r avatar zo0r commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

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?

from react-native-push-notification.

amrdraz avatar amrdraz commented on May 29, 2024

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

from react-native-push-notification.

amrdraz avatar amrdraz commented on May 29, 2024

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

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

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

Thanks @kfiroo

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

@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

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

@frangeris Great to hear :)
Good luck

from react-native-push-notification.

kfiroo avatar kfiroo commented on May 29, 2024

@zo0r I think we can close this

from react-native-push-notification.

frangeris avatar frangeris commented on May 29, 2024

Yep, go ahead...

from react-native-push-notification.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.