Saturday 24 October 2020

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

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,
});


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

No comments:

Post a Comment