由于无法读取未定义的属性"onTokenRefresh",导致无法在离子应用程序中获取设备令牌 [英] I'm unable to get Device token in an ionic app as a result of Cannot read property 'onTokenRefresh' of undefined

本文介绍了由于无法读取未定义的属性"onTokenRefresh",导致无法在离子应用程序中获取设备令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了两个建议安装以下内容的教程:

I followed two tutorials that suggested installed the following:

ionic cordova插件添加cordova-plugin-fcm-with-dependecy-updatednpm安装@ ionic-native/fcm

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

安装到未安装fcm插件的设备后,我一直收到错误消息,所以我查看了

I kept getting errors after installing to a device that said the fcm plugin isn't installed so I viewed https://www.npmjs.com/package/cordova-plugin-fcm-with-dependecy-updated#fcmontokenrefresh site that suggested uninstalling npm uninstall @ionic-native/fcm # Ionic support is included and conflicts with @ionic-native's implementation.

所以我这样做了,而是改用了它:

So I did that and used this instead:

import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated';


  getUserToken(userId: string) {
    FCM.getToken().then(token => {
      console.log('token');
      console.log(token);
    });
  }

  getToken(userId: string) {
    FCM.onTokenRefresh((fcmToken: string) => {
      console.log('token refresh');
      console.log(fcmToken);
    });
  }

但是现在我得到以下信息:

But now I get the following:

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性'onTokenRefresh'TypeError:无法读取未定义的属性"onTokenRefresh"

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'onTokenRefresh' of undefined TypeError: Cannot read property 'onTokenRefresh' of undefined

这时我被卡住了.如果我使用Ionic插件,则会收到未安装'FCMPluging的信息,并且在使用Cordova的库时会出现无法读取的属性'onTokenRefresh'的未定义错误.任何帮助将不胜感激.

At this point I'm stuck. I get 'FCMPluging isn't installed if I use Ionic plugin and I get the Cannot read property 'onTokenRefresh' of undefined error when I use Cordova's library. Any help would be greatly appreciated.

{
  "name": "",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~9.1.6",
    "@angular/core": "~9.1.6",
    "@angular/forms": "~9.1.6",
    "@angular/platform-browser": "~9.1.6",
    "@angular/platform-browser-dynamic": "~9.1.6",
    "@angular/router": "~9.1.6",
    "@ionic-native/core": "^5.0.7",
    "@ionic-native/image-picker": "^5.27.0",
    "@ionic-native/ionic-webview": "^5.27.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^5.0.0",
    "@ionic/storage": "^2.3.0",
    "angularfire2": "^5.4.2",
    "cordova-plugin-androidx": "^2.0.0",
    "cordova-plugin-androidx-adapter": "^1.1.1",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-fcm-with-dependecy-updated": "^7.2.0",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-ionic-webview": "^4.2.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-telerik-imagepicker": "^2.3.3",
    "cordova-plugin-whitelist": "^1.3.3",
    "firebase": "^7.17.1",
    "moment": "^2.27.0",
    "ngx-google-places-autocomplete": "^2.0.4",
    "rxjs": "~6.5.1",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.901.5",
    "@angular/cli": "~9.1.5",
    "@angular/compiler": "~9.1.6",
    "@angular/compiler-cli": "~9.1.6",
    "@angular/language-service": "~9.1.6",
    "@ionic/angular-toolkit": "^2.1.1",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3",
    "cordova-plugin-whitelist": "1.3.3",
    "cordova-plugin-statusbar": "2.4.2",
    "cordova-plugin-device": "2.0.2",
    "cordova-plugin-splashscreen": "5.0.2",
    "cordova-plugin-ionic-webview": "^4.0.0",
    "cordova-plugin-ionic-keyboard": "^2.0.5"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {
        "ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
      },
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-telerik-imagepicker": {},
      "cordova-plugin-fcm-with-dependecy-updated": {
        "ANDROID_FCM_VERSION": "19.0.0",
        "ANDROID_GRADLE_TOOLS_VERSION": "3.5.3",
        "ANDROID_GOOGLE_SERVICES_VERSION": "4.3.3",
        "ANDROID_DEFAULT_NOTIFICATION_ICON": "@mipmap/ic_launcher"
      },
      "cordova-plugin-androidx": {},
      "cordova-plugin-androidx-adapter": {}
    },
    "platforms": [
      "android",
      "ios"
    ]
  }
}

9.0.0 (cordova-lib@9.0.1)

ionic -v
6.0.1

推荐答案

尝试一下:

import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';

constructor(private fcm: FCM){}

private async pushSetup() {
    await this.platform.ready();

    console.log('FCM SETUP INIT');
    if (!this.platform.is('cordova')) {
      return;
    }

    console.log('IN CORDOVA');

    this.hasPermission = await this.fcm.requestPushPermission();
    console.log('CHECK hasPermission:', this.hasPermission);

    this.token = await this.fcm.getToken();
    console.log('CHECK getToken: ' + this.token);

    console.log('ON NOTIFICATION SUBSCRIBE');
    this.fcm
      .onTokenRefresh()
      .subscribe((newToken) => console.log('NEW TOKEN:', newToken));
    this.fcm
      .onNotification()
      .subscribe((payload: object) => console.log('ON NOTIFICATION:', payload));
  }

这篇关于由于无法读取未定义的属性"onTokenRefresh",导致无法在离子应用程序中获取设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆