SOLUTION
In case anyone will ever stumble upon this, here’s what I ended up with. Just a reminder that I am using @ionic/cloud-angular
Handle a TAP on a notification while the app is CLOSED
I have this inside my app component ngOnInit()
this.push.rx.notification().subscribe(notification => {
if(notification.raw.additionalData.coldstart === true && this.coldstartRedirect === false) {
if(notification.payload && notification.payload["news_article_id"]) {
this.coldstartRedirect = true;
// Redirect to article
}
} else if(this.platform.is("android")) {
// For some reason this event is triggered from a CLICK
// on Android, which only makes it easier to do.. Handle
// the click here.
}
});
Why the this.coldstartRedirect
?
Because I had issues when there were multiple notifications. The one you tapped would trigger this event first, and the other ones would get triggered waaayyyy after, even if not clicked, causing multiple redirects.
Handle a TAP on a notification while the app is MINIMIZED/OPEN
These go under the same thing. Again, in the app component ngOnInit()
, inside platform.ready()
I register for push notifications. In the fulfilled promise I setup LocalNotifications to listen for clicks. I put in here all because of timing, if I put it outside (e.g. after the subscription to the coldstart notifications), this would also be triggered for clicks on notifications on a coldstart.
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then(
(t: PushToken) => {
LocalNotifications.on("click", (notification, state) => {
/*
Handle click, do same as for coldstarts
Note that the notification object has different keys
than the notification object in this push.rx.notification
subscription.
Since Android apparently works on clicks at the
push.rx.notification subscription, this handling
will only be for iOS
*/
});
},
err => {
console.error(err);
}
);
#iOS 10 issues
You need the iOS 10 branch of the LocalNotifications plugin for this to work on iOS 10, I added the following to my config.xml
<plugin name="https://github.com/katzer/cordova-plugin-local-notifications.git#ios10"/>
You might need to delete the plugins folder and remove->add platforms, at least for me it was slightly buggy.
Updated the title from “Show Push notification in the app like Telegram” to “Handling click on notification when app is closed/minimized/open”