首页 文章

Ionic 2 - firebase插件 - 无法获得手机令牌

提问于
浏览
12

我正在使用这个插件: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中未定义 . 我没有收到任何错误,日志很干净,即没有关于这个特定插件的错误 .


Update 1:

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

Update 2: 试过这样:cordova插件添加https://github.com/arnesson/cordova-plugin-firebase - 变量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 回答

  • 4

    我的google-services.json错了 . 我想我已经创建了一个新项目,但我必须没有用新的/当前项目取代google-services.json .

相关问题