invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.87k stars 226 forks source link

onBackgroundEvent not triggered on iOS for remote notifications #1133

Open riad40 opened 3 weeks ago

riad40 commented 3 weeks ago

I'm encountering an issue where the onBackgroundEvent handler is not triggered on iOS when a remote notification is received. The same functionality works perfectly fine on Android.

Environment:

Notifee version: ^9.1.2 React Native version:"0.72.5" Platform: iOS

Relevant Code:

async function initializeNotifications() {
  await notifee.setNotificationCategories([
    {
      id: '// some id',
      actions: [
        { id: 'YES', title: 'YES', foreground: false },
        { id: 'NO', title: 'NO', foreground: false },
        { id: 'CHECK', title: 'CHECK', foreground: true, authenticationRequired: true },
      ],
    },
  ]);
}

async function onMessageReceived(message) {
  console.log('Message received:', message);
  const notificationData = { /* ... */ };
  await notifee.displayNotification({
    ...notificationData,
    android: {
      channelId: '// some channel id',
      actions: [
        { title: 'YES', pressAction: { id: 'YES' } },
        { title: 'NO', pressAction: { id: 'NO' } },
        { title: 'CHECK', pressAction: { id: 'CHECK' } },
      ],
    },
    ios: {
      categoryId: '// some category id',
    },
  });
}

notifee.onBackgroundEvent(async ({ type, detail }) => {
  console.log('Background event received:', type, detail);
  switch (type) {
    case EventType.ACTION_PRESS:
      await handleNotification(detail);
      break;
    case EventType.PRESS:
      // i navigate to certain screen
      break;
  }
});

messaging().setBackgroundMessageHandler(onMessageReceived);
initializeNotifications().catch(console.error);
LunatiqueCoder commented 3 weeks ago

Possible duplicate. See: