收到通知Ionic上的附加数据(Ionic原生推送/phonegap插件推送)



我们正在与Ionic Native推送插件(基于phonegap插件推送(作斗争。虽然我们确实收到了发送的推送通知,但我们无法处理我们发送的特定负载,因此当点击通知时,应用程序会在特定页面中打开。

对于Android推送通知,我们使用Firebase云消息传递通知,对于iOS,我们使用APNS。

该应用程序会打开,但要么在主页中,要么在以前打开的任何页面中。

这是我们的init推送代码:

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);
}

onNotification方法应该通过Ionic Native推送"通知"事件触发,但它从未被调用,因此我们无法处理让我们导航到与通知相关的特定页面的额外负载。

我们使用以下版本:

@ionic/core: 4.11.1
@ionic-native/push: 5.15.0
phonegap-plugin-push: 2.3.0
@angular/core: 8.1.2

我们知道这个插件已经停产,我们可能应该切换到OneSignal,但我们正在努力避免这种情况,除非这是我们的最后手段,因为这需要额外的开发。

这是Kotlin代码片段,我们在其中创建带有有效载荷的通知,如果它有帮助的话:

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())

不确定这是否适用于您,但在我以前的应用程序中,我不得不为android和iOS设置不同的结构。

安卓:

registration_ids : [],
data: {
notId: "", // notId on Android needed to be an int and not a string
title: "",
body: "",
soundname: "default",
}

iOS:

registration_ids : [],
notification:{
title: "",
body: "",
sound: "default",
},
data: {
title: "",
body: "",
}

或。。

你可以通过这个Android通知数据通行证,下面的有效载荷应该对Android和Android都有效;iOS。

{"优先级":"高","通知":{"title":"title","身体":"身体","声音":"默认","click_action":"com.adobe.phonegap.push.background.MESSAGING_EVENT"},"数据":{"更多":"此处显示数据"},"收件人":"id"}

希望这能有所帮助。干杯

所以我最终设法修复了它……必须在数据有效负载中设置一个content-available: "1",在推送请求主体中设置contentAvailable: true,以及设置noCache: "1"

放弃了Firebase SDK并手动执行请求,现在它终于调用了on('notification')

如果additionalData.foreground为true,您还需要在通知回调中进行处理,因为在前台,通知一显示就会触发通知事件,并在点击时在前台为false的情况下再次触发。

以下是我发送给FCM API的有效载荷样本:

{
"to":"FIREBASE_TOKEN",
"content_available": true,
"data": {
"action": "ACTION(Custom payload data)",
"referenced": "REFERENCED(Custom payload data)",
"force-start": "1",
"content-available": "1",
"no-cache": "0",
"notId": "4361c4f5-ae8d-42fa-a863-a04acff2ab7a",
"title": "TEST"
}
"priority":10
}

最新更新