Skip to content

Instantly share code, notes, and snippets.

@hawlik
Created January 4, 2017 23:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hawlik/7478892b5950e18d10d576142936389f to your computer and use it in GitHub Desktop.
Save hawlik/7478892b5950e18d10d576142936389f to your computer and use it in GitHub Desktop.
service worker redirect to browser tab on push notification click event
//browser push notification "onClick" event heandler
self.addEventListener('notificationclick', function(event) {
console.log('[Service Worker] Notification click Received.');
event.notification.close();
/**
* if exists open browser tab with matching url just set focus to it,
* otherwise open new tab/window with sw root scope url
*/
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == self.registration.scope && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow('/');
}
}));
});
@thanhtutzaw
Copy link

My function is redirected to pwa when pwa is installed . Is there any solution? I was using FCM service worker with React.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment