I have a React Native/Expo app wherein I am trying to use Firebase Cloud Messaging on an Android Device. My web instance of this works just fine, but I am stuck on implementing a successful Firebase Cloud Messaging initialization on Android.
I am building the app with expo build:android
and downloading it onto my phone, so that I may test the Google Login in conjunction with a few other API calls that happen.
I am completely stuck on this React Native Firebase error: you attempted to use a firebase module that's not installed on your Android project by calling "firebase.app()"
firebase.tsx
import { Platform, Alert } from "react-native";// ANDROIDimport messaging from '@react-native-firebase/messaging'// Android uses the google-services.json file for config// WEBimport { initializeApp } from 'firebase/app';import { getMessaging, onMessage, getToken } from 'firebase/messaging';import { FIREBASE_API_KEY, FIREBASE_AUTH_DOMAIN, FIREBASE_DATABASE_URL, FIREBASE_PROJECT_ID, FIREBASE_STORAGE_BUCKET, FIREBASE_MESSAGING_SENDER_ID, FIREBASE_APP_ID, FIREBASE_WEB_VAPID_KEY, // @ts-ignore} from '@env';const firebaseConfig = { apiKey: FIREBASE_API_KEY, authDomain: FIREBASE_AUTH_DOMAIN, databaseURL: FIREBASE_DATABASE_URL, projectId: FIREBASE_PROJECT_ID, storageBucket: FIREBASE_STORAGE_BUCKET, messagingSenderId: FIREBASE_MESSAGING_SENDER_ID, appId: FIREBASE_APP_ID,};const init = async () => { switch(Platform.OS) { // ANDROID INIT case 'android': const alreadyAuthed = await messaging().hasPermission(); const authStatus = await messaging().requestPermission(); const enabled = (authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL || alreadyAuthed); if (enabled) { const token = await messaging().getToken(); messaging().onMessage(async remoteMessage => { Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage)); }); messaging().setBackgroundMessageHandler(async remoteMessage => { console.log('Message handled in the background!', remoteMessage); }); if (token) { return token; } } return null; // WEB INIT case 'web': let web_messaging; let web_app; if (!web_app) { web_app = initializeApp(firebaseConfig); } web_messaging = await getMessaging(web_app); console.log(messaging); onMessage(web_messaging, (payload) => { console.log('Message received') console.log(payload) }) if (web_messaging) { try { const token = await getToken(web_messaging, { vapidKey: FIREBASE_WEB_VAPID_KEY }) console.log(token); if (token) { return token; } } catch(err) { // TODO err handling console.log(err) } } return null; // TODO case 'ios': break; }};const FIREBASE = { init,};export default FIREBASE;
app.json
{"expo": {"name": "frontend-2","slug": "frontend-2","version": "1.0.0","orientation": "portrait","icon": "./src/assets/images/icon.png","scheme": "myapp","userInterfaceStyle": "automatic","splash": {"image": "./src/assets/images/splash.png","resizeMode": "contain","backgroundColor": "#ffffff" },"updates": {"fallbackToCacheTimeout": 0 },"assetBundlePatterns": ["**/*" ],"ios": {"supportsTablet": true },"android": {"adaptiveIcon": {"foregroundImage": "./src/assets/images/adaptive-icon.png","backgroundColor": "#ffffff" },"googleServicesFile": "./firebase-google.json","package": "com.michaelsolomon.frontend2","config": {"googleSignIn": {"certificateHash": "--redacted--" } } },"web": {"favicon": "./src/assets/images/favicon.png" },"plugins": ["@react-native-google-signin/google-signin" ] }}
build.gradle
dependencies { classpath 'com.google.gms:google-services:4.3.3'
app/build.gradle
dependencies { implementation 'com.google.firebase:firebase-core:16.0.1' implementation 'com.google.firebase:firebase-messaging:17.0.0'}apply plugin: 'com.google.gms.google-services'
MainApplication.java
import io.invertase.firebase.app.ReactNativeFirebaseApp;
Does expo build:android
re-build the /android
file and export it to Expo? I have made numerous changes the coincide with various guides about this particular error. How can I use expo build:android
and source it from my Bare Workflow/Ejected /android
file?
Can I use the same package for Android and Web? I was having a lot of trouble making the same firebase/app
and /messaging
package work for Android, so I've been trying to follow documentation. I have not seen a working solution for this/my issue, yet. I was not able to call getToken
, and thus, save the device token in my backend on Android. Should I just use expo-notifications
?