Hi all,
can you please tell me how is possible to redirect to specific page after clicking on notification if app is in background or if cold start of app? I am using push plugin.
My code in app.component.ts in platform.ready function:
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification)
if (notification.additionalData.foreground) {
// if application open, show popup
let notifAlert = this.alertCtrl.create({
title: notification.title,
message: notification.message,
cssClass: "notification",
buttons: [{
text: 'View',
role: 'cancel',
handler: () => {
//TODO: Your logic here
console.log("click alert")
if (notification.additionalData.key_code == "new_message") {
this.nav.push(MessagesPublicPage);
}
}
}]
});
notifAlert.present();
setTimeout(function () {
notifAlert.dismiss()
}, 5000);
} else {
//if user NOT using app and push notification comes - NOT working, redirect to default rootPage
/if (notification.additionalData.key_code == "new_message") {
this.rootPage = MessagesPublicPage;
}
console.log("Push notification clicked");
}
});
This is my function (which I call from the constructor, not from platform.ready like you do)
pushNotificationsHandler() {
// initialise Push Notifications
const pushObject: PushObject = this.pushService.initialise();
// if a notification arrives and the app is
// not running in the foreground, handle it
if (pushObject !== undefined) {
pushObject.on("notification").subscribe((data: any) => {
if (!data.additionalData.foreground) {
this.pushData = data;
this.openPushItem();
} else {
this.pushData = null;
}
});
}
}
If the app is running, I can see in console log message ON NOTIFICATION RESP. But when the app is closed and notification is received, no console log on receive.
So I guess the app ignore it :-/. Also app has only blank white screen, because no root page is defined before.
I am also calling push initialization from constructor. But I have to place it in platform ready. If it is not in, the app tells me that push notification can`t be initialized due to not installed plugin :-/
Dude it works for me for all platforms. Just put it above all other construct calls in your constructor! And make sure the push plugin ist the first one that initialized!
I initially had my listeners set up after the platform.ready() and experienced the same behavior that mcihak describes: the app would launch from the background and for an instant load the page I wanted. But then it would re-navigate to the default page.
Once I moved the listeners to the constructor, to be run before the platform.ready() function, I was able to setRoot to my preferred page. Which prevented the default root from being set.