I have searched the internet for quite a while now, and I couldn't find anywhere a proper documentation to implement background fetch when app is terminated(not in background).
I have followed the documentation and implemented it like this:
useEffect(() => {
BackgroundFetch.configure(
{
forceAlarmManager: true,
enableHeadless: true,
stopOnTerminate: false,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
startOnBoot: true,
},
async (taskId) => {
console.log('[BackgroundFetch] taskId: ', taskId);
PushNotification.localNotificationSchedule({
bigText:
'We will be watering your Plants in few minutes. Please check if your schedule is updated in Device',
subText: 'Background task',
title: 'Watering Plants',
color: '#60ad5e',
message: 'Expand to show more',
actions: ['Okay'],
group: 'group',
date: new Date(Date.now() + 5 * 1000), //Change this to (scheduled time - some minutes) as to notify the user earlier.
});
BackgroundFetch.finish(taskId);
},
);
}, []);
// And with with #scheduleTask
BackgroundFetch.scheduleTask({
taskId: 'com.foo.customtask',
delay: 15000, // milliseconds
forceAlarmManager: true,
periodic: false,
});
so here basically I want to run API fetch and show push notification to the user. So there is the chance that the user removes the app from background(i.e he TERMINATES). So I want to show this Push Notification even when the app is terminated in ANDROID.
Could someone tell me what I am doing wrong here?
For Android, you have to use firebase to send push notification. I highly recommend the @react-native-firebase app, and their documentation.
Basically, AFTER YOU MADE THE INSTALLS, the code is just
In index.js
See their documentation for the firebase app https://rnfirebase.io/
And the messaging part https://rnfirebase.io/messaging/usage