我有一些像这样的Javascript,作为我的Service Worker。无论我做什么,我都无法使通知可点击。推送通知可以正常显示,这很好,一切都很好,但是当我单击它时,什么也没发生-没有打开URL或任何东西。我真的不确定这里是什么问题。
当然,目标是能够单击通知并使其启动一个新窗口,并且我认为我的代码已完美地设置为可以执行此操作,但是它似乎不起作用。通知本身似乎不是可单击的,不要忘记“操作”按钮,该按钮也不可单击。
self.addEventListener('push', function(event) {
const parsed = JSON.parse(event.data.text());
event.waitUntil(
self.registration.showNotification('Scam Stopper', {
body: parsed.body,
icon: "https://i.imgur.com/QbweVE1.jpg",
vibrate: [200, 100, 200, 100, 200, 100, 400],
actions: [{ "action": "yes", "title": "View on Shopify"}],
}
)
);
}
);
self.addEventListener('notificationclick', function(event) {
const parsed = JSON.parse(event.data.text());
let url = parsed.link;
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);
}
})
);
});