fchat-rising/webchat/notifications.ts

23 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2018-07-20 01:12:26 +00:00
import core from '../chat/core';
import {Conversation} from '../chat/interfaces';
//tslint:disable-next-line:match-default-export-name
import BaseNotifications from '../chat/notifications';
export default class Notifications extends BaseNotifications {
async notify(conversation: Conversation, title: string, body: string, icon: string, sound: string): Promise<void> {
if(!this.shouldNotify(conversation)) return;
try {
2018-08-10 16:59:37 +00:00
await super.notify(conversation, title, body, icon, sound);
2018-07-20 01:12:26 +00:00
} catch {
2018-08-10 16:59:37 +00:00
//tslint:disable-next-line:no-require-imports no-submodule-imports
await navigator.serviceWorker.register(<string>require('file-loader!./sw.js'));
const reg = await navigator.serviceWorker.ready;
await reg.showNotification(title, this.getOptions(conversation, body, icon));
navigator.serviceWorker.onmessage = (e) => {
const conv = core.conversations.byKey((<{key: string}>e.data).key);
if(conv !== undefined) conv.show();
window.focus();
};
2018-07-20 01:12:26 +00:00
}
}
}