45 lines
1.1 KiB
TypeScript

import { NotificationRequest } from '@ucap-webmessenger/native';
export class NotificationService {
notificationPermission: NotificationPermission;
constructor() {
this.notificationPermission = this.isSupported() ? 'default' : 'denied';
}
public isSupported(): boolean {
return 'Notification' in window;
}
requestPermission(): void {
const self = this;
if ('Notification' in window) {
Notification.requestPermission().then(result => {
self.notificationPermission = result;
});
}
}
notify(noti: NotificationRequest, click?: () => void) {
if (!this.isSupported()) {
return;
}
const notification = new Notification(noti.title, {
body: noti.contents,
icon: noti.image || 'assets/images/img_nophoto_50.png',
});
notification.onclick = e => {
console.log('notification.onclick');
if (!!click) {
click();
}
};
notification.onclose = e => {
console.log('notification.onclose');
};
notification.onerror = e => {
console.log('notification.onerror');
};
}
}