Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifee EventType.PRESS not firing when notification is pressed in background state iOS

I have a working react-native app which I'm now integrating Notifee into. I am only prioritizing iOS platform at the moment, so assume iOS only for the rest of the question. Thank you in advance!

In my index.js, prior to registering the App component, I'm setting the onBackgroundEvent event listener, which is correct according to the documentation:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import notifee, {EventType} from '@notifee/react-native';

notifee.onBackgroundEvent(async ({type, detail}) => {
  console.log('onBackgroundEvent', event);
  if (event.type === EventType.PRESS) {
    console.log('User pressed the notification.', event.detail.pressAction?.id);
  }
});

AppRegistry.registerComponent(appName, () => App);

When I deliver a remote notification from my server while my application is in the background state and then press the notification, I see only the following logs:

 LOG  handleBackgroundMessage
 LOG  onBackgroundEvent 3 // 3 === EventType.DELIVERED

So the background event listener is being set up correctly, but the EventType.PRESS isn't firing as expected. I only ever receive the EventType.DELIVERED event.

Here is the code I'm using to display the notification:

const handleBackgroundMessage = async message => {
  console.log('handleBackgroundMessage');
  await notifee.requestPermission();
  // Display a notification
  const notificationPayload = {
    title: message.data.title + ' pdosprewq',
    body: message.data.body,
  };
  await notifee.displayNotification(notificationPayload);
};

I've searched through Github issues and the notifee docs, and nothing seems to document any extra implementation to receive the EventType.PRESS. Any help is appreciated!

package.json:

    "@notifee/react-native": "^7.7.1",
    "react": "18.2.0",
    "react-native": "0.71.8",
like image 373
stanjk997 Avatar asked Sep 05 '25 21:09

stanjk997


1 Answers

I seem to have found a solution/workaround.

The docs are misleading, particularly the code snippets in these sections:

https://notifee.app/react-native/docs/ios/interaction#press-action

https://notifee.app/react-native/docs/events#app-open-events (I tried the bootstrap sequence, but was not receiving the initialNotification there either).

I still am not receiving the EventType.PRESS in the background event handler, but I was finally able to receive it in the foreground event handler.

const App: React.FC = () => {
  useEffect(() => {
    return notifee.onForegroundEvent(({type, detail}) => {
      switch (type) {
        case EventType.PRESS:
          console.log('User pressed notification', detail.notification);
          break;
      }
    });
  }, []);
  ...

This appears to be a bit slower than the background event handler, as there is latency (~1s) from when the app loads to when the PRESS event is received. If anyone knows a better solution, or if this is the incorrect way to handle this, please let me know! I hope this helps someone who is struggling with this same issue.

like image 186
stanjk997 Avatar answered Sep 08 '25 11:09

stanjk997