我正在实施Webpush ruby gem向我的网站的用户发送推送通知。点击网页推送通知时打开自定义网址
Server代码:
Webpush.payload_send({
message: notification.message,
url: notification.url, # I can't figure out how to access this key
id: notification.id, # or this key from the service worker
endpoint: endpoint,
p256dh: p256dh_key,
vapid: vapid_keys,
ttl: 24 * 60 * 60,
auth: auth_key,
})
我有一个服务人员建立在客户端上显示通知,并使其点击。
self.addEventListener("push", function (event) {
var title = (event.data && event.data.text()) || "New Message";
event.waitUntil(
self.registration.showNotification(title, {
body: "New push notification",
icon: "/images/[email protected]",
tag: "push-notification-tag",
data: {
url: event.data.url, // This is returning null
id: event.data.id // And this is returning null
}
})
)
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(
clients.openWindow(event.data.url + "?notification_id=" + event.data.id)
);
})
这是所有工作正常,但我通过不能从服务人员中访问自定义键(url
,id
)。
有谁知道如何通过WebPush宝石传递自定义数据?
即时通讯不是专家,但我会这样做:message:JSON.stringify(notification)on服务器和客户端上的JSON.parse ... –
@Jonasw - 我结束了你的解决方案。如果你把它写成答案,我会接受它。谢谢您的帮助。 – BananaNeil