View not updating after receiving Push


#1

When I receive a notification I setRoot() to a page. That works, and the Typescript is called for the buttons on that page, but the view is not being updated. It doesn’t matter if the app is the foreground or background. I created a small 1-page app that demonstrates it. Below are the relevant parts. I’ve tried things in comments below, with no changes. Am I doing something wrong, or is this a bug?

cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.19.2
ionic (Ionic CLI) : 3.20.0

global packages:

cordova (Cordova CLI) : 8.0.0 

local packages:

@ionic/app-scripts : 3.1.11
Cordova Platforms  : ios 4.5.4
Ionic Framework    : ionic-angular 3.9.2

System:

ios-deploy : 1.9.2 
Node       : v8.4.0
npm        : 5.4.1 
OS         : macOS High Sierra
Xcode      : Xcode 9.4 Build version 9F1027a 

cordova plugin ls
cordova-plugin-device 2.0.2 “Device”
cordova-plugin-ionic-keyboard 2.0.5 “cordova-plugin-ionic-keyboard”
cordova-plugin-ionic-webview 1.1.19 “cordova-plugin-ionic-webview”
cordova-plugin-splashscreen 5.0.2 “Splashscreen”
cordova-plugin-whitelist 1.3.3 “Whitelist”
phonegap-plugin-push 2.2.3 “PushPlugin”

Home.html


Toggle the heart:
<ion-icon class=“icon-heartset” icon-only text-left
name="{{(favorite == true) ? ‘heart’:‘heart-outline’}}" (click)=“toggleFavorite()” >

Home.ts

import {ChangeDetectorRef, Component, OnInit} from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage implements OnInit{
favorite: boolean =false;

constructor(public navCtrl: NavController, private cd: ChangeDetectorRef) {

}

ngOnInit () {
this.cd.detectChanges(); //Tried it with and with out this.
}

toggleFavorite () {
    this.favorite = !this.favorite;
    console.log ( "toggleFavorite: = ", this.favorite)
}

}

Then in app.componet.ts

initPushNotification()  {
    if (!this.platform.is('cordova')) {
        console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
        return;
    }
    const options: PushOptions = {

        ios: {
            alert: 'true',
            badge: 'true',
            sound: 'true',
            clearBadge: 'true'
        }
    };
    this.pushObject = this.push.init(options);

    this.pushObject.on('registration').subscribe((data: any) => {
        console.log('pushObject-device token -> ' + data.registrationId,JSON.stringify(data));
    });

    this.pushObject.on('notification').subscribe((data: any) => {

        // if (data.additionalData.foreground) {

        //Tried with and without finish()
       /* this.pushObject.finish().then ( foo => {
           }).catch( err => {
           });
        */

        setTimeout(() => { // Tried with and without timeout
             this.nav.setRoot(HomePage);
            //this.appCtrl.getRootNav().setRoot(HomePage);
        }, 200);
    });

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

#2

I can force the view to update after receiving a notification by calling ChangeDetectorRef.detectChanges(); after each instance where the view is updated. But this 1) seems is should be unnecessary, and 2) seem to slow things down. But so far that is the only thing that seem to work after a notification if the app is open.