React Native - Clear previous push notifications on receiving local push notification

1.8k Views Asked by At

I'm using react-native-push-notification for scheduling and generating local push notifications in React Native and I'm trying to clear previously received notification on receiving any scheduled notification or auto remove currently received notification after sometime but I can't find any way to achieve it on iOS. One way is to use timeoutAfter property provided by the library which is for android only. For iOS, other way is to use PushNotificationIOS.removeAllDeliveredNotifications() if I can call any method on receiving local notification but onNotification() doesn't trigger on receiving local notification. It only triggers when receiving or tapping remote notification or tapping local notification. Is there any other way or any other library to achieve this? This is my code for scheduling local notifications:

PushNotification.localNotificationSchedule({
    channelId: 'channelId',
    id,
    message: '...',
    timeoutAfter: 10000,
    soundName: 'default',
});

notification config:

PushNotification.configure({
    onRegister: function (token) {
        console.log('TOKEN:', token);
    },
    onNotification: function (notification) {
        console.log('NOTIFICATION:', notification);
    },
    onAction: function (notification) {
        console.log('ACTION:', notification.action);
        console.log('NOTIFICATION:', notification);
        // process the action
    },
    onRegistrationError: function (err) {
        console.error(err.message, err);
    },
    permissions: {
        alert: true,
        badge: true,
        sound: true,
    },
    popInitialNotification: true,
    requestPermissions: Platform.OS === OS.IOS,
});
1

There are 1 best solutions below

0
On

Although its a generally solid and well-maintained library, I started running into some of the same issues with react-native-push-notification. Eventually, I transitioned to Notifee: https://notifee.app/ for generating/handling local notification events. It does a fairly good job at bridging the underlying differences w/ iOS and Android, providing a pretty uniform API across platforms. It appears to expose cancellation APIs, although in my case I didn't need to implement those. The production license requires a fee, but it was nothing crazy, and it was absolutely worth it in my case. Also, Notifee integrates nicely with react-native firebase for remote notifications, fwiw.