React-native-push-notification: Android can not cancel local notification

Created on 9 Mar 2018  路  4Comments  路  Source: zo0r/react-native-push-notification

I have used:

"react": "16.0.0",
"react-native": "0.50.4",
"react-native-push-notification": "^3.0.2"

I have problem with action cancel local notification.

How do i push a local notification schedule:

const localNotificationSchedule = (id, title, message, time) => {
    let notification = {
        id: id,
        ticker: title, 
        autoCancel: true,
        largeIcon: "ic_notification_large",
        smallIcon: "ic_notification_small",
        bigText: message,
        vibration: 300, 
        title: title,
        message: message,
        soundName: 'default',
        date: time
    }

    PushNotification.localNotificationSchedule(notification);

    return notification;
}

And I cancel local notification by call: PushNotification.cancelLocalNotifications(notification); - notification is the object i create to push noti above. But the notification do not cancel.

I try to debug and see that:

public void cancelScheduledNotification(ReadableMap userInfo) {
        for (String id : scheduledNotificationsPersistence.getAll().keySet()) {
            try {
                String notificationAttributesJson = scheduledNotificationsPersistence.getString(id, null);
                if (notificationAttributesJson != null) {
                    RNPushNotificationAttributes notificationAttributes = fromJson(notificationAttributesJson);
                    if (notificationAttributes.matches(userInfo)) {
                        cancelScheduledNotification(id);
                    }
                }
            } catch (JSONException e) {
                Log.w(LOG_TAG, "Problem dealing with scheduled notification " + id, e);
            }
        }
    }

In this method, matches always return false. Because i found some values do not have in scheduledNotificationsPersistence:

"playSound":false
"ongoing":false
"userInteraction":false
"vibrate":false
"repeatTime":0

How can i cancel notification with only the id value?

Most helpful comment

This works for me

PushNotification.localNotificationSchedule({
    id: "123", // unique id as string
    number: 0, // Must be 0
    userInfo: {
        id: "123", // same id as above
    },
    message: "Local notification",
    date: moment().add(30, 'seconds').toDate(),
});
PushNotification.cancelLocalNotifications({id: "123"});

All 4 comments

This works for me

PushNotification.localNotificationSchedule({
    id: "123", // unique id as string
    number: 0, // Must be 0
    userInfo: {
        id: "123", // same id as above
    },
    message: "Local notification",
    date: moment().add(30, 'seconds').toDate(),
});
PushNotification.cancelLocalNotifications({id: "123"});

@FredrikMBP Oh. So i need add userInfo? I'm missing it.

@sinhpn92 I followed the readme at https://github.com/zo0r/react-native-push-notification/issues/639
TBH I am not sure if it's required or not

firebase.notifications().cancelAllNotifications();

Was this page helpful?
0 / 5 - 0 ratings