Push Notifications in Ionic
Ankush Aggarwal
62168

Thank you very much for this tutorial.I did everything in sharing.Working except for a minor problem.It works if the user is using the app and the push notification comes up.Does not use user application and does not work if push warning.Notification comes up, but there is no page switch.Where am I making the mistake

import { Component,ViewChild } from ‘@angular/core’;

import { Platform,NavController } from ‘ionic-angular’;

import { StatusBar } from ‘@ionic-native/status-bar’;

import { SplashScreen } from ‘@ionic-native/splash-screen’;

import { Push, PushObject, PushOptions } from ‘@ionic-native/push’;

import { HomePage } from ‘../pages/home/home’;

import {PushPage} from ‘../pages/push/push’;

@Component({

template: ‘<ion-nav #myNav [root]=”rootPage”></ion-nav>’,

providers : [Push]

})

export class MyApp {

@ViewChild(‘myNav’) nav: NavController;

rootPage:any = HomePage;

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private push: Push) {

platform.ready().then(() => {

// Okay, so the platform is ready and our plugins are available.

// Here you can do any higher level native things you might need.

statusBar.styleDefault();

splashScreen.hide();

});

const options: PushOptions = {

android: {

senderID: ‘xxxxxxxxx’

},

ios: {

alert: ‘true’,

badge: true,

sound: ‘false’

},

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

});

pushObject.on(‘notification’).subscribe((data: any) => {

//if user using app and push notification comes

if (data.additionalData.foreground) { // foreground

// if application open, show popup

alert(“message:” + data.message);

this.nav.push(PushPage,data.message);

} else {

//if user NOT using app and push notification comes

//TODO: Your logic on click of push notification directly

// alert(“message:” + data.message);

this.nav.push(PushPage,data.message);

}

});

}

}