Expo SDK 46 Android app killed notification tapped but no data received after app starts

  1. SDK Version: 46
  2. Platforms(Android/iOS/web/all): Android

I’m struggling with getting notifications data when my app is killed on Android and the user taps a notification received for that app. I have tested it using my development build and works just as expected, but it doesn’t work in production app, tapping the notification with the app killed doesn’t get the notification data to the production app.

I’m using zustand 4 for app state management

Here’s the code I have come so far:
App.tsx

...
...
export default function App() {
  const lastNotificationResponse = Notifications.useLastNotificationResponse();
...
...
  const { setlastNotificationResponse } = useAppContextStore(
    ({ setlastNotificationResponse }) => ({
      setlastNotificationResponse, // <-- Sets the last notification response in app state
    }),
  );

  useEffect(() => {
...
...
    setlastNotificationResponse(lastNotificationResponse);
  }, [lastNotificationResponse]);
...
...
  setupNotifications();  // <-- setup notification hook

setupNotifications.js

...
...
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});

export default function setupNotifications() {
  const {
    lastNotificationResponse,
  } = useAppContextStore(
    ({
      lastNotificationResponse,
    }) => ({
      lastNotificationResponse,
    }),
  );

  const notificationListener = useRef();
  const responseListener = useRef();

  useEffect(() => {
    registerForPushNotificationsAsync()
      .then(() => null)
      .catch(() => null);

    processLastNotificationResponse(lastNotificationResponse);

    // This listener is fired whenever a notification is received while the app is foregrounded
    notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
      notificationReceivedListener(notification);
    });

    // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
    responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
      notificationResponseReceivedListener(response);
    });

    return () => {
      Notifications.removeNotificationSubscription(notificationListener.current);
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, [lastNotificationResponse]);

  // This listener is fired whenever a user taps on or interacts with a notification (works when app is killed)
  const processLastNotificationResponse = (response) => {
    if (response && response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER) {
      notificationResponseReceivedListener(response);

      return;
    }
  };

  // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
  const notificationResponseReceivedListener = (response) => {
      const message = {
        msgId: response.notification.request.content.data.msgId,
        title: response.notification.request.content.title,
        message: response.notification.request.content.body,
        link: response.notification.request.content.data.link,
        read: false,
      };

      processNotification(message, true);
  };
...
...

Thanks in advise!

Have you found the solution?

Hi @comfable it seems that the issue has been around for quite some time, fortunately, there’s a workaround until Expo gurus find a definitive solution. This piece of code works as expected on iOS and Android. I’ve tested it myself in my production apps.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.