【问题标题】:Ionic Firebase Plugin not installed未安装 Ionic Firebase 插件
【发布时间】:2019-03-23 06:04:28
【问题描述】:

我创建了一个新的 ionic 项目并为此添加了 firebase,但每次我执行 ionic serve -c 并打开 DevApp 时,我都会在控制台中看到以下错误:

  • Ionic Native:尝试调用 Firebase.subscribe,但未安装 Firebase 插件。
  • 安装 Firebase 插件:'ionic cordova plugin add cordova-plugin-firebase'

离子信息:

 ionic (Ionic CLI)  : 4.1.2 (C:\Users\xxx\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.2.0

科尔多瓦:

cordova (Cordova CLI):8.1.2 (cordova-lib@8.1.1)

Cordova Platforms     : ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.0, (and 5 other plugins)

系统:

NodeJS : v8.12.0 (C:\Program Files\nodejs\node.exe)
   npm    : 6.4.1
   OS     : Windows 10

app.component.ts:

export class MyApp {
  rootPage: any = HomePage;

  constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public toastCtrl: ToastController, private firebase: Firebase) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

      try {
        this.initializeFirebase();
      } catch (error) {
        this.firebase.logError(error);
      }

    });


  }

  initializeFirebase() {
    if (!this.platform.is("core")) {
      this.firebase.subscribe("all");
      this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
    }
  }
  initializeFirebaseAndroid() {
    this.firebase.getToken().then(token => { });
    this.firebase.onTokenRefresh().subscribe(token => { })
    this.subscribeToPushNotifications();
  }
  initializeFirebaseIOS() {
    this.firebase.grantPermission()
      .then(() => {
        this.firebase.getToken().then(token => { });
        this.firebase.onTokenRefresh().subscribe(token => { })
        this.subscribeToPushNotifications();
      })
      .catch((error) => {
        this.firebase.logError(error);
      });
  }
  subscribeToPushNotifications() {
    this.firebase.onNotificationOpen().subscribe((response) => {
      if (response.tap) {
        console.log(response.body);
        //Received while app in background (this should be the callback when a system notification is tapped)
        //This is empty for our app since we just needed the notification to open the app
      } else {
        console.log(response.body);
        //received while app in foreground (show a toast)
        let toast = this.toastCtrl.create({
          message: response.body,
          duration: 3000
        });
        toast.present();
      }
    });
  }
}

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

import { Firebase } from '@ionic-native/firebase';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Firebase
  ]
})
export class AppModule {}

有人遇到同样的问题吗?

【问题讨论】:

  • 你有解决办法吗?

标签: angular firebase cordova ionic-framework ionic4


【解决方案1】:

您必须在 provider 下将 firebase 添加到您的项目应用模块。您可能还需要为将来安装的任何插件执行此操作。

如果您使用的是 Ionic3,则为文件 app.module.ts

【讨论】:

  • 你有没有运行过这个命令ionic cordova plugin add cordova-plugin-firebase?如果它不起作用尝试运行ionic cordova plugin rm cordova-plugin-firebase 然后再次添加插件。
  • 您的根文件夹中有google-services.json 文件吗?
  • 是的,Android 和 iOS 都添加了。
【解决方案2】:

在使用导入类的对象之前使用platform.ready()函数:

constructor(public qrScanner: QRScanner) {
                // solve the problem - "plugin not installed".
                platform.ready().then(()=>{
                  this.qrscanner();
                })

}

【讨论】:

    【解决方案3】:

    尝试检查 firebase 云消息插件

    ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated
    

    也许仔细检查 google-services.json 位置

    projects\src\app\google-services.json
    

    最后重新添加 android 并检查插件

    ionic repair
    

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题,然后我尝试了 FCM 而不是 Firebase。

      我会建议你使用 FCM 来做同样的事情。它对我来说很好用。

      ionic cordova plugin rm cordova-plugin-firebase
      ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated
      npm install @ionic-native/fcm
      

      【讨论】:

      • 我在控制台中收到插件未安装错误,我已删除平台然后再次添加但错误未删除,先生,我该怎么办?
      • 1.此插件仅适用于设备。 2. 再次使用该命令添加插件。 3. 平台准备好时调用插件。
      • 1.我只在设备中进行了 2 次测试。尝试了 2 次,但结果相同。3。已在就绪函数中调用
      • 您使用的是哪个操作系统
      • 我用的是mac机
      【解决方案5】:

      TL;DR - 检查您使用的 Cordova 版本并更新它以匹配项目依赖要求(即npm install -g cordova@9.0.0)。


      很可能正在使用的构建工具的版本(Cordova)与需要使用的包(插件)不兼容。

      cordova-plugin-firebase-analytics 在运行 Cordova 8.1 时遇到了类似的问题。看起来它在 Android 上运行良好,但在 iOS 上没有按预期运行。在调试时,我能够在控制台的错误日志中检测到错误,例如

      尝试调用 [method] 但 FirebaseAnalytics 插件没有 已安装

      重新添加包含 --verbose 标志的 iOS 平台显示如下:

      为 ios 安装“cordova-plugin-firebase-analytics” 插件不支持此项目的 cordova 版本。科尔多瓦:8.1.1,失败的版本要求:> = 9.0.0

      安装至少支持的最低版本 (9.0.0) 并重建 iOS 平台(删除平台、添加平台、准备/运行/模拟 iOS 构建)后,问题得到解决。

      【讨论】:

        猜你喜欢
        • 2019-03-09
        • 2018-06-03
        • 1970-01-01
        • 2018-04-13
        • 2020-02-08
        • 2020-02-06
        • 2018-10-09
        • 2017-10-28
        • 2018-04-24
        相关资源
        最近更新 更多