Tôi đang triển khai Webpush ruby gem để gửi thông báo đẩy tới người dùng trang web của tôi.Mở url tùy chỉnh khi nhấp vào thông báo đẩy web
đang 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,
})
tôi có một nhân viên dịch vụ thành lập ở phía client để hiển thị các thông báo và làm cho nó có thể click.
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)
);
})
Đó là tất cả làm việc tốt, ngoại trừ các phím tùy chỉnh (url
, id
) mà tôi đang đi qua không thể truy cập từ bên trong nhân viên dịch vụ.
Có ai biết cách chuyển dữ liệu tùy chỉnh qua đá quý WebPush không?
im không phải là chuyên gia về điều này, nhưng tôi sẽ làm: message: JSON.stringify (notification) on máy chủ và JSON.parse trên máy khách ... –
@ Jonasw - Tôi đã kết thúc với giải pháp của bạn. Nếu bạn viết nó như một câu trả lời, tôi sẽ chấp nhận nó. Cảm ơn đã giúp đỡ. – BananaNeil