React-native-push-notification: iOS: Can't remove local notifications from Notification Center

Created on 7 Jan 2019  路  2Comments  路  Source: zo0r/react-native-push-notification

After showing a local notification with

PushNotification.localNotification({
    ...
    userInfo: { id: '123' }
    ...
});

I tried the following methods to remove it from the Notification Center

  • PushNotification.cancelLocalNotifications({id: '123'});
  • PushNotification.cancelAllLocalNotifications();

Both of them are not working for me so I found a workaround

Workaround

This method is for Android only:
PushNotification.clearAllNotifications();

I edited it from this

function() {
    return this.callNative('clearAllNotifications', arguments);
}

to this

function() {
    if (Platform.OS === 'ios') return this.callNative('removeAllDeliveredNotifications', arguments);
    return this.callNative('clearAllNotifications', arguments);
}

So now it works for iOS and I can use it to remove all the notifications

Stale

Most helpful comment

A workaround in any shared code looks very similar:

import { Platform, PushNotificationIOS } from 'react-native';
...
if (Platform.OS === 'ios') {
  PushNotificationIOS.removeAllDeliveredNotifications();
}

Thanks for putting me on the right path @simonepauro !

All 2 comments

A workaround in any shared code looks very similar:

import { Platform, PushNotificationIOS } from 'react-native';
...
if (Platform.OS === 'ios') {
  PushNotificationIOS.removeAllDeliveredNotifications();
}

Thanks for putting me on the right path @simonepauro !

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings