如何修复 ionic 中 getToken() 的错误?

How to fix an error with getToken() in ionic?

我正在尝试获取用户的 fcm 令牌并将其上传到数据库。

我遵循了这个教程:https://www.youtube.com/watch?v=SOOjamH1bAA

这是我的功能:

async getToken() {

    let token;

    if(this.platform.is('android')) {
      token = await this.firebaseNative.getToken();
    }
    if (this.platform.is('ios')) {
      token = await this.firebaseNative.getToken();
      await this.firebaseNative.grantPermission();
    }

    return this.saveTokenToDb(token);
  }

进口:

import { Injectable } from '@angular/core';
import { Firebase } from '@ionic-native/firebase/ngx';
import { Platform } from "ionic-angular";
import { AngularFireDatabase } from '@angular/fire/database';
import { Device } from '@ionic-native/device';

app.module:

imports: [
   ...
    AngularFireModule.initializeApp(FIREBASE_CONFIG),
    AngularFireDatabaseModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
...
  ],
  providers: [
...
    AngularFireDatabase,
    Firebase,
    Device,
    FcmProvider
  ]

当我 运行 我的应用程序出现此错误时:

Uncaught (in promise): TypeError: Object(__WEBPACK_IMPORTED_MODULE_1__ionic_native_core__["cordova"]) is not a function. (In 'Object(__WEBPACK_IMPORTED_MODULE_1__ionic_native_core__["cordova"])(this, "getToken", {}, arguments)', 'Object(__WEBPACK_IMPORTED_MODULE_1__ionic_native_core__["cordova"])' is an instance of Object)
getToken@http://10.0.0.23:8100/build/vendor.js:64573:123
http://10.0.0.23:8100/build/main.js:1109:74
step@http://10.0.0.23:8100/build/main.js:1072:27
http://10.0.0.23:8100/build/main.js:1047:75
t@http://10.0.0.23:8100/build/polyfills.js:3:21507
http://10.0.0.23:8100/build/main.js:1043:36
handleDb@http://10.0.0.23:8100/build/main.js:271:26
onSignIn@http://10.0.0.23:8100/build/main.js:254:26

callWithDebugContext@http://10.0.0.23:8100/build/vendor.js:15783:47
dispatchEvent@http://10.0.0.23:8100/build/vendor.js:10689:36
http://10.0.0.23:8100/build/vendor.js:39326:65
onInvokeTask@http://10.0.0.23:8100/build/vendor.js:5436:43
runTask@http://10.0.0.23:8100/build/polyfills.js:3:10844
invokeTask@http://10.0.0.23:8100/build/polyfills.js:3:16801
p@http://10.0.0.23:8100/build/polyfills.js:2:27654
v@http://10.0.0.23:8100/build/polyfills.js:2:27894

所以我真的不知道问题出在哪里,但是 运行 构建然后通过 Xcode 在设备上安装它对我有用。

使用 cordova-plugin-fcm 而不是 firebase 插件。关注这个 link https://medium.com/@t1tan1um/fcm-integration-for-cordova-hybrid-apps-c679f5fc1988

输入命令 npm install --save @ionic-native/fcm@4.20.0。而不是 npm install --save @ionic-native/fcm。 fcm 版本 5+ 不适用于 ionic 3