Ionic 2-Firebase插件-无法获取手机令牌

时间:2018-08-05 03:39:43

标签: angular firebase ionic2

我正在使用此插件:https://github.com/arnesson/cordova-plugin-firebase我在home.html上有一个按钮,可在home.ts中调用test()函数

public test(): void {
    // wrap in the platform.ready && platform.is('cordova')
    this.fcm.getToken()
      .then((token: any) => {
        console.log("token: ", token);
      });
    this.fcm.onTokenRefresh()
      .subscribe((token: any) => {
        console.log("token onTokenRefresh: ", token);
      });
  }

到我进入主屏幕时,所有内容都应该初始化,并且我应该能够从getToken()函数获取令牌。但是我得到令牌:在console.log中未定义。我没有收到任何错误,日志很干净,即与此特定插件无关的错误。


更新1:

import { Firebase } from '@ionic-native/firebase';
  constructor(public navCtrl: NavController
    , public fcm: Firebase) {

更新2: 尝试这样: cordova插件添加https://github.com/arnesson/cordova-plugin-firebase --variable ANDROID_VERSION = 7.1.0 --save 但它仍然为空。

private void getToken(final CallbackContext callbackContext) {
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            try {
                String token = FirebaseInstanceId.getInstance().getToken();
                System.out.println("token: " + token); // null....
                callbackContext.success(token);
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }
        }
    });
}

以上方法未获取令牌。看来这不是科尔多瓦问题?

1 个答案:

答案 0 :(得分:4)

我的google-services.json错误。我想我已经创建了一个新项目,但是我一定没有用新的/当前的项目替换google-services.json。