ios icon indicating copy to clipboard operation
ios copied to clipboard

addEventListener for notification not called

Open jzinrh opened this issue 1 year ago • 5 comments

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:

  • Remote notification does show up on device; so I don't think it's a permission issue
  • Tried with both type = "notification" and "localNotification"

jzinrh avatar Apr 17 '24 14:04 jzinrh

the same here

therealkh avatar Apr 23 '24 08:04 therealkh

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

saibbyweb avatar Apr 28 '24 15:04 saibbyweb

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.

jzinrh avatar Apr 30 '24 21:04 jzinrh

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 background.

just realized this as well today, its working fine on android in state Foreground, Killed, and Background but on iOS when app is in Background state, and user tap the notification, it just open the app and do nothing~

did found found any workaround on this @jzinrh @saibbyweb ?

abdymm avatar Aug 05 '24 06:08 abdymm

I am facing a similar issue with setNotificationCategories notifications. Below is the code I am using to set reply notification, but I am able to get reply text in foreground only when the app is killed, and I reply from notification, then it does not log anything. Any help in this case would be appreciated. Thanks.

`if (Platform.OS == 'ios') {

  PushNotificationIOS.setNotificationCategories([
    {
      id: 'INCOMING_SMS_CATEGORY',
      actions: [
        {id: 'open', title: 'Open', options: {foreground: true}},
        {
          id: 'ignore',
          title: 'Desruptive',
          options: {foreground: true, destructive: true},
        },
        {
          id: 'text',
          title: 'Text Input',
          options: {foreground: true},
          textInput: {buttonTitle: 'Send'},
        },
      ],
    },
  ]);

  PushNotificationIOS.getInitialNotification().then(function (notification) {
    logMessage(`We have received a push notification : 2222`, notification);
    if (notification != null) {
      logMessage('app open by notif tap ', notification)
      if (notification && notification._data && notification._data.notification_type) {
        
        let notificationType = notification._data.notification_type
        if (notificationType == 'xmpp_push') {
          CallSession.setState({ initialTabRoute: 'Chat' });
        }else if(notificationType == "incoming_sms_push"){
          CallSession.setState({ initialTabRoute: 'SMS' });
        }
      }
    }
  });
  PushNotificationIOS.addEventListener("register", async (token) => {
    logMessage('apns register token ', token);
    await AsyncStorage.setItem("_apnsToken", token)
  })
  PushNotificationIOS.addEventListener("localNotification", (notification) => {
    logMessage('local notification recieved ', notification.toString())
  })
  PushNotificationIOS.addEventListener("registrationError", (error) => {
    logMessage("register error ", error.message.toString())
  })
  PushNotificationIOS.addEventListener("notification", async (notification) => {
    logMessage(`We have received a push notification : `, notification);
    notification.finish(PushNotificationIOS.FetchResult.NoData)
    logMessage("notification_received_PNIOS => ", notification)
  })
}`

Satyajeetsinh-9 avatar Apr 21 '25 12:04 Satyajeetsinh-9