【问题标题】:How to fix an error with getToken() in ionic?如何修复 ionic 中的 getToken() 错误?
【发布时间】:2019-01-27 12:47:03
【问题描述】:

我正在尝试获取用户的 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

【问题讨论】:

  • 那么您是如何运行您的应用程序的?离子服务?任何 cordova 插件(几乎所有)都需要您在真实设备或模拟器上运行代码

标签: angular firebase ionic-framework firebase-cloud-messaging angularfire


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用 cordova-plugin-fcm 而不是 firebase 插件。按照这个链接 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 2017-02-12
      • 2020-04-30
      • 2019-07-18
      • 2017-08-26
      • 2016-02-25
      • 1970-01-01
      相关资源
      最近更新 更多