1
1分後にプッシュ通知を非表示にする必要があります。これを達成するために私のサービスワーカーで何をすべきですか?しばらくしてからプッシュ通知を非表示にするにはどうすればよいですか?
1分後にプッシュ通知を非表示にする必要があります。これを達成するために私のサービスワーカーで何をすべきですか?しばらくしてからプッシュ通知を非表示にするにはどうすればよいですか?
Notification.close() methodを使用できます。 Notificationオブジェクトは、ServiceWorkerRegistration.showNotificationによって返された約束が解決するNotificationEventオブジェクトを使用して、通知にリンクさせることができます。このような
何か:
self.addEventListener('push', event => {
event.waitUntil(
self.registration.showNotification('Title', {
body: 'Body.',
})
.then(notificationEvent => {
let notification = notificationEvent.notification;
setTimeout(() => notification.close(), 60000);
})
);
});
別の可能な解決策は、あなたがServiceWorkerRegistration.getNotificationsを見せているすべての通知を取得することです。例えば
:
self.addEventListener('push', event => {
event.waitUntil(
self.registration.showNotification('Title', {
body: 'Body.',
})
.then(() => self.registration.getNotifications())
.then(notifications => {
setTimeout(() => notifications.forEach(notification => notification.close()), 60000);
})
);
});
約束でnotificationEvent変数が定義されていない、それが閉じていない理由thatsの。助けてください!! – Supermacy
代替ソリューションをお試しください。 – Marco
Chromeバージョン59.0.3071.115(公式ビルド)(64ビット)の2番目の解決策は変わっています。サーバーからメッセージをプッシュするたびに、デフォルトの通知通知が表示されます(このサイトはバックグラウンドで更新されています)。 –