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

  • The mistakes message you are seeing is indicating that Firebase hasn't been initialized to your app. This is an important step earlier than the use of any Firebase services, which include Firebase Cloud Messaging (FCM) for push notifications. Here's how you could solve the problem:

    Initialize Firebase: Ensure Firebase is initialized at the beginning of your utility. This is normally accomplished on your access report (together with App.Js or index. Js). Below is an instance of ways you may initialize Firebase:

    import * as Firebase from 'firebase';
    import 'firebase/messaging';
    
    // Your Firebase config object
    const firebaseConfig = {
      apiKey: 'PAPI-KEY',
      authDomain: 'Auth-Domain',
      databaseURL: 'Database-Url',
      projectId: 'project-id',
      storageBucket: 'storage-bucket',
      messagingSenderId: 'messaging-sender-id',
      appId: 'app-id',
    };
    
    // Initialize Firebase
    if (Firebase.apps.length === 0) {
      Firebase.initializeApp(firebaseConfig);
    }
    

    Replace the values in firebaseConfig together with your actual Firebase configuration values.

    Check Configuration Files: Double-check that your google-services. Json report is efficaciously positioned to your project root and that the app.Config.Ts record has been configured properly in line with the Expo documentation.

    Use a Real Device: While you cited looking to check on an emulator, it is really worth noting that Firebase Cloud Messaging (FCM) may have obstacles or one-of-a-kind conduct on emulators. If viable, attempt checking out on a actual tool to peer if the error persists.

    Review Firebase and Expo Versions: Ensure you're using compatible versions of Firebase and Expo. Sometimes, newer or older variations might have issues that have been resolved in different versions.