Files
doneit-web/src/app/services/notifications.service.ts
T
Peter Maquiran fbd50137f3 merge
2023-01-24 15:56:47 +01:00

230 lines
8.5 KiB
TypeScript

/* eslint-disable */
/* tslint:disable */
import { Injectable, NgZone } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { StorageService } from 'src/app/services/storage.service';
import { AuthConnstants } from 'src/app/config/auth-constants';
import { Tokenn } from '../models/token.model';
import { AlertController, Platform } from '@ionic/angular';
import { Router } from '@angular/router';
import { EventTrigger } from '../services/eventTrigger.service';
import { SessionStore } from '../store/session.service';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { notificationObject } from '../models/notifications';
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
adding: "intervenient" | "CC" = "intervenient";
folderId: string;
DataArray = new Array();
callbacks: {[key: string]: {
type: notificationObject,
funx: Function
id: string
}} = {}
active = false
constructor(
private http: HttpClient,
private storageService: StorageService,
public modalCtrl: AlertController,
private platform: Platform,
private router: Router,
private zone: NgZone,
private eventtrigger: EventTrigger,) {
}
// registerCallback({type, funx, id = uuidv4()} : {
// type: notificationObject,
// funx: Function
// id?: string
// }) {
// this.callbacks[id] = { funx, id, type}
// return id;
// }
// private async runNotificationCallback(notification) {
// for (const [key, value] of Object.entries(this.callbacks)) {
// if(value.type == notification.data.Object) {
// const dontRepeat = await value.funx(notification)
// if(dontRepeat) {
// delete this.callbacks[key]
// }
// } else if (value.type == '*') {
// const dontRepeat = await value.funx(notification)
// if(dontRepeat) {
// delete this.callbacks[key]
// }
// }
// }
// }
getTokenByUserIdAndId(user, userID) {
// const geturl = environment.apiURL + 'notifications/user/' + userID;
// return this.http.get<Tokenn[]>(`${geturl}`);
}
requestPermissions() {
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
}
getAndpostToken(username) {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
} else {
const geturl = environment.apiURL + 'notifications/token';
PushNotifications.addListener('registration',
(token: Token) => {
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const body = {
UserId: SessionStore.user.UserId,
TokenId: token.value,
Status: 1,
Service: 1
};
this.http.post<Tokenn>(`${geturl}`, body, { headers }).subscribe(data => {
this.active = true
console.log(data)
}, (error) => {
console.log(error)
})
}
);
}
}
registrationError() {
PushNotifications.addListener('registrationError',
(error: any) => {
this.active = false
}
);
}
onReciveForeground() {
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
this.active = true
console.log(notification)
//this.DataArray.push(notification)
//console.log(this.DataArray)
this.storageService.get("Notifications").then((store) => {
store.push(notification)
this.storageService.store("Notifications", store)
}).catch((error) => {
if(!error) {
this.storageService.store("Notifications", [notification])
}
})
// this.runNotificationCallback(notification)
/* this.eventTriger.publish('notificatioRecive') */
this.eventtrigger.publishSomeData({
notification: "recive"
})
}
);
}
onReciveBackground() {
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
this.active = true
console.log(notification)
/* this.DataArray.push(notification.notification)
this.storageService.store("Notifications", this.DataArray)
this.eventtrigger.publishSomeData({
notification: "recive"
}) */
this.notificatinsRoutes(notification)
// this.runNotificationCallback(notification)
}
);
}
tempClearArray(data) {
this.DataArray = new Array;
}
notificatinsRoutes = (notification) => {
if (notification.notification.data.Service === "agenda" && notification.notification.data.IdObject.length > 10) {
this.zone.run(() => this.router.navigate(['/home/agenda', notification.notification.data.IdObject, 'agenda']));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "expediente") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expediente', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "agenda" && notification.notification.data.Object === "event-list") {
//this.zone.run(() => this.router.navigate(['/home/gabinete-digital/event-list/approve-event',IdObject, 'agenda']));
this.zone.run(() => this.router.navigate(['/home/agenda/event-list/approve-event', notification.notification.data.IdObject, 'agenda']));
} else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "despachos") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos', notification.notification.data.IdObject, 'gabinete-digital'], { replaceUrl: true }));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "parecer") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "deferimento") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "despachos-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos-pr', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "accoes" && notification.notification.data.Object === "accao") {
this.zone.run(() => this.router.navigate(['/home/publications', notification.notification.data.IdObject]));
}
else if (notification.notification.data.Service === "accoes" && notification.notification.data.Object === "publicacao") {
this.zone.run(() => this.router.navigate(['/home/publications/view-publications', notification.notification.data.FolderId, notification.data.IdObject]));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "diplomas") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "diplomas-assinar") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas-assinar', notification.notification.data.IdObject, 'gabinete-digital']));
}
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "expedientes-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expedientes-pr', notification.notification.data.IdObject, 'gabinete-digital']));
}
}
}