React-native-push-notification: cancelLocalNotification not working

Created on 16 Feb 2017  路  16Comments  路  Source: zo0r/react-native-push-notification

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?

Most helpful comment

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

All 16 comments

Same problem for me. @superandrew213 's PR is a step in the right direction.

same issue here

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

@EdmundMai I think the issue is with Android only.

@superandrew213 Oh really? He said "Currently, working for Android."

I think he means PushNotification.localNotification() and PushNotification.cancelAllLocalNotifications() are working but not PushNotification.cancelLocalNotifications({id: '1'})

Is it working for you on Android?

I'm having the same issue. If I add this to the userInfo key of a PushNotification.localNotificationSchedule:

...userInfo: {tag: 'refill'}...

and then try:

PushNotification.cancelLocalNotifications({tag: 'refill'});

the cancel doesn't work. If I try to schedule/cancel/schedule/etc. on Android I end up with multiple notifications that pop instead of them getting canceled and rescheduled. Does anyone have any ideas yet?

---EDIT---

So, in looking at GantMan's code, I saw he referenced a similar issue here: https://github.com/zo0r/react-native-push-notification/issues/259

The localNotificationSchedule must contain an ID and then cancelLocalNotifications can use that ID (but must be called "id" exclusively). Some documentation in the README seems like it would really help solve this issue for a lot of people.

so @syardumi - I'll even go a level deeper. This shouldn't be documented, it should be handled by the module. Perhaps rewording/reworking the API so it handles the mess internally, and quietly, then setting up some tests to ensure that this stays handled.

Just explaining the issue to someone feels messy. Better would be to remove the need to explain.

I need to dismiss a notification that comes from server.
I raise notification like this:

PushNotification.localNotification(notification);
When the user clicks on an Action, I want to dismiss that notification:

(function () {
    // Register all the valid actions for notifications here and add the action handler for each action
    PushNotificationAndroid.registerNotificationActions(['Accept', 'Deny', 'Yes', 'No']);
    DeviceEventEmitter.addListener('notificationActionReceived', function (action) {
        ToastAndroid.show('Action button pressed.', ToastAndroid.LONG);
        // PushNotificationAndroid.clearAllNotifications();
        const info = JSON.parse(action.dataJSON);
        PushNotification.cancelLocalNotifications({tag: info.id});
        NotificationHandlerPipeLine.handleRequest(info);
    });
})();

However, it doesn't do anything.
I can clear all of notifications by this:
PushNotification.cancelAllLocalNotifications();
That is not my desire.
I am using:
"react-native": "0.53.0",
"react-native-push-notification": "3.0.2"

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

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

The same problem, still I'm unable to resolve this issue in android

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.

Same for me, the PushNotification.cancelLocalNotifications({id: '123'}) does not work, not depending on using userInfo

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.

This fixed it for me. Thank you so much!

Was this page helpful?
0 / 5 - 0 ratings