From 1e454e99ae7f6d9a09db0340199853e891b5a225 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 29 Mar 2021 21:36:53 +0900 Subject: [PATCH] createEmptyNotification --- locales/ja-JP.yml | 3 +-- src/client/sw/create-notification.ts | 28 ++++++++++------------------ src/client/sw/sw.ts | 21 ++++++++++++++------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index fa151093f4..9bd7e4e6af 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -1575,8 +1575,7 @@ _notification: youReceivedFollowRequest: "フォローリクエストが来ました" yourFollowRequestAccepted: "フォローリクエストが承認されました" youWereInvitedToGroup: "{userName}があなたをグループに招待しました" - readAllNotifications: "通知はすべて既読です" - readAllMessagingMessages: "チャットはすべて既読です" + emptyPushNotificationMessage: "プッシュ通知の更新をしました" _types: all: "すべて" diff --git a/src/client/sw/create-notification.ts b/src/client/sw/create-notification.ts index 23a65826bd..4682ab0356 100644 --- a/src/client/sw/create-notification.ts +++ b/src/client/sw/create-notification.ts @@ -11,7 +11,9 @@ import { pushNotificationData } from '@/types'; export async function createNotification(data: pushNotificationData) { const n = await composeNotification(data); - if (n) return self.registration.showNotification(...n); + + if (n) await self.registration.showNotification(...n); + else await createEmptyNotification(); } async function composeNotification(data: pushNotificationData): Promise<[string, NotificationOptions] | null | undefined> { @@ -183,26 +185,16 @@ async function composeNotification(data: pushNotificationData): Promise<[string, } } -export async function createAllReadNotification(type: 'notifications' | 'messagingMessages') { - const n = await composeAllReadNotification(type); - if (n) return self.registration.showNotification(...n); -} - -async function composeAllReadNotification(type: string): Promise<[string, NotificationOptions] | null | undefined> { +export async function createEmptyNotification() { if (!swLang.i18n) swLang.fetchLocale(); const i18n = await swLang.i18n as I18n; const { t } = i18n; - if (type === 'notifications') { - return [t('readAllNotifications'), { + await self.registration.showNotification( + t('_notification.emptyPushNotificationMessage'), + { silent: true, - tag: 'user_visible_auto_notification', - }]; - } - if (type === 'messagingMessages') { - return [t('readAllMessagingMessages'), { - silent: true, - tag: 'user_visible_auto_notification', - }]; - } + tag: 'read_notification', + } + ); } diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index 89bb168dbf..8ef081fdc0 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -3,7 +3,7 @@ */ declare var self: ServiceWorkerGlobalScope; -import { createNotification } from '@client/sw/create-notification'; +import { createEmptyNotification, createNotification } from '@client/sw/create-notification'; import { swLang } from '@client/sw/lang'; import { swNotificationRead } from '@client/sw/notification-read'; import { pushNotificationData } from '@/types'; @@ -41,12 +41,6 @@ self.addEventListener('fetch', ev => { //#region When: Caught Notification self.addEventListener('push', ev => { - setTimeout(async () => { - for (const n of await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })) { - n.close(); - } - }, 5000); - // クライアント取得 ev.waitUntil(self.clients.matchAll({ includeUncontrolled: true, @@ -62,6 +56,7 @@ self.addEventListener('push', ev => { case 'notification': case 'unreadMessagingMessage': return createNotification(data); + case 'readAllNotifications': for (const n of await self.registration.getNotifications()) { if (n.data.type === 'notification') n.close(); @@ -91,6 +86,18 @@ self.addEventListener('push', ev => { } break; } + + createEmptyNotification(); + setTimeout(async () => { + for (const n of + [ + ...(await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })), + ...(await self.registration.getNotifications({ tag: 'read_notification' })) + ] + ) { + n.close(); + } + }, 500); })); }); //#endregion