Created
January 4, 2017 23:00
-
-
Save hawlik/7478892b5950e18d10d576142936389f to your computer and use it in GitHub Desktop.
service worker redirect to browser tab on push notification click event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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('/'); | |
} | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My function is redirected to pwa when pwa is installed . Is there any solution? I was using FCM service worker with React.