I'm encountering an issue when trying to open a new link by clicking the web push notification.
Although my code works well and I am able to open the link after clicking the notification, I am seeing an error in the console. Interestingly, when I use a hardcoded URL, there is no error shown in the console.
Any assistance with resolving this error would be greatly appreciated.
Below is my service-worker.js code:
var link;
try{
self.addEventListener('push', function(e) {
var data = e.data.json();
var title = data.title;
var options = {
body: data.body
};
if(data.link){
link = data.link;
}
if(data.badge){
options.badge = data.badge;
}
if(data.icon){
options.icon = data.icon;
}
if(data.image){
options.image = data.image;
}
e.waitUntil(self.registration.showNotification(title, options));
});
}
catch(error){
console.log('error while sending push notification');
}
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(clients.openWindow(link));
});