【发布时间】:2017-09-25 22:21:04
【问题描述】:
我已经使用 Service Worker 实现了推送通知。有什么方法可以找出窗口中当前显示的通知数量?我的目的是限制窗口中显示的通知数量。
我尝试了以下方法。但是 getNotifications 函数返回我的空数组。
self.addEventListener('push', function(event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
var data = event.data.json();
var options = {
body: data.notificationText,
icon: 'files/assets/staff.png',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
onClickUrl: data.onClickUrl,
event_id: data.event_id,
productName: data.product_name
}
};
event.waitUntil(
self.registration.getNotifications().then(function(notifications) {
console.log(notifications);
if (notifications && notifications.length > 0) {
notifications.forEach(function(notification) {
notification.close();
});
}
showNotification(data.title, options);
})
);
});
【问题讨论】:
标签: notifications push service-worker