運行這段代碼:
if ('Notification' in window) {
Notification.requestPermission();
}
控制檯在Chrome DevTools顯示了這個錯誤:
Uncaught TypeError: Failed to construct ‘Notification’: Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead
更好的方法可能是:
function isNewNotificationSupported() {
if (!window.Notification || !Notification.requestPermission)
return false;
if (Notification.permission == 'granted')
throw new Error('You must only call this \*before\* calling
Notification.requestPermission(), otherwise this feature detect would bug the
user with an actual notification!');
try {
new Notification('');
} catch (e) {
if (e.name == 'TypeError')
return false;
}
return true;
}
功能來源:HTML5Rocks
我確切地說同樣的問題。還嘗試過'window.webkitNotification'。 *推送通知*,另一方面,工作,但完全是一個不同的野獸:https://developers.google.com/web/updates/2015/03/push-notificatons-on-the-open-web?hl = en(演示https://simple-push-demo.appspot.com/) – BoppreH