Ionic firebase token alway null and no token refre

2019-04-12 21:04发布

问题:

i'm in trouble with ionic firebase module (https://ionicframework.com/docs/native/firebase/) during the last 2 days.

I don't understand why my firebase token is alway NULL in the following code section in app.component.ts :

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

If i had grantPermission() at the beggining of the ready block my token become an empty string

I ran the following command to install my plugin :

$ ionic plugin rm cordova-plugin-firebase

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

Here is the list of my plugins :

$ 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"

And here is my project architecture :

$ 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

EDIT: Iadded the line alert(JSON.stringify(this.firebase)) which give me the following string {}

Thanks for reading :)

EDIT :

I found out that it was a recent but in firebase API. I had to update my package with git repo's HEAD

回答1:

try is code in 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)); 
  });
 }


回答2:

The cordova-pugin-firebase is broken in cordova ver:7.0.0

Use any of the following methods for fixing it.

https://github.com/arnesson/cordova-plugin-firebase/pull/570

Installation command for cordova android version 7 and above:

cordova plugin add cordova-plugin-firebase --variable ANDROID_VERSION=7x --save

Installation command for cordova android version 6 and below:

cordova plugin add cordova-plugin-firebase --variable ANDROID_VERSION=6x --save

or

https://github.com/arnesson/cordova-plugin-firebase/issues/558

Update the stringsXml in the following files:

plugins/cordova-plugin-firebase/scripts/after_prepare.js line 51 node_modules/cordova-plugin-firebase/scripts/after_prepare.js line 51

-       stringsXml: ANDROID_DIR + '/res/values/strings.xml' 
+       stringsXml: fileExists(ANDROID_DIR + '/app/src/main/res/values/strings.xml') ? ANDROID_DIR + '/app/src/main/res/values/strings.xml' : ANDROID_DIR + '/res/values/strings.xml'