Search code examples
react-nativenotificationsreact-native-push-notification

Notification action buttons action without showing the app


I have a local notification with action buttons 'Start' and 'Remind later'. When I click on either of two, the app opens. I want to keep it for the Start button, but for Remind later button I want to silently run some code (reschedule notification on a later time) without opening an app.

Is this possible to do?

    function onOpened(notification) {
        if (notification.action == 'Start') {
            cancelNotification(notification.id)
            NavigationService.navigate(notification.title)
        } else {
            cancelNotification(notification.id)
            // reschedule
            let dateTime = new Date(notification.fireDate)
            dateTime.setMinutes(dateTime.getMinutes() + 30)
            scheduleNotification(notification.id, notification.title, notification.message, dateTime)
    }
}

This code works, but as I said, it bring the app to the foreground, and I'd like to avoid it.


Solution

  • You need to include your full code if you looking for help, but from your tags, I assume you are using React Native Push Notifications. If that's the case you might want to look at this Action click brings the app from background to foreground issue. Basically, you need to go through some native codes and change a few lines.