evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 682 forks source link

Headless JS #279

Open alialamine opened 7 years ago

alialamine commented 7 years ago

React native has added a Headless option to run javascript code while the app is in the background (and not running).

See https://facebook.github.io/react-native/docs/headless-js-android.html

Any chance of interating it?

Ohh and there is this thread on productpains to add this feature for ios, if it's being integrated please add a link in the docs for people to vote for it. TY.

evollu commented 7 years ago

does this have anything to do with firebase?

bpaczkowski commented 7 years ago

I think the question is whether it's possible to use react-native-fcm to run an action in the background using Headless JS based on an incoming push notification, without requiring the user to actually press the notification. I need this kind of functionality myself, can this be done?

alialamine commented 7 years ago

@bpaczkowski Yes I'm doing this in my app and it's working.

On android, I have a service that already has Headlessjs (the javascript is already subscribed to such a task), so react-native is always running in the background, alongside the js thread.

On ios, I have the background location mode and the js thread is always running in the background with the notifications being handled by the app.

I'm not sure that this is all on android (I only know that it's working now, will find out if this ever stops in a future date and update this thread, but for now a test that checks it is enough), but on IOS a background mode that allows you to run code in the background is enough to keep react-native running and handle the notifications.

alialamine commented 7 years ago

Ohh and you have to have the data part of the notification set when sending the it on the server. Otherwise the app will only get notified when the user clicks the notification.

bpaczkowski commented 7 years ago

But always running Headless JS in the background is kind of defeating the whole point of push notifications. What I had in mind is starting a Headless JS service only when a notification is received, and stopping it immediately after whatever I had to do about that notification is finished.

alialamine commented 7 years ago

TLDR; No effect on battery life, not implemented yet in react-native-fcm.

As far as I can tell, the way that react-native handles the app if there is a Headlessjs service attached is to keep at least part of the js thread running.

If you are afraid on the battery life, this is my experience: our app is using the location service in the background to get a high precision location every 5 minutes (reason for the headless service). That being said, we are only using 0.5% of the battery over a single day (and I'd blame the location service for that, as disabling it will show our app as using 0% if it stays in the background). So I see they did a good job of reducing the footprint while building it. I think they turn off all of the rendering and diff related stuff, so only the calls that has to do with the calls from java to the js thread will be executed as long as the app is in the background. Don't quote me on that last part as it's just an educated guess from my app's behaviour.

As for react-native-fcm sending those headlessjs calls itself, it's very much possible, but has yet to be implemented (a pull request is most welcome).

evollu commented 7 years ago

In latest repo, android will wake up JS thread and execute JS code in notification callback just as when app is open. So there is no need for headless JS now