15

As far as I know, push notification can be used in react native, even when the app is closed. would it be possible to use push notification to run a background task in react native?

for example, when a push notification is sent to a device, it runs a function to fetch data from server and update database.

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

Ata Mohammadi
  • 3,430
  • 6
  • 41
  • 69
  • 1
    Actually, This can't be done in Javascript. Background Tasks can't be done using Javascript. You can do those things in java inside the React Native application. – Sriraman Oct 16 '16 at 12:50

2 Answers2

5

Push notification and data notification handling part could be done using react-native-firebase easily (I recommend to use react-native-firebase instead of react-native-push-notification because it has wide community support and support for many more firebase services)

And in here it show how to fetch data in background for iOS; with the few modification same thing could be done in android also.

Useful links :

EL173
  • 797
  • 9
  • 22
4

Please have a look at react-native-push-notification#silent

Android:

If your Android app is not running when a silent notification is received then this library will start it. It will be started in the background, however, and if the OS starts your app in this way it will not start the react-native lifecycle. This means that if your notification delivery code relies on the react-native lifecycle then it will not get invoked in this situation. You need to structure your app in such a way that push notification. configure gets called as a side effect of merely importing the root index.android.js file.

iOS:

The crucial bit of an iOS silent notification is the presence of the "content-available": 1 field.

Rajan
  • 1,512
  • 2
  • 14
  • 18