React-native-push-notification: cancelLocalNotification doesnt cancel scheduled notification

Created on 17 Dec 2018  路  17Comments  路  Source: zo0r/react-native-push-notification

It doesnt claim to but not sure if its a bug or not. The reason is that cancelAllLocalNotifications does work on scheduled notifications but i imagine this cant be the only way to cancel a scheduled notification as there might be many that are scheduled and you wouldn't want to delete all of them just to delete one of them.

Any insight on this?

Most helpful comment

Something I had overlooked in the docs initially was that it needs to be the userInfo object's id and not the notification id itself. Don't know if that is your problem, but it is how I got it to work for me instead of cancelling all notifications.

PushNotification.localNotificationSchedule({
    id: '1',
    userInfo: { id: '123' },
    message: 'My Scheduled Notification Message',
    date: new Date(Date.now() + (5 * 1000))
  })
  PushNotification.cancelLocalNotifications({ id: '123' })

Also, to piggy back on these post, why is it that we can't cancel the notification by id anyway? It seems to me it would make more sense to do so that way as opposed to using the userInfo id.

All 17 comments

@Return-1 did you manage to handle cancelling local notification by id ? any alternate solution would be appretiate

@codemeall not really. im using cancel all and recreating the notifications from scratch. not the best solution but one that works.

Something I had overlooked in the docs initially was that it needs to be the userInfo object's id and not the notification id itself. Don't know if that is your problem, but it is how I got it to work for me instead of cancelling all notifications.

PushNotification.localNotificationSchedule({
    id: '1',
    userInfo: { id: '123' },
    message: 'My Scheduled Notification Message',
    date: new Date(Date.now() + (5 * 1000))
  })
  PushNotification.cancelLocalNotifications({ id: '123' })

Also, to piggy back on these post, why is it that we can't cancel the notification by id anyway? It seems to me it would make more sense to do so that way as opposed to using the userInfo id.

@Return-1 removing tag from notification details solved the issue for me

@JulsRicketti userInfo is only necessary for cancelling notification on iOS

Hi, I'm also having some serious issues with this.

I'm trying to make a calendar app for iOS and Android (so people can plan something, and get a notification on a specific date). So far using Pushnotification.scheduleNotif works perfect and users get their notification when they should receive it.

BUT, as I have all these 'planned items' in a list, I haven't found a way to cancel the notification for a specific item when the user deletes that item from his calendar.

Is there any way of having the id (that now is a number in a string? which is very weird), to be an actual string? That way I could just use the unique key my items have in the database (this is a string with characters, numbers and underscores), and use that to cancel a notification.

Thanks to anyone that can help me with this.

EDIT: It works on iOS simulator to cancel local scheduled notifications by just passing a number without the ', but that means that the documentation is wrong, as it says that it should be wrapped with a '. But that doesn't work on Android...

My solution:

const id = 1;

PushNotification.localNotificationSchedule({
    id:  `${id}`
    message: 'My Scheduled Notification Message',
    date: new Date(Date.now() + (5 * 1000))
  })

PushNotification.cancelLocalNotifications({ id: `${id}` })

After investigating this issue on Android (using the latest version 3.3.1), I found a problem with the scheduleLocalNotification() method being used in RNPushNotification.java class.

if (bundle.getString("id") == null) {...}
Even if the Bundle has 'id' parameter (which I checked by printing all keys of the Bundle), the code fails and generates a Random integer. Thus, whenever I try to cancel a notification with my supplied ID, it fails to find a matching ID.

Thus, after updating the method from -

@ReactMethod
    public void scheduleLocalNotification(ReadableMap details) {
        Bundle bundle = Arguments.toBundle(details);
        // If notification ID is not provided by the user, generate one at random
        if (bundle.getString("id") == null) {
            bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
        }
        mRNPushNotificationHelper.sendNotificationScheduled(bundle);
    }

to :

@ReactMethod
    public void scheduleLocalNotification(ReadableMap details) {
        Bundle bundle = Arguments.toBundle(details);
        boolean isIdFound = false;
        if (bundle != null) {
            for(String extraKey : bundle.keySet()){
                if(extraKey.equals("id")) {
                    isIdFound = true;
                }
            }
            // If notification ID is not provided by the user, generate one at random
            if (!isIdFound) {
                Log.e(LOG_TAG, "ID not supplied from ReactNative");
                bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
            }
            mRNPushNotificationHelper.sendNotificationScheduled(bundle);
        }
    }

my problem of cancelling scheduled notification is resolved (on Android).
Now when I schedule and cancel a notification, the IDs are the same, and I also don't see the supposedly cancelled notifications firing up.

If this helps anybody else, we can submit a PR.

Hi @kckunal2612
I will take a look into it!

@kckunal2612
Can you check the dev branch:

yarn add zo0r/react-native-push-notification#dev

OR

npm install zo0r/react-native-push-notification#dev

I think I already made a fix on this branch

@Dallas62
Still the same - https://github.com/zo0r/react-native-push-notification/blob/dev/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java

 @ReactMethod
    public void scheduleLocalNotification(ReadableMap details) {
        Bundle bundle = Arguments.toBundle(details);
        // If notification ID is not provided by the user, generate one at random
        if (bundle.getString("id") == null) {
            bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
        }
        mRNPushNotificationHelper.sendNotificationScheduled(bundle);
    }

The null check is what's causing my code to fail.

I tested the Example project and the id is correctly generated.
I had this bug with the random value for id but it not the case anymore.

@kckunal2612 I know the code is still the same, but the fix is not the same as your fix.
You can test version 3.4.0

Can confirm - issue was resolved on 3.4.0.
Thanks so much @Dallas62 ! You rock !
Let me know how I can buy you a beer 馃嵑
Cheers!

@Dallas62 thanx bro!! its also helped me to delete schedule notification

There is also a bug I will fix ASAP.
Be sure to not pass NaN as ID 馃ぃ馃ぃ
If your are impacted in a release (users are stucked) a fix available on dev branch.

@Dallas62 can you mention in doc that to use custom sound just need to pass only its name. do not need to use android.resource to use it.

@kckunal2612 I don't understand what you mean, there is already:
https://github.com/zo0r/react-native-push-notification#custom-sounds
If it's not enough, PR are welcome 馃槈

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cidevant picture cidevant  路  3Comments

ahmadallo1 picture ahmadallo1  路  3Comments

sungjinoh picture sungjinoh  路  3Comments

uendar picture uendar  路  3Comments

selimonline picture selimonline  路  3Comments