react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
731 stars 280 forks source link

addEventListener for notification not called #438

Open jzinrh opened 2 months ago

jzinrh commented 2 months ago

React Native Version: 0.73.6

Reproduction Steps:

  1. Background App
  2. Send push notification
  3. Tap push notification Using the following code, "entered" is never logged.
  const onRemoteNotification = (notification) => {
    console.log('entered')
    // Use the appropriate result based on what you needed to do for this notification
    const result = PushNotificationIOS.FetchResult.NoData;
    notification.finish(result);
  };

  useEffect(() => {
    const type = 'notification';
    PushNotificationIOS.addEventListener(type, onRemoteNotification);
    return () => {
      PushNotificationIOS.removeEventListener(type);
    };
  });

Notes:

therealkh commented 2 months ago

the same here

saibbyweb commented 2 months ago

onRemoteNotification should get triggered if/when the app is in foreground. PushNotificationIOS.getInitialNotification() is what you're looking for (if you're coming from background i.e opening the app by tapping on the notification)

  useEffect(() => {
    // Check if the app was opened by a notification
    PushNotificationIOS.getInitialNotification().then(notification => {
      if (notification) {
          const notificationData = notification.getData();
          console.log(notificationData);
      }
    });
  }, []);
jzinrh commented 2 months ago

I'm able to get that working when the app is launched (i.e. after I force close it) from the notification, but not when it's backgrounded.