Cordova-plugin-firebase: grantPermission() fails on iOS

Created on 6 Jul 2018  路  2Comments  路  Source: arnesson/cordova-plugin-firebase

This ends up in the catch block, with error being null.

if (this.platform.is('ios')) {
   this.firebase.grantPermission().then(data => {
      console.log(`*** FCM: grantPermission`, data);
   }).catch(error => {
      console.error('*** FCM: Error grantPermission', error);
   });
}

I'm currently using version 1.0.5. Last known version to work is 0.1.25.

Anyone else struggling with this?

Most helpful comment

Hi, this is my example of code, this work with topics on Android and iOS
and work check permissions

_Function for coll after device ready_

 private registrPushFn(): boolean {
    if (!this.platform.is('cordova')) {
      console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
      return true;
    }
    if (this.platform.is('ios')) {
      this.firebase.hasPermission().then(data => {
        if (!data.isEnabled) {
          this.firebase.grantPermission().then(data => {
            return this.getTokenPush();
          })
            .catch(error => {
              console.error('Error getting permissions', error);
              return false;
            });
        } else {
          return this.getTokenPush();
        }
      });
    } else {
      return this.getTokenPush();
    }
  } 

_Function for Get tocken_

 private getTokenPush() {
    this.firebase.getToken()
      .then(token => {
        this.globalVars.userData.userServiceInfo.gmsToken = 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.onTokenRefresh()
      .subscribe((token: string) => {
        this.globalVars.userData.userServiceInfo.gmsToken = token;
      });

    // this.firebase.onNotificationOpen().subscribe(data => { });    

       this.setTopicPush();
    return true;
  } 

_And function for subscribe to topic_ (Attention!!! for android good topic with /topics/blabla_bla-bla but for iOS must be without /topics/ example blabla_bla-bla)

private setTopicPush() {
    let topic = "/topics/MyTopic-User_ENGLISH";
    if (this.platform.is('ios')) {
      topic = "MyTopic-User_ENGLISH"
    }    
    this.firebase.subscribe(topic);
  } 

All 2 comments

Hi, this is my example of code, this work with topics on Android and iOS
and work check permissions

_Function for coll after device ready_

 private registrPushFn(): boolean {
    if (!this.platform.is('cordova')) {
      console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
      return true;
    }
    if (this.platform.is('ios')) {
      this.firebase.hasPermission().then(data => {
        if (!data.isEnabled) {
          this.firebase.grantPermission().then(data => {
            return this.getTokenPush();
          })
            .catch(error => {
              console.error('Error getting permissions', error);
              return false;
            });
        } else {
          return this.getTokenPush();
        }
      });
    } else {
      return this.getTokenPush();
    }
  } 

_Function for Get tocken_

 private getTokenPush() {
    this.firebase.getToken()
      .then(token => {
        this.globalVars.userData.userServiceInfo.gmsToken = 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.onTokenRefresh()
      .subscribe((token: string) => {
        this.globalVars.userData.userServiceInfo.gmsToken = token;
      });

    // this.firebase.onNotificationOpen().subscribe(data => { });    

       this.setTopicPush();
    return true;
  } 

_And function for subscribe to topic_ (Attention!!! for android good topic with /topics/blabla_bla-bla but for iOS must be without /topics/ example blabla_bla-bla)

private setTopicPush() {
    let topic = "/topics/MyTopic-User_ENGLISH";
    if (this.platform.is('ios')) {
      topic = "MyTopic-User_ENGLISH"
    }    
    this.firebase.subscribe(topic);
  } 

closing as resolved per the above comment

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielAccorsi picture DanielAccorsi  路  3Comments

JonSmart picture JonSmart  路  3Comments

rolinger picture rolinger  路  3Comments

rolinger picture rolinger  路  5Comments

jdla1990 picture jdla1990  路  4Comments