This commit is contained in:
tamaina 2021-09-21 02:08:02 +09:00
parent 74dc874110
commit 1c49074ea2
2 changed files with 22 additions and 14 deletions

View file

@ -14,11 +14,15 @@ import { getAccountFromId } from '@client/scripts/get-account-from-id';
export async function createNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) { export async function createNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
const n = await composeNotification(data); const n = await composeNotification(data);
if (n) await self.registration.showNotification(...n); if (n) {
else await createEmptyNotification(); return self.registration.showNotification(...n);
} else {
console.error('Could not compose notification', data);
return createEmptyNotification();
}
} }
async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null | undefined> { async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null> {
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;
@ -151,7 +155,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
}]; }];
case 'groupInvited': case 'groupInvited':
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(data.body.group) }), { return [t('_notification.youWereInvitedToGroup', { userName: getUserName(data.body.user) }), {
body: data.body.invitation.group.name, body: data.body.invitation.group.name,
data, data,
actions: [ actions: [
@ -208,4 +212,18 @@ export async function createEmptyNotification() {
tag: 'read_notification', tag: 'read_notification',
} }
); );
return new Promise<void>(res => {
setTimeout(async () => {
for (const n of
[
...(await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })),
...(await self.registration.getNotifications({ tag: 'read_notification' }))
]
) {
n.close();
}
res();
}, 1000);
})
} }

View file

@ -88,16 +88,6 @@ self.addEventListener('push', ev => {
} }
createEmptyNotification(); 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();
}
}, 1000);
})); }));
}); });
//#endregion //#endregion