OneSignal / OneSignal-Cordova-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Ionic, PhoneGap CLI, PhoneGap Build, Cordova, or Sencha Touch app with OneSignal. Supports Android, iOS, and Amazon's Fire OS platforms. https://onesignal.com
Other
251 stars 198 forks source link

Ionic 3 - getTags() return empty value or never execute #307

Closed oTTa closed 6 years ago

oTTa commented 6 years ago

I using Ionic 3 and I can add and delete tags and receive notifications but i can't get the tags because the value is empty or the function never execute, The code is the following:

app.component.ts //init app

import { OneSignal } from '@ionic-native/onesignal';

constructor(private oneSignal: OneSignal...........){
.//code
.//code
.
platform.ready().then(() => {
.//code
this.handlerNotifications();
.//code
}
.//code
.//more code
.//mode code
}
private  handlerNotifications() {
    this.oneSignal.startInit(Configuration.ONESIGNAL_ID, Configuration.FIREBASE_CONFIG.messagingSenderId);
    this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
    this.oneSignal.handleNotificationReceived().subscribe(() => {
       // do something when notification is received
    });
    this.oneSignal.handleNotificationOpened()
    .subscribe(jsonData => {
      let alert = this.alertCtrl.create({
        title: jsonData.notification.payload.title,
        subTitle: jsonData.notification.payload.body,
        buttons: ['OK']
      });
      alert.present();
      console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
    });
    this.oneSignal.endInit();
    this.oneSignal.sendTag('email',this.user.email); //testing code
    this.oneSignal.sendTag('notificaciones','1'); //testing code
  }

configuracion.ts //here use getTags()

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { OneSignal } from '@ionic-native/onesignal';
import { SeccionService } from '../../services/seccion.service';
import { ToastService } from '../../services/toast.service';
import { Seccion } from '../../models/seccion';
import { Platform } from 'ionic-angular';
@IonicPage()
@Component({
  selector: 'page-configuracion',
  templateUrl: 'configuracion.html',
})
export class ConfiguracionPage {
  public secciones: Array<Seccion>;

  constructor(public navCtrl: NavController, public navParams: NavParams, private oneSignal: OneSignal,
              public seccionService: SeccionService, public toast: ToastService, private platform: Platform) {
   this.obtener_secciones_principales();
  }

  private obtener_secciones_principales() {
    this.seccionService.get_principales().subscribe(
      (data) => {
        this.secciones=data;
        if(this.platform.is('cordova')) {
        this.oneSignal.getTags()
             .then((tags) => {console.log("TAGS: ",JSON.stringify(tags));})
             .catch((error) => {
               console.log("TAGSerror: ",JSON.stringify(error))
             });
        }
      },
      (error) => { this.toast.error('Error al cargar las secciones'); }
    );
  }

}

OneSignal dashboard show me sin titulo

Android Device Motitor sin titulo

Nightsd01 commented 6 years ago

@oTTa We have not implemented promises with the Cordova SDK. Have you tried using the callback instead of a promise chain?

this.oneSignal.getTags((tags) => {
   // use the tags result
});
oTTa commented 6 years ago

@Nightsd01, The method was working was the Android Device Monitor that did not show the result. If I showed the tags in a layout I could see them, thanks anyway.