Vib*_*bgy 9 push-notification firebase service-worker web-push firebase-cloud-messaging
我正在使用最近发布的FCM消息传递支持Chrome上的推送通知.当我的应用程序在后台时,我收到通知,但是当我点击通知时没有任何反应.如何指定用户单击通知时应打开的URL?(我理解如何使用notificationclick事件使用纯服务工作者概念完成,我想知道如何使用FCM消息传递.)
messaging.setBackgroundMessageHandler(function(payload) {
var data = payload || {};
var shinyData = decoder.run(data);
console.log('[firebase-messaging-sw.js] Received background message ', shinyData);
return self.registration.showNotification(shinyData.title, {
body: shinyData.body,
icon: shinyData.icon
})
});
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
Mar*_*rco 10
click_action不是showNotification函数的可能参数之一.
要处理对通知的单击,请定义notificationclick事件处理程序.
例如:
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(self.clients.openWindow(YOUR_URL_HERE));
});
Run Code Online (Sandbox Code Playgroud)
马可的答案是正确的。
Firebase消息传递库是Web Push API之上的包装。
该notification: { click_action: 'https://...' }有效载荷将显示一个通知,并办理点击你。要使用数据有效负载实现相同的目的,您应该实现notificationclick事件监听器(如Marco建议)。
self.addEventListener('notificationclick', function(event) {
event.notification.close();
... Do your stuff here.
});
Run Code Online (Sandbox Code Playgroud)
您也可以对notificationclose事件执行相同操作:
self.addEventListener('notificationclose', function(event) {
... Do your stuff here.
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6360 次 |
| 最近记录: |