我是新手,初次接触Google Chrome推送通知功能。我正在阅读stackoverflow上的一些问题和答案,最终找到了这段简单的推送通知JavaScript代码。
navigator.serviceWorker.register('sw.js');
function notify() {
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification('test notification', {
body: 'Hey I am test!',
icon: 'image.png',
});
});
}
});
}
这只是一个简单的通知,但我需要在点击通知后打开一个新窗口,并跳转到其他网页。
我知道这是可能的,但我找不到使用“serviceWorker”语法的示例。
请帮忙,谢谢。
matchAll({type: 'window'})
更改为matchAll({includeUncontrolled: true})
,它才开始对我起作用。此外,我搜索主机而不是精确的 URL。 - user3502626