Ionic cannot navigate to a specific tab upon notification clicked? - ionic-framework

I am using Ionic and FCM(firebase) plugin to send push notification. I can receive notifications, when I tap the notification it triggers the alert successfully but it is not navigating to the specified tab. Just to make sure this line:
this.tab.select(2);
works I have tested navigating with that code with a button and it works
FCMPlugin.onNotification(function(data){
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('data was tapped');
this.tab.select(2);
//this.navCtrl.push(ViewurgentnewsPage); also tried this
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert('this was received on foreground');
}
});

You should use arrow functions
FCMPlugin.onNotification((data) => {
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('data was tapped');
this.tab.select(2);
} else {
//Notification was received in foreground. Maybe the user needs to be notified.
alert('this was received on foreground');
}
});
By using arrow function, the this keyword still references the component code, where the this.tab property exists.

Related

Detecting flutter app opened from a terminated state with FCM and flutter_local_notifications

I am working on a flutter app that uses FCM data messages sent through cloud functions. In my app I'm receiving that data messages and then displaying a local notification. I need to be able to tap the local notification and have it direct the user to the correct screen. My current solution works when the app is in the foreground, or background, but NOT terminated. When it's terminated tapping on the notification appears to just open the app. Is there a way to detect if the app just opened from a terminated state? Below is my current code for when the notification is tapped, and the comments show what I want it to do
Future onSelectNotification(String payload) async {
Map data = json.decode(payload);
print('onSelectNotification: $data');
// if app is in foreground or background do this
if (data.containsKey('conversation_with')) {
print('Notification was for a conversation');
String conversationID = data['conversation_with'];
print('Conversation with UID: $conversationID');
PublicAppUser conversationWith =
await FriendDatabase().getPublicAppUser(conversationID);
navigatorKey.currentState!.pushNamed(
ConversationPublicUserScreen.routeName,
arguments: conversationWith);
}
// if it just opened from a terminated state do this
// ...
}

Ionic 4 FCM.onNotification not called on iOS if app is killed

i am struggling with an issue for weeks now with FCM push notifications on iOS. The problem is that if the app is in killed state when the notification is clicked, the fcm.onNotification callback is not fired.
Checked cases :
iOS - App in foreground - notification popup not shown, correct code running : OK
iOS - App running in background - notification popup shown - notification clicked - correct code running : OK
iOS - App killed by OS / manually swiped away - notification popup shown - notification clicked - app opens - platform.ready runs correctly - onNotification not fired.
On android all is working fine.
I have tried setting everything in the notification i could google up :
force-start : "1"
content_available : true
priority : "high"
click_action : "FCM_PLUGIN_ACTIVITY"
Code which should run :
this.platform.ready().then(() => {
alert("This alert is shown");
this.fcm.onNotification().subscribe(data => {
alert("This alert is not shown");
if (data.wasTapped) {
alert('Received in background');
self.router.navigate([data.landing_page, data.content]);
} else {
alert('Received in foreground');
self.router.navigate([data.landing_page, data.content]);
}
});
});
Thanks!
I made iOS background notifications work with content-available: 1 set in the aps payload and testing in the service whether the additionalData.foreground value is false.

ionic native diagnostic - how to know when the user has come back to the app

In my Ionic application I ask the user to enable location through this code:
let alert = this.alertCtrl.create({
title: 'Posizione',
message: 'Abilita il GPS per localizzare la tua posizione.',
buttons: [
{
text: 'No',
role: 'cancel',
handler: () => {
}
},
{
text: 'Sì',
handler: () => {
this.diagnostic.switchToLocationSettings();
}
}
]
});
alert.present();
I'm looking for a way to know when the user has exited location settings and went back to the app, so that I can call the method geolocate, that otherwise is called only when the user opens the page. So at the moment he would have to go to another page and then back to the one with geolocation to locate himself. I don't know, does a kind of a callback exist? Or is there another way to call immediately geolocate after closing the settings?
You can use Platform.resume of ionic 2 as explained here.
The resume event emits when the native platform pulls the application
out from the background. This event would emit when a Cordova app
comes out from the background, however, it would not fire on a
standard web browser.

display push notification in pretty way in ionic 3

I implemented push notification FCM in my ionic 3 app and I want to display it in pretty way.
for now, I just show alert when get any notification.
I'm looking for another way to display it
ionViewDidLoad() {
FCMPlugin.onNotification(function (data) {
if (data.wasTapped) {
//Notification was received on device tray and tapped by the user.
alert(data.message);
} else {
//Notification was received in foreground. Maybe the user needs to be notified.
alert(data.message);
}
});
FCMPlugin.onTokenRefresh(function (token) {
alert(token);
});
}
You can use local notifications for this. Local notifications are device native and hence will have, as the name suggests, native look and feel.
Refer to https://ionicframework.com/docs/native/local-notifications/ for implementation details.

How to wake up an app on push notification when an app is in background

Is there any way to launch an app when push notification is received from background?
I want to bring up a modal page (alert page with some info) when push notification is received.
I have tried to use the plugin https://github.com/katzer/cordova-plugin-background-mode, but no luck. (wakeUp and unlock function do not work)