react-nativepush-notificationexpoexpo-notifications

How to change whether or not to receive notifications within the expo app


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. enter image description here 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?


Solution

  • there are multiple solutions for this.