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