cancelLocalNotifications() does not work on Android
See original GitHub issueI 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.
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.
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:
- Created 2 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top GitHub Comments
Fixed in 8.0.0. Regards
@Dallas62 this is still not working in latest v8.0.0.
Please see https://github.com/zo0r/react-native-push-notification/issues/2122