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

230 lines
8.5 KiB
TypeScript
Raw Normal View History

/* eslint-disable */
2021-07-08 14:45:38 +01:00
/* tslint:disable */
2021-06-25 09:35:50 +01:00
import { Injectable, NgZone } from '@angular/core';
2021-10-05 15:02:52 +01:00
import { HttpClient } from '@angular/common/http';
2021-02-01 13:31:33 +01:00
import { environment } from 'src/environments/environment';
2021-04-12 08:34:17 +01:00
import { StorageService } from 'src/app/services/storage.service';
import { AuthConnstants } from 'src/app/config/auth-constants';
import { Tokenn } from '../models/token.model';
2023-01-24 15:56:47 +01:00
import { AlertController, Platform } from '@ionic/angular';
import { Router } from '@angular/router';
2021-09-21 06:09:41 +01:00
import { EventTrigger } from '../services/eventTrigger.service';
2021-09-30 15:42:51 +01:00
import { SessionStore } from '../store/session.service';
2023-01-24 15:56:47 +01:00
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { notificationObject } from '../models/notifications';
2021-02-01 13:31:33 +01:00
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
2021-04-12 08:34:17 +01:00
adding: "intervenient" | "CC" = "intervenient";
folderId: string;
2021-11-04 06:39:39 +01:00
DataArray = new Array();
2021-04-12 08:34:17 +01:00
2023-01-24 15:56:47 +01:00
callbacks: {[key: string]: {
type: notificationObject,
2021-08-25 11:51:02 +01:00
funx: Function
2021-09-03 12:19:21 +01:00
id: string
2023-01-24 15:56:47 +01:00
}} = {}
2021-08-25 11:51:02 +01:00
2023-01-03 21:08:49 +01:00
active = false
2021-04-12 08:34:17 +01:00
constructor(
2021-09-03 12:19:21 +01:00
private http: HttpClient,
2021-04-12 08:34:17 +01:00
private storageService: StorageService,
public modalCtrl: AlertController,
2021-06-25 09:35:50 +01:00
private platform: Platform,
2021-11-03 12:21:19 +01:00
private router: Router,
2021-07-07 15:28:29 +01:00
private zone: NgZone,
2023-01-24 15:56:47 +01:00
private eventtrigger: EventTrigger,) {
2021-08-25 11:51:02 +01:00
2021-11-03 12:21:19 +01:00
}
2021-08-25 11:51:02 +01:00
2021-02-01 13:31:33 +01:00
2023-01-24 15:56:47 +01:00
// registerCallback({type, funx, id = uuidv4()} : {
// type: notificationObject,
// funx: Function
// id?: string
// }) {
// this.callbacks[id] = { funx, id, type}
// return id;
// }
2021-09-03 12:19:21 +01:00
2023-01-24 15:56:47 +01:00
// 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]
// }
// }
// }
// }
2021-09-03 12:19:21 +01:00
2021-02-01 13:31:33 +01:00
getTokenByUserIdAndId(user, userID) {
2022-01-03 18:45:37 +01:00
// const geturl = environment.apiURL + 'notifications/user/' + userID;
2021-02-01 13:31:33 +01:00
2022-01-03 18:45:37 +01:00
// return this.http.get<Tokenn[]>(`${geturl}`);
}
requestPermissions() {
2023-01-24 15:56:47 +01:00
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
2021-02-01 13:31:33 +01:00
}
getAndpostToken(username) {
2022-04-01 09:06:57 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
2022-04-28 09:32:27 +01:00
2022-04-01 09:06:57 +01:00
} else {
const geturl = environment.apiURL + 'notifications/token';
2023-01-24 15:56:47 +01:00
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)
})
}
);
2022-04-01 09:06:57 +01:00
}
2021-11-03 12:21:19 +01:00
2021-02-01 13:31:33 +01:00
}
registrationError() {
2023-01-24 15:56:47 +01:00
PushNotifications.addListener('registrationError',
(error: any) => {
this.active = false
}
);
}
2021-11-03 12:21:19 +01:00
onReciveForeground() {
2023-01-24 15:56:47 +01:00
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
this.active = true
console.log(notification)
//this.DataArray.push(notification)
//console.log(this.DataArray)
2022-12-30 15:29:10 +01:00
2023-01-24 15:56:47 +01:00
this.storageService.get("Notifications").then((store) => {
store.push(notification)
this.storageService.store("Notifications", store)
}).catch((error) => {
if(!error) {
this.storageService.store("Notifications", [notification])
}
})
2022-04-28 09:32:27 +01:00
2023-01-24 15:56:47 +01:00
// this.runNotificationCallback(notification)
/* this.eventTriger.publish('notificatioRecive') */
this.eventtrigger.publishSomeData({
notification: "recive"
})
}
);
}
onReciveBackground() {
2023-01-24 15:56:47 +01:00
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
this.active = true
console.log(notification)
/* this.DataArray.push(notification.notification)
2022-04-28 09:32:27 +01:00
2023-01-24 15:56:47 +01:00
this.storageService.store("Notifications", this.DataArray)
this.eventtrigger.publishSomeData({
notification: "recive"
}) */
this.notificatinsRoutes(notification)
// this.runNotificationCallback(notification)
}
);
}
2021-11-03 12:21:19 +01:00
2022-12-30 15:29:10 +01:00
tempClearArray(data) {
this.DataArray = new Array;
2021-11-03 12:21:19 +01:00
}
2021-04-12 08:34:17 +01:00
2021-07-31 22:04:09 +01:00
notificatinsRoutes = (notification) => {
2021-11-09 17:23:39 +01:00
2022-04-28 09:32:27 +01:00
2022-04-01 09:06:57 +01:00
if (notification.notification.data.Service === "agenda" && notification.notification.data.IdObject.length > 10) {
2022-04-28 09:32:27 +01:00
2022-04-01 09:06:57 +01:00
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']));
}
2021-04-12 08:34:17 +01:00
2021-04-12 15:39:44 +01:00
}
2021-06-25 09:35:50 +01:00
2021-02-01 13:31:33 +01:00
}