为什么推送通知不显示与showNotification JavaScript,Service Worker?

时间:2019-11-15 03:43:34

标签: javascript push-notification service-worker

我正尝试与服务人员在浏览器中显示本地通知,它询问我是否要接受通知,我回答是,但它不显示创建的通知

那可能正在发生?我是否需要修改或配置其他内容?

const check = () => {
    if (!('serviceWorker' in navigator)) {
      throw new Error('No Service Worker support!')
    }
    if (!('PushManager' in window)) {
      throw new Error('No Push API Support!')
    }
  }
// I added a function that can be used to register a service worker.
const registerServiceWorker = async () => {
    const swRegistration = await navigator.serviceWorker.register('/servicereminder/js/service.js'); //notice the file name
    return swRegistration;
}
 const requestNotificationPermission = async () => {
     const permission = await window.Notification.requestPermission();

     if(permission !== 'granted'){
         throw new Error('Permission not granted for Notification');
     }
 }
 const showLocalNotification = (title, body, swRegistration) => {
    const options = {
        body,
        // here you can add more properties like icon, image, vibrate, etc.
    };

    swRegistration.showNotification(title, options);
}
 const main = async () => {
     check();
     const swRegistration = await registerServiceWorker();
     const permission =  await requestNotificationPermission();

    //showLocalNotification('This is title', 'this is the message', swRegistration);

    swRegistration.showNotification('test')


 }
 main();

0 个答案:

没有答案