I'm using the Firebase plugin to create push notifications, and it works well. The idea is that I receive the notification - when I click on the notification the app opens with a dialog, and when you click on one of the options, you navigate to a page. However, when the app is minimized or closed and I receive a notification, the navigation doesn't works successfully. I'm using Nativescript 6.3.1 with Angular 8
onMessageReceivedCallback: (message: firebase.Message) => {
let meterNumber = message.data.meter;
let peid = message.data.peid;
let entid = message.data.entid;
this.mainService.listMeterNumbers(peid, true).subscribe((res) => {
let options = {
title: "Don't remind me about '" + message.data.meter + "' within -",
message: "",
cancelButtonText: "Cancel",
actions: ["1 Day", "1 Week", "1 Month", "1 Year"]
}
action(options).then((snooze)=> {
this.mainService.setMeterNotificationReminder(entid, snooze).subscribe(() => {
if(snooze !== 'Cancel') {
this.loading.navigateFromNotification = true;
this.loading.navigatedMeterNumber = meterNumber;
this.loading.navigatedMeterNumberParentEntityId = peid;
setTimeout(()=>{ // i've tried several different things here
this.ngZone.run(() => { // and here
this.loading.showNotificationList = false; // to do this - this was at first a .navigate(['newpage'], id)
});
}, 1000)
}
});
});
});
Please any help would be appreciated
There are two main states in which a push notification may be received by your application:
1.) Application is in Foreground.
2.) Application is in Background.
In the background state tho, there is a possibility that:
A. Your app is not in view (hence in the background) but is not closed and still fully running. Eg. you are on a different app.
B. Your app is not in view (still background) but is fully closed and not fully running. Only the Push Notification subscription service is running.
In your case, the reason why your code handling on onMessageReceivedCallback doesn't fully work is because the app is closed (case B) and thus some context of the code here are undefined.
For example, your this.mainService is undefined and this service might only be constructed when you run the application or the application is open.
There are various work around for this, but main thing is that on your onMessageReceivedCallback you should separate your code handling by:
if (message.foreground) {
... do stuff when app is foreground ...
}
else {
... handling if app is closed or in background. this code is executed when the user taps on the notification ...
}
You'll have to route the user to the correct page in onMessageReceivedCallback. Probably in an 'app loaded' event.
You can get more ideas here.