react-native-push-notification: cancelLocalNotification not working

I have an issue trying to figure out how to make it work with cancelLocalNotification.

This is the local notification definition. Currently, working for Android.

PushNotification.localNotification({
      /* Android Only Properties */
      id: '1',
      ticker: 'Notification Center', // (optional)
      autoCancel: true, // (optional) default: true
      largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
      smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher"
      subText: 'Alert', // (optional) default: none
      color: 'red', // (optional) default: system default
      vibrate: true, // (optional) default: true
      vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
      tag: notification.type, // (optional) add tag to message
      group: 'group', // (optional) add group to message
      ongoing: false, // (optional) set whether this is an "ongoing" notification
      lat: geolocation.lat,
      lon: geolocation.lon,
      title: 'ALERT!', // (optional, for iOS this is only used in apple watch, the title will be the app name on other iOS devices)
      message: notification.message, // (required)
      playSound: false, // (optional) default: true
      soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
      actions: '["Yes", "No"]'  // (Android only) See the doc for notification actions to know more
    })

cancelAllLocalNotifications works but I don’t want to lose all my local notifications.

This is how I’m trying to cancel by attribute my local notifications PushNotification.cancelLocalNotifications({id: '1'}).

What am I doing wrong?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

Ok, I figured out why it wasn’t working for me.

The id cannot be an int, it must be a string. {id:‘33133’} works fine while {id: 33133} will not work

i am unable to cancel my notification in android , PushNotification.localNotificationSchedule({ message: ‘Time to record day mood’, title: ‘How was your day today!’, date: now, //date: new Date(Date.now() + 20000), repeatType: ‘day’, userInfo: { id: ‘2’, screenType: ‘DaysPerformance’ }, id: ‘2’ }); } disableLocalNotificationDays() { PushNotification.cancelLocalNotifications({ id: ‘2’ }); } still i am unable to cancel my notification

Just fixed it with this. In the docs it says that you have to specify an object under the user userInfo options for iOS.

      PushNotification.localNotificationSchedule({
        id: 'myid',
        message: "Your event will begin in 10 minutes. Get ready!",
        userInfo: {
          id: 'myid'
        },
        date: moment().add(10, 'seconds').toDate(),
      });

      PushNotification.cancelLocalNotifications({
        id: 'myid'
      });

Not sure if it works with Android, but it should in theory since your solution works right now. I’d make userInfo.id the same as id

PushNotification.localNotificationSchedule({ id: params.id, alertAction: ‘view’, title: params.title, // (optional) message: params.message, userInfo: { id: params.id, }, playSound: true, soundName: soundName, date: params.date, });

PushNotification.cancelLocalNotifications({id: params.id});

This is what’s working with me, you need to supply the userInfo attribute. And params.id must also be String.