how to detect Onpress in Push notification React Native

3.2k Views Asked by At

I am using react-native-push-notification package in my project. when the user clicks a notification. How to Detect it in android and ios using OnNotification Function?? here is the code

import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "@react-native-community/push-notification-ios";

PushNotification.configure({
    onRegister: function (token) {
      console.log("TOKEN:", token);
    },

    onNotification: function (notification) {
      console.log("[onNotification] :", notification);
 
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    },

    onAction: function (notification) {
      console.log("[onAction]", notification.action);
      console.log("[onAction]:", notification);
    },

    onRegistrationError: function (err) {
      console.error(err.message, err);
    },

    permissions: {
      alert: true,
      badge: true,
      sound: true,
    },

    popInitialNotification: true,

    requestPermissions: true,
  });
1

There are 1 best solutions below

2
On BEST ANSWER

You can use the userInteraction property of the notification object argument passed to the onNotification callback, as mentioned in their documentation.

Here's an example

PushNotification.configure({
  onNotification: function (notification) {
    if (notification.userInteraction) {
      // Handle notification click
    }

    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },
});