Skip to content

Commit

Permalink
readme Update serviceworker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
andreinwald authored Nov 5, 2023
1 parent 8126e09 commit eec63da
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions serviceworker.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
self.addEventListener('push', (event) => {
// PushData sample - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
// {
// "title": "Push title",
// "body": "Additional text with some description",
// "icon": "https://andreinwald.github.io/webpush-ios-example/images/push_icon.jpg",
// "data": {
// "url": "https://andreinwald.github.io/webpush-ios-example/success.html",
// "message_id": "your_internal_unique_message_id_for_tracking"
// }
// }
// PushData keys structure standart https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
let pushData = event.data.json();
if (!pushData || !pushData.title) {
console.error('Received WebPush with an empty title. Received body: ', pushData);
}
self.registration.showNotification(pushData.title, pushData)
.then(() => {
// You can save to your analytics fact that push was shown
fetch('https://your_backend_server.com/track_show?message_id=' + pushData.data.message_id);
// fetch('https://your_backend_server.com/track_show?message_id=' + pushData.data.message_id);
});
});

Expand All @@ -35,6 +26,6 @@ self.addEventListener('notificationclick', function (event) {
clients.openWindow(event.notification.data.url)
.then(() => {
// You can send fetch request to your analytics API fact that push was clicked
fetch('https://your_backend_server.com/track_click?message_id=' + pushData.data.message_id);
// fetch('https://your_backend_server.com/track_click?message_id=' + pushData.data.message_id);
});
});

0 comments on commit eec63da

Please sign in to comment.