createEmptyNotification

This commit is contained in:
tamaina 2021-03-29 21:36:53 +09:00
parent b949dec9f4
commit 1e454e99ae
3 changed files with 25 additions and 27 deletions

View File

@ -1575,8 +1575,7 @@ _notification:
youReceivedFollowRequest: "フォローリクエストが来ました" youReceivedFollowRequest: "フォローリクエストが来ました"
yourFollowRequestAccepted: "フォローリクエストが承認されました" yourFollowRequestAccepted: "フォローリクエストが承認されました"
youWereInvitedToGroup: "{userName}があなたをグループに招待しました" youWereInvitedToGroup: "{userName}があなたをグループに招待しました"
readAllNotifications: "通知はすべて既読です" emptyPushNotificationMessage: "プッシュ通知の更新をしました"
readAllMessagingMessages: "チャットはすべて既読です"
_types: _types:
all: "すべて" all: "すべて"

View File

@ -11,7 +11,9 @@ import { pushNotificationData } from '@/types';
export async function createNotification(data: pushNotificationData) { export async function createNotification(data: pushNotificationData) {
const n = await composeNotification(data); 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> { 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') { export async function createEmptyNotification() {
const n = await composeAllReadNotification(type);
if (n) return self.registration.showNotification(...n);
}
async function composeAllReadNotification(type: string): Promise<[string, NotificationOptions] | null | undefined> {
if (!swLang.i18n) swLang.fetchLocale(); if (!swLang.i18n) swLang.fetchLocale();
const i18n = await swLang.i18n as I18n<any>; const i18n = await swLang.i18n as I18n<any>;
const { t } = i18n; const { t } = i18n;
if (type === 'notifications') { await self.registration.showNotification(
return [t('readAllNotifications'), { t('_notification.emptyPushNotificationMessage'),
{
silent: true, silent: true,
tag: 'user_visible_auto_notification', tag: 'read_notification',
}];
}
if (type === 'messagingMessages') {
return [t('readAllMessagingMessages'), {
silent: true,
tag: 'user_visible_auto_notification',
}];
} }
);
} }

View File

@ -3,7 +3,7 @@
*/ */
declare var self: ServiceWorkerGlobalScope; 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 { swLang } from '@client/sw/lang';
import { swNotificationRead } from '@client/sw/notification-read'; import { swNotificationRead } from '@client/sw/notification-read';
import { pushNotificationData } from '@/types'; import { pushNotificationData } from '@/types';
@ -41,12 +41,6 @@ self.addEventListener('fetch', ev => {
//#region When: Caught Notification //#region When: Caught Notification
self.addEventListener('push', ev => { 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({ ev.waitUntil(self.clients.matchAll({
includeUncontrolled: true, includeUncontrolled: true,
@ -62,6 +56,7 @@ self.addEventListener('push', ev => {
case 'notification': case 'notification':
case 'unreadMessagingMessage': case 'unreadMessagingMessage':
return createNotification(data); return createNotification(data);
case 'readAllNotifications': case 'readAllNotifications':
for (const n of await self.registration.getNotifications()) { for (const n of await self.registration.getNotifications()) {
if (n.data.type === 'notification') n.close(); if (n.data.type === 'notification') n.close();
@ -91,6 +86,18 @@ self.addEventListener('push', ev => {
} }
break; 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 //#endregion