【发布时间】:2017-10-14 16:05:13
【问题描述】:
我正在实施Webpush ruby gem 向我网站的用户发送推送通知。
服务器代码:
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/logo@2x.png",
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 gem 传递自定义数据?
【问题讨论】:
-
我不是这方面的专家,但我会这样做:消息:服务器上的 JSON.stringify(notification),客户端上的 JSON.parse...
-
@Jonasw - 我最终选择了你的解决方案。如果您将其写为答案,我会接受。感谢您的帮助。
标签: javascript notifications web-push