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

时间:2019-03-20 12:55:00

标签: angular firebase cordova ionic3

当我调用firebase.getToken()时,它将返回undefined或null

system.webServer

已安装平台:

import { Firebase } from '@ionic-native/firebase';

constructor(private firebase: Firebase) { }

...

this.firebase.getToken()
  .then(token => console.log(`The token is ${token}`)) // save the token server-side and use it to push notifications to this device
  .catch(error => console.error('Error getting token', error));

this.firebase.onNotificationOpen()
   .subscribe(data => console.log(`User opened a notification ${data}`));

this.firebase.onTokenRefresh()
  .subscribe((token: string) => console.log(`Got a new token ${token}`));

可用平台:

 android 8.0.0

2 个答案:

答案 0 :(得分:3)

我得到了答案。

我没有从中获取令牌

this.fcm.getToken()

但是我使用以下代码获得了令牌刷新的代码:

this.fcm.onTokenRefresh();

在本机文档-v3中

获取设备令牌

返回:承诺请注意,如果尚未建立令牌,它将为null

请参考以下链接
https://ionicframework.com/docs/v3/native/firebase/

实际上,getToken()根本不返回任何响应。

答案 1 :(得分:0)

我也面临着同样的问题。这是因为平台尚未准备好,所以这样做了:

import { Firebase } from '@ionic-native/firebase';
import { Platform } from 'ionic-angular'; 

constructor(private firebase: Firebase, private platform: Platform) {
   this.platform.ready().then((readySource) => {
   
      this.firebase.getToken()
        .then(token => console.log(`The token is ${token}`))
        .catch(error => console.error('Error getting token', error));

      this.firebase.onNotificationOpen()
        .subscribe(data => console.log(`User opened a notification ${data}`));

      this.firebase.onTokenRefresh()
        .subscribe((token: string) => console.log(`Got a new token ${token}`));  
        
   });
}