1

这里是服务人员的侦听推送通知的代码:如何在JavaScript中设置推送通知的显示时间?

self.addEventListener('push', function (event) { 
var payload = event.data ? event.data.text() : 'no payload'; 

event.waitUntil(
     self.registration.showNotification('Notify Message', { 
      body: payload, 
      icon: 'push.png' 
     }) ); 

}); 

我有推送通知我的屏幕上时,来发送通知。但是我想在屏幕上显示推送通知一段时间(例如说90秒或发送通知的用户输入的动态持续时间)。这不适用于android或ios。这是桌面级通知。那么,如何设置推送通知的持续时间?

任何形式的帮助,高度赞赏。 感谢

+0

找到解决方案吗? –

回答

0

没有看到你是如何显示您的通知,你可以实现一个简单的超时,沿着线的东西:

function clearNotice(notice) { 
    // notice.hide(); 
} 

function showNotice(notice) { 
    notice.show(); 
    // clear your notice 
    setTimeout(clearNotice(notice), notice.timeout); 
} 
0

没有听众用或属性来设置时间。您可以使用以下代码关闭通知:

Notification.requestPermission(function(result) { 
if (result === 'granted') { 
    navigator.serviceWorker.ready.then(function(registration) { 
    registration.showNotification('Notify Message', { 
     body: payload, 
     icon: 'push.png' 
    }).then(function(event) { 
     console.log(event); 
      if(event && event.notification) { 
      setTimeout(function(){ event.notification.close();}, 3000); 
     } 
    }); 
    }); 
} 

});

+0

我得到TypeError:事件在承诺中未定义。但我收到通知。 –

+0

在上面的代码中给self.registration,并检查(event.Notifcation)是否关闭。 – vbharath

+0

更新我的代码,检查这个 – vbharath