【问题标题】:Ionic 3 - firebase plugin - not able to get phone's tokenIonic 3 - firebase 插件 - 无法获取手机的令牌
【发布时间】:2019-08-11 04:14:19
【问题描述】:

当我调用 firebase.getToken() 时,它返回 undefined 或 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

【问题讨论】:

    标签: angular firebase cordova ionic3


    【解决方案1】:

    我得到了答案。

    我没有从

    获得令牌
    this.fcm.getToken()
    

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

    this.fcm.onTokenRefresh();
    

    在原生文档 -v3 中

    获取设备令牌

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 嘿,谢谢你的回答我已经在下面提到了答案,我想知道为什么 this.firebase.getToken() 不起作用。我认为它是 ionic 的错误,因为我检查了 firebase ,令牌已经存在。
      猜你喜欢
      • 2019-01-12
      • 2017-12-06
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      相关资源
      最近更新 更多