在我的Chrome JavaScript函数中调用该函数(在Service Worker之外)在我的情况下实际上是可行的,我这样尝试:
function showNotification() {
navigator.serviceWorker.getRegistration().then(registration => {
setTimeout(() => {
registration.showNotification('Title', {
body: 'body blablabla',
badge: '/images/icon-light.png',
icon: '/images/icon-light.png',
renotify: false,
requireInteraction: true,
silent: false,
vibrate: [200, 100, 200],
dir: 'ltr',
lang: 'en-US'
});
}, 3000);
});
}
使命感
showNotification
在软件工作中,我也尝试了以下代码以及
Applications -> Service Workers -> Push
在Chrome Devtools控制台按钮中,发送推送通知,然后在3秒钟后显示:
self.addEventListener('push', function(event) {
if (event.data) {
console.log('This push event has data: ', event.data.text());
} else {
console.log('This push event has no data.');
}
setTimeout(() => {
self.registration.showNotification(
'Title',
{
body: 'body blablabla',
badge: '/images/icon-light.png',
icon: '/images/icon-light.png',
renotify: false,
requireInteraction: true,
silent: false,
vibrate: [200, 100, 200],
dir: 'ltr',
lang: 'en-US'
}
);
}, 3000);
});
希望这有帮助!