How to remove a specific notification forever using react-native-push-notification

699 views Asked by At

I am currently using the react-native-push-notification library to schedule and receive notifications in my React Native app. I am able to cancel a scheduled notification using the cancelLocalNotification method, but this only cancels the notification for 24 hours. I want to find a way to remove a specific notification forever, so it will not be rescheduled.

I have tried using the following code to cancel a notification by its ID, but it only cancels it for 24 hours:

const onCancelNotification = async (id: string) => {
    // Get a list of all scheduled notifications
    PushNotification.getScheduledLocalNotifications((notifications) => {
      // Iterate through the list of notifications
      notifications.forEach((notification) => {
        // Check if the notification is the one you want to cancel
        if (notification.id.indexOf(id) === 0) {
          // Cancel the notification
          PushNotification.cancelLocalNotification(notification.id);
        }
      });
    });
  };

I would greatly appreciate any help or suggestions on how to achieve this.

1

There are 1 answers

0
Nouh Belahcen On

This code snippet demonstrates a workaround for removing a specific scheduled local notification in React Native. The function onRemoveNotification takes in an id parameter, which is used to identify the specific notification that needs to be removed.

It's important to note that there is no direct method for removing a specific scheduled local notification in React Native. This code provides a workaround that can be used, but it's important to be aware that it relies on scheduling a notification with an earlier date and setting the repeatType to undefined.

Please note that this code is not a perfect solution and could have side effects on the app.

const onRemoveNotification = async (id: string) => {
  // Get a list of all scheduled notifications
  PushNotification.getScheduledLocalNotifications((notifications) => {
    // Iterate through the list of notifications
    notifications.forEach((notification) => {
      // Check if the notification is the one you want to cancel
      if (notification.data.notificationId?.indexOf(id) === 0) {
        // Create a new date one day earlier than the current scheduled date
        const earlyDate = moment(notification.date).add(-1, "day").toDate();
        // remove the notification
        // schedule the notification with an earlier date and repeat type undefined
        // this will effectively "remove" the notification
        // since it will not be displayed
        PushNotification.localNotificationSchedule({
          id: notification.id,
          title: notification.title,
          message: notification.message,
          repeatType: undefined,
          date: earlyDate,
        });
        // cancel the previous scheduled notification
        PushNotification.cancelLocalNotification(notification.id);
      }
    });
  });
};