调用firebase.init()初始化NativeScript Firebase插件时遇错误
Hey there, let's tackle this Firebase init error in your NativeScript app step by step. I've seen this pop up a lot when setting up FCM push notifications, so here are the most likely fixes to get things working:
Missing or incorrect config files are the #1 cause of init failures:
- For Android: Make sure
google-services.jsonis placed directly in theApp_Resources/Androiddirectory, and it's the exact file you downloaded from your Firebase project console (don't modify it manually). - For iOS: Ensure
GoogleService-Info.plistlives inApp_Resources/iOS, and you've confirmed the bundle ID in the plist matches your app's bundle ID inpackage.json.
Outdated or mismatched versions often break things:
- Run
tns plugin listto check yournativescript-plugin-firebaseversion, then cross-reference it with your NativeScript version (checkpackage.jsonfor thenativescriptentry). The plugin maintains a compatibility table—stick to versions that explicitly support your NS version. - If needed, refresh the plugin:
(Or use a specific compatible version if latest isn't a fit for your NS setup.)tns plugin remove nativescript-plugin-firebase && tns plugin add nativescript-plugin-firebase@latest
FCM needs specific permissions to work:
- Android: Open
App_Resources/Android/src/main/AndroidManifest.xmland confirm these permissions are present:
Also, verify the Firebase messaging receiver is declared (the plugin should add this automatically, but sometimes it gets lost):<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /><receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> - iOS: Open your project in Xcode, go to the Signing & Capabilities tab, and:
- Add the Push Notifications capability.
- Enable Background Modes and check the Remote notifications box.
Looking at your snippet, let's clean it up and add critical error handling to see what's going wrong:
firebase.init({ onMessageReceivedCallback: (message) => { console.log("Title: " + message.title); console.log("Body: " + message.body); // Guard against undefined data to avoid crashes if (message.data?.foo) { console.log("Value of 'foo': " + message.data.foo); } }, // Don't forget this callback to get the device token for sending notifications onPushTokenReceivedCallback: (token) => { console.log("Firebase push token: " + token); } }) .then(() => { console.log("Firebase initialized successfully! 🎉"); }) .catch((error) => { // This will log the EXACT error—use this to debug further! console.error("Firebase init failed: ", error); });
The .catch() block is non-negotiable here—it'll tell you exactly what's broken (e.g., "missing config file" or "permission denied").
Cached build files can cause weird, unexplained errors. Run these commands to start fresh:
# For Android tns platform remove android && tns platform add android # For iOS tns platform remove ios && tns platform add ios # Clean all cached files tns clean # Run the app again tns run android # or tns run ios
If after all this you still get an error, share the exact message from the .catch() log—that'll help narrow down the issue instantly!
内容的提问来源于stack exchange,提问作者Saidul Amin Nobin

