Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using this plugin: https://github.com/arnesson/cordova-plugin-firebase I have a button on the home.html that calls a test() function in home.ts

public test(): void {
    // wrap in the platform.ready && platform.is('cordova')
    this.fcm.getToken()
      .then((token: any) => {
        console.log("token: ", token);
      });
    this.fcm.onTokenRefresh()
      .subscribe((token: any) => {
        console.log("token onTokenRefresh: ", token);
      });
  }

By the time I am at the home screen, everything should be initialized and I should be able to get the token from getToken() function. But I am getting token: undefined in my console.log. I am not getting any errors, the log is clean, i.e. no errors are being generated regarding this particular plugin.


Update 1:

import { Firebase } from '@ionic-native/firebase';
  constructor(public navCtrl: NavController
    , public fcm: Firebase) {

Update 2: Tried like this: cordova plugin add https://github.com/arnesson/cordova-plugin-firebase --variable ANDROID_VERSION=7.1.0 --save But its still null.

private void getToken(final CallbackContext callbackContext) {
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            try {
                String token = FirebaseInstanceId.getInstance().getToken();
                System.out.println("token: " + token); // null....
                callbackContext.success(token);
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }
        }
    });
}

Above method isn't getting the token. So it seems this is not a cordova issue?

like image 250
Noman Avatar asked Aug 05 '18 03:08

Noman


1 Answers

My google-services.json was wrong. I think I had created a new project but I must have not replaced the google-services.json with the new/current project's.

like image 132
Noman Avatar answered Nov 15 '22 23:11

Noman