2019-11-11 18:09:47 +09:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:38:51 +09:00
|
|
|
notify(noti: NotificationRequest, click?: () => void) {
|
2019-11-13 11:48:39 +09:00
|
|
|
if (!this.isSupported()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-11 18:23:30 +09:00
|
|
|
const notification = new Notification(noti.title, {
|
|
|
|
body: noti.contents,
|
2019-11-13 11:48:39 +09:00
|
|
|
icon: noti.image || 'assets/images/img_nophoto_50.png',
|
2019-11-11 18:23:30 +09:00
|
|
|
});
|
2019-11-11 18:09:47 +09:00
|
|
|
notification.onclick = e => {
|
|
|
|
console.log('notification.onclick');
|
2019-11-12 10:38:51 +09:00
|
|
|
if (!!click) {
|
|
|
|
click();
|
|
|
|
}
|
2019-11-11 18:09:47 +09:00
|
|
|
};
|
|
|
|
notification.onclose = e => {
|
|
|
|
console.log('notification.onclose');
|
|
|
|
};
|
|
|
|
notification.onerror = e => {
|
|
|
|
console.log('notification.onerror');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|