Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete previous notification(s) using react-native-push-notification

I'm writing an app that sends a local push notification every fifteen minutes while a background timer is running via react-native-push-notification. If notification n isn't acted on by the user, when notification n+1 is pushed, I'd like to delete notification n.

The things I've tried so far are to set popInitialNotification to true when running PushNotification.configure() and setting ongoing to true when calling PushNotification.localNotification(). I've also tried adding an id when calling localNotification() and then calling PushNotification.cancelLocalNotifications({id: myId}), but the documentation explicitly says that cancelLocalNotifications() only cancels scheduled notifications (and I'm pretty sure it means only future ones).

componentWillMount() {
    PushNotification.configure({
        permissions: {
            alert: true,
            badge: true,
            sound: true
        },
        popInitialNotification: true,
    });
}

doPushNotification() {
    if (AppState.currentState !== 'active') {
        PushNotification.localNotification({
            vibration: 500,
            message: 'my message',
            ongoing: true,
        });
    }
}

Currently I'm only working on the Android version, but soon I'll work on the iOS version, so a general solution (or the solution for each) would be most helpful.

I'm not totally sold on react-native-push-notification, either, if there's a better React Native library out there; I just haven't found one.

Edit

I figured it out for Android. Setting the id when calling PushNotification.localNotification() to the same value as the previous notification will overwrite the notification.

I'm still installing XCode on my Mac, so I can't test the current behavior on iOS just yet. However, the react-native-push-notification readme says that id is an Android-only option (as is ongoing). I may still need help getting the iOS notifications to do what I want.

like image 508
dx_over_dt Avatar asked Mar 09 '17 19:03

dx_over_dt


3 Answers

The docs for react-native-push-notification state that you can cancel a ios local notification using userInfo like this:

PushNotification.localNotification({
  ...
  userInfo: { id: '123' }
  ...
});
PushNotification.cancelLocalNotifications({id: '123'});

I have tested this, and it works.

For android, this somehow worked:

Creating the notification :

 PushNotification.localNotificationSchedule({
      message: message ,
      date: new Date(date),
      repeatType: "minute",
      id: JSON.stringify(id),
      userInfo: { id: JSON.stringify(id) }
    });

 PushNotification.cancelLocalNotifications({ id: id});

Having the id stringified worked with cancelling the android push notification.

like image 177
Jordan Daniels Avatar answered Nov 18 '22 21:11

Jordan Daniels


you can add this code inside handleAppStateChange(appState)

   if (appState === 'active') {
     PushNotification.cancelAllLocalNotifications()
   }
like image 35
Govan Avatar answered Nov 18 '22 20:11

Govan


if you are useing react-native-push-notification lib. then you can use this code in componentDidMount section

PushNotification.getDeliveredNotifications((all) => {
   console.log(all, "notification liast");
   PushNotification.removeAllDeliveredNotifications();
});

here you can get all notification list and clear all list after open your app. these code work for me in ANDROID device (i did not test it in IOS device)

like image 1
Kamal Kishor Avatar answered Nov 18 '22 21:11

Kamal Kishor