Skip to content

Commit b335978

Browse files
committed
Handle notificationclick
1 parent 9468b6a commit b335978

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

public/service-worker.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
self.addEventListener('push', (event) => {
44
// https://developer.mozilla.org/en-US/docs/Web/API/PushEvent/data
55
const payload = event.data.json();
6+
console.log(`payload: ${JSON.stringify(payload, null, 2)}`);
67

78
const options = {
89
body: payload.body,
@@ -12,20 +13,54 @@ self.addEventListener('push', (event) => {
1213
// The notification object includes a tag attribute that is the grouping key.
1314
// When creating a notification with a tag and there is already a notification with the same tag visible to the user,
1415
// the system automatically replaces it without creating a new notification.
15-
tag: 'vibration-sample',
16+
// tag: 'vibration-sample',
1617
actions: [
1718
{
1819
// https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications#the_notificationclick_event
19-
action: 'open-a-link',
20-
title: 'Open a link',
20+
action: 'open-product-link',
21+
title: `Open product's link`,
2122
//icon: '/demos/notification-examples/images/action-1-128x128.png'
2223
}
2324
],
25+
data: {
26+
url: payload.url
27+
}
2428
}; // end of options
2529

26-
event.waitUntil(
27-
self.registration.showNotification(payload.title, options)
28-
);
30+
// https://javascript.info/promise-basics
31+
// https://javascript.info/async-await
32+
33+
// https://swizec.com/blog/how-to-add-real-web-push-notifications-to-your-webapp/
34+
event.waitUntil(showNotificationAsync(payload, options));
35+
36+
});
37+
38+
// https://stackoverflow.com/a/63917373/1872200
39+
// aync function is a function that synchronously returns a promise
40+
async function showNotificationAsync(payload, options) {
41+
await self.registration.showNotification(payload.title, options);
42+
}
43+
44+
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event#examples
45+
self.addEventListener('notificationclick', (event) => {
46+
const { action, notification } = event;
47+
console.log(JSON.stringify(notification, null, 2))
48+
49+
notification.close();
50+
if (action === 'open-product-link') {
51+
// https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow#return_value
52+
53+
// https://web-push-book.gauntface.com/common-notification-patterns/
54+
clients.openWindow(notification.data.url);
55+
}
56+
57+
// event.waitUntil(openProductLink(action, data));
2958
});
3059

31-
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event#examples
60+
async function openProductLink(action, data) {
61+
notification.close();
62+
if (action === 'open-product-link') {
63+
// https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow#return_value
64+
await clients.openWindow(data.url);
65+
}
66+
}

0 commit comments

Comments
 (0)