Skip to content

Instantly share code, notes, and snippets.

@divyasonaraa
Last active November 27, 2023 11:22
Show Gist options
  • Save divyasonaraa/1efe17cc4d852872a3868a61d4114a38 to your computer and use it in GitHub Desktop.
Save divyasonaraa/1efe17cc4d852872a3868a61d4114a38 to your computer and use it in GitHub Desktop.
/*
events - push
*/
self.addEventListener("push", (event) => {
if (event.data) {
let data = JSON.parse(event.data.text());
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: "icons/icon-128x128.png",
badge: "icons/icon-128x128.png",
data: {
openUrl: data.openUrl,
},
})
);
}
});
/*
events - notifications
*/
self.addEventListener("notificationclick", (event) => {
let notification = event.notification;
event.waitUntil(
clients.matchAll().then((clis) => {
let clientUsingApp = clis.find((cli) => {
return cli.visibilityState === "visible";
});
if (clientUsingApp) {
clientUsingApp.navigate(notification.data.openUrl);
clientUsingApp.focus();
} else {
clients.openWindow(notification.data.openUrl);
}
})
);
notification.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment