Files
doneit-web/src/app/services/notification/web-notification-popup.service.ts
T

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-08-31 11:10:40 +01:00
import { Injectable } from '@angular/core';
2021-08-31 11:44:10 +01:00
import { AlertController, Platform } from '@ionic/angular';
2021-08-31 22:49:37 +01:00
import { v4 as uuidv4 } from 'uuid'
2021-08-31 11:10:40 +01:00
@Injectable({
providedIn: 'root'
})
export class WebNotificationPopupService {
2021-08-31 22:49:37 +01:00
constructor( private platform: Platform) {
var myWorker = new Worker( new URL('./sw.js', import.meta.url));
myWorker.onmessage = function(oEvent) {
console.log('Worker said : ' + oEvent.data);
}
}
2021-08-31 11:10:40 +01:00
askNotificationPermission() {
2021-08-31 11:44:10 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {}
else {return false}
2021-08-31 11:10:40 +01:00
// function to actually ask the permissions
function handlePermission(permission) {}
// Let's check if the browser supports notifications
if (!('Notification' in window)) {
console.log("This browser does not support notifications.");
} else {
if(this.checkNotificationPromise()) {
Notification.requestPermission()
.then((permission) => {
handlePermission(permission);
})
} else {
Notification.requestPermission(function(permission) {
handlePermission(permission);
});
}
}
}
private checkNotificationPromise() {
try {
Notification.requestPermission().then();
} catch(e) {
return false;
}
return true;
}
2021-08-31 22:49:37 +01:00
cc = 0;
sendNotification(e) {
2021-08-31 19:30:08 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {}
else {return false}
2021-08-31 22:49:37 +01:00
Notification.requestPermission((result) => {
if (result === 'granted') {
navigator.serviceWorker.ready.then((registration)=> {
registration.showNotification(e.Object+ this.cc, {
body: e.Service,
icon: 'assets/icon/favicon.png',
requireInteraction: true,
tag: 'require-interaction'+uuidv4(),
// actions: [
// {action: 'like', title: 'Like', icon: 'https://example/like.png'},
// {action: 'reply', title: 'Reply', icon: 'https://example/reply.png'}
// ]
}).then(e =>{
console.log(e)
})
});
}
});
2021-08-31 19:30:08 +01:00
2021-08-31 11:10:40 +01:00
}
}