cordova - 在 Ionic (ionic native push / phonegap-plugin-push) 上接收通知附加数据
问题描述
我们正在努力使用 Ionic Native 推送插件(基于 phonegap-plugin-push)。虽然我们确实收到了发送的推送通知,但我们无法处理我们发送的特定有效负载,因此当点击通知时,应用程序会在特定页面中打开。
对于 Android 推送通知,我们使用 Firebase Cloud Messaging 来传递通知,对于 iOS,我们使用 APNS。
应用程序打开,但在主页或之前打开的任何页面中。
这是我们的初始化推送代码:
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 和 iOS。
{“优先级”:“高”,“通知”:{“标题”:“标题”,“正文”:“正文”,“声音”:“默认”,“click_action”:“com.adobe.phonegap.push .background.MESSAGING_EVENT" }, "data" : { "more": "data goes here" }, "to" : "id" }
希望这可以帮助。干杯。
推荐阅读
- fortran - 如何在 f2py 中构建 .so 文件而不会出错?
- javascript - GraphQL:覆盖 slug
- gis - netlogo:如何对自己的属性进行分组
- php - Laravel:使用 cookie 仅显示一次模态
- javascript - 无法在reactjs中添加json
- php - 如何制作动态行跨度php codeigniter?
- javascript - 在 vue 中从 v-for 更改特定 v-model 的数据
- doxygen - 如何使用 Automake 有条件地将文档安装到 pkghtmldir
- installation - 在 HP DL20 Gen9 上安装 debian 9
- node.js - 从 mongodb 查询所有元素的函数不填充数组