android - Ionic firebase token 始终为空且无 token 刷新

标签 android cordova firebase ionic-framework

我在过去 2 天遇到了 ionic firebase 模块 ( https://ionicframework.com/docs/native/firebase/) 的问题。

我不明白为什么我的 firebase token 在 app.component.ts 的以下代码部分中始终为 NULL:

this.platform.ready().then(() => {
  this.firebase.getToken().then(firebaseTokenSuccess).catch(firebaseTokenFailure);

  this.firebase.onTokenRefresh().subscribe(firebaseTokenRefresh);

  function firebaseTokenRefresh(tkn) {
    return alert("New token received: " + JSON.stringify(tkn));
  }

  function firebaseTokenSuccess(tkn) {
    return alert("Token access success: " + JSON.stringify(tkn));
  }

  function firebaseTokenFailure(e) {
    return alert("Token access failure: " + e.toString());
  }
});

如果我在就绪 block 的开头有 grantPermission(),我的 token 将变为空字符串

我运行了以下命令来安装我的插件:

$ ionic plugin rm cordova-plugin-firebase

$ npm install @ionic-native/push --save

这是我的插件列表:

$ ionic plugins                        
com.googlemaps.ios 2.4.0 "Google Maps SDK for iOS"
cordova-android-play-services-gradle-release 1.1.1 "cordova-android-play-services-gradle-release"
cordova-plugin-compat 1.2.0 "Compat"
cordova-plugin-console 1.1.0 "Console"
cordova-plugin-device 1.1.6 "Device"
cordova-plugin-facebook4 1.9.1 "Facebook Connect"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-file-transfer 1.6.3 "File Transfer"
cordova-plugin-firebase 0.1.24 "Google Firebase Plugin"
cordova-plugin-googlemaps 1.4.5 "cordova-googlemaps-plugin"
cordova-plugin-inappbrowser 1.7.1 "InAppBrowser"
cordova-plugin-splashscreen 4.0.3 "Splashscreen"
cordova-plugin-statusbar 2.2.3 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"
ionic-plugin-keyboard 2.2.1 "Keyboard"

这是我的项目架构:

$ tree -L 1  
.
├── appli_dev.keystore
├── bower_components
├── build.gradle
├── config.xml
├── GoogleService-Info.plist
├── google-services.json
├── hooks
├── init.sh
├── ionic.config.json
├── node_modules
├── package.json
├── package-lock.json
├── platforms
├── plugins
├── README.md
├── resources
├── src
├── tsconfig.json
├── tslint.json
└── www

编辑:我添加了行 alert(JSON.stringify(this.firebase)) 这给了我以下字符串 {}

感谢阅读:)

编辑:

我发现它是最近的但在 firebase API 中。我不得不用 git repo 的 HEAD 更新我的包

最佳答案

try 是 ionic 2 中的代码

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

 constructor(public platform: Platform,private cordovaFirebase: Firebase)   {
  platform.ready().then(() => {
    StatusBar.styleDefault();
    Splashscreen.hide();
    this.cordovaFirebase.grantPermission();
    this.cordovaFirebase.getToken().then(token => {
       console.log("token....",token)
    }).catch(error => console.log(error)); 
  });
 }

关于android - Ionic firebase token 始终为空且无 token 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583560/

相关文章:

android - enum vs android @Intdef - 哪个优化得更好

android - CountDownTimer 如何在 OnTick 方法中访问 UI?

android - 我如何修复 "A problem occurred evaluating project ' :CordovaLib' ."when running ' cordova run android'

javascript - 错误: ENOENT: no such file or directory even when file exists in Firebase Cloud Functions

android - 不活动,与 AppMeasurementService 断开连接

android - 如何绘制自定义背景

android - GridView 不在 android 中显示内容

javascript - 使用 Jquery Mobile 进行 XML 解析

javascript - 是否可以通过 WebView.addJavascriptInterface 从 JS 端接收对象?

javascript - Reactjs Firebase Auth 电话注册流程?