【发布时间】:2020-06-16 19:08:28
【问题描述】:
我们正在努力使用 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);
}
应该使用 Ionic Native 推送“通知”事件触发的 onNotification 方法永远不会被调用,因此我们无法处理让我们导航到与通知相关的特定页面的额外负载。
我们使用以下版本:
@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())
【问题讨论】:
-
在
pushObject.on('notification')中尝试console.log来检查您是否获得了数据。 -
如您所见,onNotification 方法已经做了一个控制台日志,没有任何成功。
标签: cordova ionic-framework push-notification ionic-native phonegap-plugin-push