React-native-fcm: Do stuff while app is closed

Created on 29 Dec 2016  Â·  35Comments  Â·  Source: evollu/react-native-fcm

How can i do stuff on my app, if the app is closed and the user not click on the notification banner.
I want to do some stuff before the user click on the notification.

question

Most helpful comment

@niros001 Please understand that it's not Javascript that triggers the notification but the underlying native code

All 35 comments

I don't think you can since Javascript is not running

I know the javascript is running 100% cause the javascript trigger the notification to show up when the app is closed. but i need to do somethings while the trigger execute (before the user see the notification)

Wait, with app closed do you mean app running in background or app killed?

both

Javascript doesn't run while the app is killed but it runs while the app is in the background. You can intercept the notification with FCM.on("notification", callback) and do stuff with it

What kind of payload are you sending from the server? notification or data only?

I tried only notification, only data and both

@niros001 you can't do stuff before your notification is shown, only after with FCM.on. Otherwise you can send data only payload and use FCM.presentLocalNotification after your intercept your data only notification with FCM.on

Fcm. on() doesn't work with app killed

I use this:

this.notificationUnsubscribe = FCM.on("notification", notif => {
            console.log("Notification", notif);
            if (notif && notif.local) {
                return;
            }
            this.sendRemote(notif);
        });

The console log executed only when the app open.
I tried to send any kind of notification. T_T

Ok, so help me get clarified the different behaviour you get for FCM.on("notification") in different situations:
-App running in foreground: You can log the notification
-App running in background. You can not log the notification
-App not running/killed. You can not log the notification

Is this right?

@niros001 Please understand that it's not Javascript that triggers the notification but the underlying native code

Yes you right, so how can i update some app data if user not clicked my notification?

You currently can not, unfortunately, unless you know Java/ObjC and start writing native code

I think you might find the content_available flag useful

s70103-145021

@niros001 yes. content_available will trigger a notification event in JS world before use clicking notification (when app is running in background).

@evollu what's the behaviour of notifications when app is closed/killed after swiping up? Will we receive notification anyway in Ios but we won't in Android?
I'm planning to improve documentation with better info

Android will always wake up the app and show notification. during the wake up you can run some native code
iOS will show notification if you use notification payload, but you can only run native code when content_available is true. but is app is killed manually system will ignore content_available flag
@ethan37 thanks for all the help.

Is there a way of knowing by the response that the app is closed?

Do you mean response from firebase server? Nope

@evollu Oh, thanks..

Android will always wake up the app and show notification. during the wake up you can run some native code
@evollu
How is this done? I'm not getting the notification when the app is not running.

@dudukenesto did you pass "notification" payload?

@evollu actually it was a permission issue. I tested on my Xiaomi redmi note 3 pro. All Xiomi devices
have miui (8) op system, which require giving "autostart" permission to the application, other wise - no push notification after you close the app. After granting the permission - works great. Tested on other Adnroid devices, works well without having to grant this special permission.
Thank you for sharing this component, great job!

@dudukenesto Can you create a pull request to add this permission to readme? Thanks

Have this pull request been implemented?

which pull request?

Seems like @dudukenesto didn't make a PR that adressed:

»All Xiomi devices have miui (8) op system, which require giving "autostart" permission to the application, other wise - no push notification after you close the app. After granting the permission - works great. «

oh great... another custom event. @Norfeldt do you mind open an PR for that?

I wouldn't mind doing it (and would have done it instead of asking about
it) if I just knew what @dukdukensto did - its not obvious from his reply.

Sorry

On Fri, Jul 21, 2017, 17:09 Libin Lu notifications@github.com wrote:

oh great... another custom event. @Norfeldt https://github.com/norfeldt
do you mind open an PR for that?

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/evollu/react-native-fcm/issues/251#issuecomment-317027226,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AApbllA6q-gMOToAXfI5cmYT6PC0lAPRks5sQL8sgaJpZM4LXR5K
.

>

Venlig hilsen/Yours sincerely
Lasse Nørfeldt

he haven't open any PR yet.

Sorry to comment on closed thread.

I think it's painful for users to follow guide about enabling autostart in MIUI devices manually. It's better to do it programmatically. The code is here https://stackoverflow.com/questions/39368251/how-to-enable-autostart-option-for-my-app-in-xiaomi-phone-security-app-programma/41696993#41696993

Unfortunately I didn't understand about Java, so I hope anyone who understand Java may create PR about that :beers:

are they serious....
can you try add that code in your react-native project, MainActivity.java -> onCreate to see if it works or have the crash problem? I don't have xiaomi phone

@pewh is https://github.com/evollu/react-native-fcm/issues/546 continue of you comment in this thread?

Okay, I'll test it.

My case here is enabling autostart in Xiaomi programmatically, where WhatsApp have done it automatically.

And #546 is different case, where I already enable autostart manually, but still not receive notification when app is killed. But on WhatsApp, it receive notification although has been killed. I test my app like Samsung, notification is working well although has been killed. I don't have any clue why it's happen.

EDIT
I already update question on #546

Was this page helpful?
0 / 5 - 0 ratings