I am receiving push notifications via expo-notification. I created a button within the app that allows you to set whether or not to receive notifications. The desired behavior is:
If this button is on, the app will receive push notifications.
When the button is OFF, the app cannot receive push notifications.
I am wondering how to handle receiving push notifications within the app. It does not set the right to receive notifications. I want to save the value of AsyncStorage, and set whether or not to display when a notification is received according to the value.
const BACKGROUND_NOTIFICATION_TASK = "BACKGROUND-NOTIFICATION-TASK";
TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({ data, error, executionInfo }) => {
if (error) {
console.log("error occurred");
}
if (data) {
console.log("data-----", data);
}
});
Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
The above function is a function set for background reception.
I tried unregisterTaskAsync
when the button is in OFF state and I still receive the notification in the background. What method should I use to implement changing notification settings in-app?
there are multiple solutions for this.
The first thing is that set a flag in the user table on backend and if that is on then send notifications. if that flag is off that no notification will be send to user. The background and kill state notification can only be handle on this way. because these can be handle from backend.
For foreground notification you can create flag in redux store and based on that you can display notifications to user.
The third one is that if user turn off push notification then set his device token to null in that backend. and no notifications will be received.