I’ve implemented push notifications using service workers. Is there any way to find out the number of notifications which are currently shown in the window? My intention is to limit the number of notifications shown in the window.
I tried the following. But the getNotifications function returning me empty array.
$(document).ready(function(){
"use strict";
$("#uploadBtn").on("change", function(){
var files = $(this)[0].files;
if(files.length >= 2){
$("#label_span").text(files.length + " files ready to upload");
} else {
var filename = addEventListener.target.value.split('\\').pop();
$("#label_span").text(filename);
}
});
});
答案 0 :(得分:3)
您可以使用serviceWorker.getNotifications()
返回通知列表。您可以像这样使用它:
navigator.serviceWorker.register('sw.js');
navigator.serviceWorker.ready.then(function(registration) {
registration.getNotifications().then(function(notifications) {
// get the number of notifications
})
});
如果你在服务工作文件中这样做,那就是:
self.registration.getNotifications().then(function(notifications) {
// get the number of notifications
})