单击网络推送通知时打开自定义 url
Open a custom url when clicking on a web push notification
我正在实施 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
)无法从 service worker 中访问。
有谁知道如何通过 WebPush 传递自定义数据gem?
从 Webpush (with a payload) documentation 看来,您应该使用 JSON.stringify()
方法将所有数据放入消息中,并使用 JSON.parse()
在 service worker 中检索它。
服务器:
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
}
})
自定义数据直接来自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)
);
})
将此添加到 node_modules/@angular/service-worker/ngsw-worker.js
中的 Angular 项目中
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"}}}
在 url 中您可以输入您的 url 并在点击通知时您的推送通知将打开给定的 link 并将其聚焦在浏览器中
对于 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
模型中定义到 return 端点 的方法
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)
);
});
我正在实施 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
)无法从 service worker 中访问。
有谁知道如何通过 WebPush 传递自定义数据gem?
从 Webpush (with a payload) documentation 看来,您应该使用 JSON.stringify()
方法将所有数据放入消息中,并使用 JSON.parse()
在 service worker 中检索它。
服务器:
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
}
})
自定义数据直接来自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)
);
})
将此添加到 node_modules/@angular/service-worker/ngsw-worker.js
中的 Angular 项目中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"}}}
在 url 中您可以输入您的 url 并在点击通知时您的推送通知将打开给定的 link 并将其聚焦在浏览器中
对于 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
模型中定义到 return 端点 的方法
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)
);
});