We are struggling with Ionic Native push plugin (which is based on phonegap-plugin-push). While we do receive push notifications sent, we cannot process the specific payload that we send so that when the notification is tapped, the app opens in a specific page.
For Android push notifications we use Firebase Cloud Messaging to deliver the notifications, for iOS we are using APNS.
The app opens, but either in the home page or whatever page was open before.
Here is our init push code:
private initPush() {
this.push.hasPermission()
.then((res: any) => {
// just some console logs irrelevant to this question
});
const options: PushOptions = {
android: { clearBadge: true, forceShow: true },
ios: { alert: 'true', badge: true, sound: 'false', clearBadge: true },
windows: {}
};
const pushObject: PushObject = this.push.init(options);
try {
pushObject.on('notification').subscribe((notification: any) => this.onNotification(notification));
pushObject.on('registration').subscribe((registration: any) => this.onRegister(registration));
pushObject.on('error').subscribe(error => this.onError(error));
this.authorizationService.currentUser.subscribe((nextUser) => this.fullfillPushTokenRegistration());
} catch (e) {
console.error('Error on registering push methods', e);
}
}
onNotification(notification): void {
console.info('On Push Notification', JSON.stringify(notification));
const addData = notification.additionalData;
this.notificationEventsService.createEvent('push', 'click', addData);
}
The onNotification method, which should be fired with the Ionic Native push "notification" event is never called, hence we can't process the additional payload that lets us navigate to the specific page related to the notification.
We're using the following versions:
@ionic/core: 4.11.1
@ionic-native/push: 5.15.0
phonegap-plugin-push: 2.3.0
@angular/core: 8.1.2
We are aware that this plugin is discontinued and that we should probably switch to OneSignal, but we're trying to avoid this unless it's our last resort, since it would require an additional development.
This is the Kotlin code fragment where we create the notification with the payload, if it helps:
val message = Message.builder().setToken(device.deviceToken)
val fcmNotification: com.google.firebase.messaging.Notification = com.google.firebase.messaging.Notification(
notification.title, notification.message
)
message.setNotification(fcmNotification)
message.putData("action", notification.action!!.toString())
message.putData("pendingToViewUserNotifications", pendingToViewUserNotifications.toString())
message.putData("referenced", notification.referenced)
message.putData("notificationId", notification.id.toString())
message.putData("title", notification.title)
message.putData("body", notification.message)
message.putData("badge", pendingToViewUserNotifications.toString())
message.putData("content-available", "1")
when (device.deviceOs!!.toLowerCase()) {
"android" -> message.setAndroidConfig(AndroidConfig.builder()
.setTtl(3600 * 1000)
.setNotification(AndroidNotification.builder()
.setIcon("stock_ticker_update")
.setColor("#f45342")
.build())
.build())
console.loginsidepushObject.on('notification')to check are you getting data on not. - Najam Us Saqib