Skip to content

addEventListener for notification not called #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jzinrh opened this issue Apr 17, 2024 · 5 comments
Open

addEventListener for notification not called #438

jzinrh opened this issue Apr 17, 2024 · 5 comments

Comments

@jzinrh
Copy link

jzinrh commented Apr 17, 2024

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"
@therealkh
Copy link

the same here

@saibbyweb
Copy link

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
Copy link
Author

jzinrh commented Apr 30, 2024

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.

@abdymm
Copy link

abdymm commented Aug 5, 2024

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 ?

@Satyajeetsinh-9
Copy link

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)
  })
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants