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

View File

@ -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<any>;
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',
}
);
}

View File

@ -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