Expo-notifications: problem with notification response when app is closed / in the background

73 views Asked by At

I have an Expo React Native app that uses expo-notifications. One user of the app can send notifications to another user of the app, and the receiving user can respond to the notification by pressing one of the actions from the drop down that appears when a user interacts with the notification. This works fine when the app is in the foreground, but when the app is closed, it does not work.

I have set up expo-notifications like this:

useEffect(() => {
    notificationServices
      .registerForPushNotificationsAsync()
      .then((token) => {
        if (customUser && token && customUser.expoPushToken !== token) {
          updateExpoPushToken(customUser.userId, token)
            .then((action) => {
              if (action) {
                dispatch(action);
              }
            })
            .catch((error) => {
              console.log(error);
            });
        }
      })
      .catch((error) => {
        console.log("Error registering for push notifications: ", error);
      });

    notificationServices.setNotificationCategoryPumping();
    notificationListener.current =
      notificationServices.addNotificationReceivedListener();

    responseListener.current =
      notificationServices.addNotificationResponseReceivedListener(customUser!);

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

And the addNotificationResponseReceivedListener function:

export const addNotificationResponseReceivedListener = (
  customUser: CustomUser
) => {
  return Notifications.addNotificationResponseReceivedListener((response) => {
    if (response.actionIdentifier === "response1") {
        const message = {
            to: friendToken,
            sound: "default",
            title: `${nickname} sent you a message`,
            body: "Test",
            data: {},
            ios: { _displayInForeground: true },
        };
        const response = await fetch("https://exp.host/--/api/v2/push/send", {
            method: "POST",
            headers: {
                Accept: "application/json",
                "Accept-encoding": "gzip, deflate",
                "Content-Type": "application/json",
            },
            body: JSON.stringify(message),
        }).catch((error) => {
            console.log(error);
        });
    }
  });
};
```I have also tried to implement `expo-task-manager that runs tasks when the app is in the background, but I don't know how I should implement the `response-received` listener logic to work correctly. Is it necessary to implement expo-task-manager for this purpose or should it work out of the box with expo-notifications? 
0

There are 0 answers