Search code examples
firebasereact-nativepush-notificationexporeact-native-firebase

Error on Expo Push Token Retrieval in Emulator: FirebaseApp Not Initialized


I'm working on a React Native project with Expo and I've encountered a problem while trying to retrieve the Expo push token on an emulator. The error I'm receiving is as follows:

LOG error [Error: Encountered an exception while calling native method: Exception occurred while executing exported method getDevicePushTokenAsync on module ExpoPushTokenManager: Default FirebaseApp is not initialized in this process com.app.development. Make sure to call FirebaseApp.initializeApp(Context) first.]

This error occurs when I attempt to log the token to the console using the getExpoPushTokenAsync method. Here's a snippet of my code:

  useEffect(() => {
  async function configurePushNotifications() {
    const { status: existingStatus } = await Notifications.getPermissionsAsync();
    let finalStatus = existingStatus;

    if (finalStatus !== 'granted') {
      const { status } = await Notifications.requestPermissionsAsync();
      finalStatus = status;
    }

    if (finalStatus !== 'granted') {
      Alert.alert(
        'Permission required',
        'Push notifications need the appropriate permissions'
      );
      return;
    }

    const projectId = Constants.expoConfig?.extra?.eas.projectId;
    try {
      const token = await Notifications.getExpoPushTokenAsync({
        projectId: projectId,
      });
      console.log('token', token);
    } catch (error) {
      console.error('error', error);
    }
  }

  configurePushNotifications();
}, []);

I've added the google-services.json to my project root and have configured the app.config.ts file accordingly. Currently, I'm only testing this on an emulator because I want to console log the token. Why do I keep getting the message about the FirebaseApp not being initialized?

I would appreciate any insights or solutions to resolve this issue. Thank you!

I've tried what's suggested here and it didn't work: firebase.initializeApp() vs FirebaseApp.initializeApp()


Solution

  • You may be using a simulator or emulator

    expo-notifications library is used to request a user's permission and to fetch the ExpoPushToken. It is not supported on an Android Emulator or an iOS Simulator.

    From https://docs.expo.dev/push-notifications/push-notifications-setup/#install-libraries