我正在实施Webpush ruby gem向我网站的用户发送推送通知。
服务器代码:
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
,where title >= '0' and title < ':'
)无法从服务工作者中访问。
有谁知道如何通过WebPush gem传递自定义数据?
答案 0 :(得分:10)
自定义数据位于 event.notification 对象中,不在事件中直接(在 notificationclick 中)。因此,如果您想在notificationclick函数中获取自定义数据变量,那么您应该这样做:)
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(
clients.openWindow(event.notification.data.url + "?notification_id=" + event.notification.data.id)
);
})
答案 1 :(得分:8)
从Webpush (with a payload) documentation开始,您似乎应该使用JSON.stringify()
方法将所有数据放入邮件中,然后使用JSON.parse()
在服务工作者中检索它。
服务器:
Webpush.payload_send({
message:JSON.stringify({
message: notification.message,
url: notification.url,
id: notification.id,
}),
endpoint: endpoint,
p256dh: p256dh_key,
vapid: vapid_keys,
ttl: 24 * 60 * 60,
auth: auth_key,
})
客户端:
event.waitUntil(
self.registration.showNotification(title, {
body: "New push notification",
icon: "/images/logo@2x.png",
tag: "push-notification-tag",
data: {
url: JSON.parse(event.message).url
}
})
答案 2 :(得分:0)
将此添加到您的Angular项目中的node_modules/@angular/service-worker/ngsw-worker.js
this.scope.addEventListener('notificationclick', (event) => {
console.log('[Service Worker] Notification click Received. event:%s', event);
event.notification.close();
if (clients.openWindow && event.notification.data.url) {
event.waitUntil(clients.openWindow(event.notification.data.url));
}
});
您可以输入上面的代码,在文件中的此行
this.scope.addEventListener('notificationclick', (event) => ..
您必须再次构建dist才能使其正常工作。在后端,您将需要使用以下格式:
{"notification":{"body":"This is a message.","title":"PUSH MESSAGE","vibrate":[300,100,400,100,400,100,400],"icon":"https://upload.wikimedia.org/wikipedia/en/thumb/3/34/AlthepalHappyface.svg/256px-AlthepalHappyface.svg.png","tag":"push demo","requireInteraction":true,"renotify":true,"data":{"url":"https://maps.google.com"}}}
您可以在网址内输入网址,点击通知后,您的推送通知将打开给定的链接并将其聚焦在浏览器中
答案 3 :(得分:0)
对于Webpush Ruby Gem,经过一些修改,这对我有用:
Webpush.payload_send(
endpoint: user.push_subscription.endpoint,
message: {
message: "A new service request is created",
id: service_request.request_number,
url: "https://google.com/"
}.to_json,
p256dh: user.push_subscription.key,
auth: user.push_subscription.auth_key,
ttl: 24 * 60 * 60,
vapid: {
subject: 'A Push Notification',
public_key: ENV['VAPID_PUBLIC_KEY'],
private_key: ENV['VAPID_PRIVATE_KEY']
}
)
位置:
user.push_subscription.endpoint
是PushSubscription
模型中定义的用于返回端点的方法user.push_subscription.key, user.push_subscription.auth_key
还是在同一模型中定义的方法在/serviceworker.js.erb
内
self.addEventListener("push", (event) => {
let response = event.data && event.data.text();
let title = JSON.parse(response).message;
let body = JSON.parse(response).id;
let icon = '/assets/logo-blue-120x120.jpg';
event.waitUntil(
self.registration.showNotification(title, { body, icon, data: { url: JSON.parse(response).url } })
)
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
});