Push native - Not all code executing in app.component.ts for Android


#1

I’ve implemented Push in my app, and it works fine in iOS and Android.

I currently have some code which triggers the NavController on receipt of a specific kind of message.

In iOS it works fine. In Android it is not working. Further, I can’t even see any console.log code being executed while on iOS it shows no problem.

The following function is fired on platform.ready():

  initPushNotification() {
    if (!this.platform.is('cordova')) {
      console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
      return;
    }
    const options: PushOptions = {
      android: {
        senderID: 'xxxxxxxx',
        icon: 'icon',
        forceShow: 'true'
      },
      ios: {
        alert: 'true',
        badge: 'true',
        sound: 'true'
      },
      windows: {}
    };
    const pushObject: PushObject = this.push.init(options);


    pushObject.on('registration').subscribe((data: any) => {
      console.log('device token -> ' + data.registrationId);
      //TODO - send device token to server
      //this.updateToken(data.registrationId);
      this.devicetoken = data.registrationId;
      this.storage.set('devicetoken', data.registrationId);
    });//, () => { this.storage.set('devicetoken', this.devicetoken); });


    pushObject.on('notification').subscribe((data: any) => {
      console.log('message -> ' + data.message);

      var msg = data.message;
      var pos_break = msg.indexOf("/");
      var embedded_b_id = "";

      if (pos_break > 0) {
        embedded_b_id = msg.substring(0, pos_break);
        msg = msg.substring(pos_break+1, msg.length);
        console.log("completion - message found");
      }
      //this.ev.publish('notification:booking', data.message);

      //if user using app and push notification comes

      if (data.additionalData.foreground) {
        // if application open, show popup
        // send toast via publish

        let toast = this.toastCtrl.create({
          message: msg,
          duration: 5000,
          //showCloseButton: true,
          //closeButtonText: 'OK',
          position: 'bottom'
        });

        toast.onDidDismiss(() => {
          //console.log('Dismissed toast');
          //this.refresh('', true);
        });
        toast.present();
        //console.log("sub / message received: ");

        if (pos_break > 0 ) {
          console.log('completed booking sent -> ' + embedded_b_id);
          this.nav.push(ReviewsPage, { b_id: embedded_b_id, mode: 'pay' });
          console.log("completion - going to reviews page");
        }
/*
        let confirmAlert = this.alertCtrl.create({
          title: 'Patient has booked an appointment',
          message: data.message,
          buttons: [{
            text: 'OK',
            handler: () => {
              //TODO: Your logic here
              this.nav.push(HomePage, { message: data.message });
            }
          }]
        });
        confirmAlert.present();
*/
      } else {
        //if user NOT using app and push notification comes
        //TODO: Your logic on click of push notification directly
        //this.nav.push(SearchPage, { message: data.message });
        console.log('Push notification clicked');
      }
    });

    pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
  }