App crashes on iOS when I open the app from a new notification. On production.

Please provide the following:

  1. SDK Version: sdk-40.0.1
  2. Platforms(Android/iOS/web/all): iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

I am having an issue on production mode; when I get a new notification I click on it with the app closed, it opens the app and then it crashes.

The problem is similar to this Standalone app crashes when I tap on notification (only when app is completely closed) - #3 by maketroli but the developer didn’t post the answer.

On my root navigator (navigation/index.tsx) I have the code for the notifications, where locally, it works:

function RootNavigator() {
  React.useEffect(() => {
    if (user?.token) {
      registerForPushNotificationsAsync().then(async (token) => {
        setExpoPushToken(token);
      });

      responseListener.current = Notifications.addNotificationResponseReceivedListener(
        (response: { [key: string]: any }) => {
          const getBody = response.notification.request.content.data
            .notification.request.content.data as NotificationBody;

          setDocDetailID(getBody.document_id);

          navigate("Docs", {
            name: "Docs",
            params: { itemID: getBody.document_id },
          });
        },
      );

      return () => {
        Notifications.removeNotificationSubscription(
          notificationListener.current,
        );

        Notifications.removeNotificationSubscription(responseListener.current);
      };
    }
  }, [user?.token]);

  React.useEffect(() => {
    if (user?.token) {
      if (expoPushToken) {
        const asyncFn = async (): Promise<void> => {
          try {
            const bearer = "Bearer " + user?.token;
            const response = await fetch(PUSH_ENDPOINT, {
              method: "POST",
              headers: {
                Authorization: bearer,
                "X-Requested-With": "XMLHttpRequest",
                "Content-Type": "application/json",
              },
              body: JSON.stringify({ token: expoPushToken }),
            });

            const responseJson = await response.json();

            if (has(responseJson, "error")) {
              Alert.alert(
                "Error",
                "There was an error trying to fetch registerForPushNotificationsAsync. Please try again later.",
              );
            }
          } catch (error) {
            Alert.alert(
              "Error",
              "There was an error while attempting to fetch registerForPushNotificationsAsync. Please try again later.",
            );
          }
        };

        asyncFn();
      }
    }
  }, [expoPushToken, docDetailID, user?.token]);
}

All that works locally, I just uploaded my app with Transporter.app and I am testing it with TestFlight. The notifications arrive properly, but the app crashes.

I am wondering if it has something to do with the navigator not being mounted yet or if I am using the service wrong. When I opened the app the first time it asks me for permissions to send notifications which I accepted.

Any thoughts?

Hey @maketroli, it could be the result of the navigator not being mounted. Have you looked at the nav docs regarding Handling Initialization?

Cheers,
Adam