I am building a react native Android/IOS app using Expo and I need to have a scheduled notification come in and run some code regardless of whether the app is in the foreground or background.
I currently have this background task defined with Expo:
TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({data, error, executionInfo}) => {
console.log('Received a notification in the background!');
Notifications.scheduleNotificationAsync({
content: {
title: "HOORAY",
body: 'HOORAY',
data: {type: 'pre'}
},
trigger: {
seconds: 10,
},
});
});
Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
Which is all done in the global scope as per the docs
I have tried to use the Notifications.scheduleNotificationAsync
(shown below) and tried sending a push notification from the Firebase dashboard and I receive them fine but the background notification task doesn’t run regardless of whether the app is in the foreground or background.
Notifications.scheduleNotificationAsync({
content: {
title: "Example title",
body: 'Example body',
data: {test: 'true'}
},
trigger: {
seconds: 10,
},
});
It’s also worth noting that the task is being registered, TaskManager.getRegisteredTasksAsync()
gives me this [{"options": {}, "taskName": "BACKGROUND-NOTIFICATION-TASK", "taskType": "remote-notification"}]
From what I understand from the docs the background notification task I have set in TaskManager
should run when a notification comes in, regardless of whether the app is in the foreground or background as per the docs
I am running an Expo (version 51.0.10) development build on a real Android device (Pixel 7 Pro).
Where am I going wrong?
2
Answers
According to the issue below, the background task only works for data-only notifications.
https://github.com/expo/expo/issues/29622
When I run the same code, it generates this error below even with the updated dependencies, directly correct, reinstallation of the node_modules files
My code