I am guessing you are in a Service Worker context, because that’s where Push Notifications are received. So you have the self
object to add a event listener to, that will react to a click on the notification.
(Place this code in your sw.js
file, which is your Service Worker script.)
self.addEventListener('notificationclick', function(event) {
let url="https://example.com/some-path/";
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({type: 'window'}).then( windowClients => {
// Check if there is already a window/tab open with the target URL
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
// If so, just focus it.
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});