You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用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:

1. Double-Check Firebase Configuration Files

Missing or incorrect config files are the #1 cause of init failures:

  • For Android: Make sure google-services.json is placed directly in the App_Resources/Android directory, and it's the exact file you downloaded from your Firebase project console (don't modify it manually).
  • For iOS: Ensure GoogleService-Info.plist lives in App_Resources/iOS, and you've confirmed the bundle ID in the plist matches your app's bundle ID in package.json.
2. Verify Plugin & NativeScript Version Compatibility

Outdated or mismatched versions often break things:

  • Run tns plugin list to check your nativescript-plugin-firebase version, then cross-reference it with your NativeScript version (check package.json for the nativescript entry). The plugin maintains a compatibility table—stick to versions that explicitly support your NS version.
  • If needed, refresh the plugin:
    tns plugin remove nativescript-plugin-firebase && tns plugin add nativescript-plugin-firebase@latest
    
    (Or use a specific compatible version if latest isn't a fit for your NS setup.)
3. Ensure Required Permissions & Capabilities

FCM needs specific permissions to work:

  • Android: Open App_Resources/Android/src/main/AndroidManifest.xml and confirm these permissions are present:
    <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" />
    
    Also, verify the Firebase messaging receiver is declared (the plugin should add this automatically, but sometimes it gets lost):
    <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.
4. Fix Your Initialization Code

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").

5. Clean & Rebuild Your Project

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:47:32