add web notification popup

This commit is contained in:
Peter Maquiran
2021-08-31 11:10:40 +01:00
parent 8056981ca1
commit b9b3f5be24
3 changed files with 70 additions and 1 deletions
+7 -1
View File
@@ -18,6 +18,7 @@ import { ExpedienteGdStore } from '../store/expedientegd-store.service';
import { InativityService } from '../services/inativity.service';
import { SessionStore } from '../store/session.service';
import { StorageService } from '../services/storage.service';
import { WebNotificationPopupService } from '../services/notification/web-notification-popup.service';
@Component({
selector: 'app-home',
@@ -72,7 +73,10 @@ export class HomePage implements OnInit {
public documentCounterService: DocumentCounterService,
private despachoRule: DespachoService,
private inativityService: InativityService,
private storageService: StorageService,) {
private storageService: StorageService,
private webNotificationPopupService: WebNotificationPopupService) {
this.webNotificationPopupService.askNotificationPermission()
this.router.events.subscribe((val) => {
document.querySelectorAll('ion-modal').forEach((e: any) => e.remove())
@@ -176,6 +180,8 @@ export class HomePage implements OnInit {
synchro.registerCallback('Notification', (DataArray)=> {
this.webNotificationPopupService.sendNotification(DataArray.Object)
this.storageService.get('Notifications').then((data)=>{
data.push(DataArray)
this.storageService.store("Notifications", data)
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { WebNotificationPopupService } from './web-notification-popup.service';
describe('WebNotificationPopupService', () => {
let service: WebNotificationPopupService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(WebNotificationPopupService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,47 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class WebNotificationPopupService {
constructor() { }
askNotificationPermission() {
// 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;
}
sendNotification(message) {
var n = new Notification(message);
}
}