Ionic 3 - firebase 插件 - 无法获取 phone 的令牌

Ionic 3 - firebase plugin - not able to get phone's token

当我调用 firebase.getToken() 时,它返回未定义或 null

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

可用平台:

  browser ~5.0.1
  ios ~4.5.4
  osx ~4.0.1
  windows ~6.0.0

我得到了答案。

我没有从

获得令牌
this.fcm.getToken()

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

this.fcm.onTokenRefresh();

在原生文档中 -v3

获取设备令牌

Returns: Promise 请注意,如果还没有建立token,则为null

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

实际上 getToken() 根本没有返回任何响应。

我也遇到了同样的问题。这是因为平台还没有准备好,所以才这样做:

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}`));  
        
   });
}