question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cancelLocalNotifications() does not work on Android

See original GitHub issue

I have tested this extensively to see if there was something wrong with the way I was excuting cancelLocalNotification() but it does not appear that this works when providing an id on Android. I am executing it exactly per the documentation.

After adding debug logging into the Anrdoid pacakge I believe I have found the root cause of the issue. In RNPushNotificationHelper.java the method cancelScheduledNotification(ReadableMap userInfo) tries to extract the id from the provided JSON map and then find the correct notification to cancel.

https://github.com/zo0r/react-native-push-notification/blob/6a81fba73b55f1ac975a2568486297102998150c/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java#L783-L786

The problem here is that the matches method in RNPushNotificationAttributes.java is being used incorrectly in this case. That method tries to match every element of the userInfo object with the notification it is iterating on which contains additional info such as the title, message etc. If the user has only provided the id attribute this always returns false.

https://github.com/zo0r/react-native-push-notification/blob/6a81fba73b55f1ac975a2568486297102998150c/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java#L200-L247

What is needed in this check is a simple method that matches the id’s only.

For instance something like this in RNPushNotificationHelper.java:

RNPushNotificationAttributes notificationAttributes = fromJson(notificationAttributesJson);
if (notificationAttributes.idMatches(userInfo)) {
    cancelScheduledNotification(id);
}

and this in RNPushNotificationAttributes.java

public boolean idMatches(ReadableMap userInfo) {
try {
    if(this.userInfo == null || userInfo == null) {
        return false;
    }
        return this.id.equals(userInfo.getString("id"));
    } catch(Exception e) {
        return false;
    }
}

I am happy to submit a pull request for this issue. I am working on my own fork at the moment.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Dallas62commented, Aug 19, 2021

Fixed in 8.0.0. Regards

1reaction
dimadeveatiicommented, Aug 24, 2021

@Dallas62 this is still not working in latest v8.0.0.
Please see https://github.com/zo0r/react-native-push-notification/issues/2122

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native PushNotification.cancelLocalNotifications not ...
cancelLocalNotifications not working ... I'm trying to do simple reminder app with RN and zo0r ... cancelAllLocalNotifications().
Read more >
@react-native-community/push-notification-ios - npm
A function that will be passed the current badge number. cancelLocalNotifications(). PushNotificationIOS.cancelLocalNotifications(userInfo) ...
Read more >
Local Notifications | React Native Notifications
Triggering local notifications is fully compatible with React Native ... Note that scheduled notifications are not yet implemented on Android.
Read more >
PushNotificationIOS - React Native
A function that will be passed the current badge number. cancelLocalNotifications() ​. PushNotificationIOS.cancelLocalNotifications(userInfo) ...
Read more >
Send push notifications to React Native apps using Azure ...
However, you do not need to run the app on both Android and iOS in order to complete this tutorial. You can follow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found