2021-07-08 14:07:33 +01:00
|
|
|
/* 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-07-07 15:28:29 +01:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
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';
|
2021-11-05 07:49:01 +01:00
|
|
|
import { Tokenn } from '../models/token.model';
|
2021-04-12 15:39:44 +01:00
|
|
|
import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular';
|
2021-11-03 12:21:19 +01:00
|
|
|
import { NavigationExtras, Router } from '@angular/router';
|
2021-06-25 09:35:50 +01:00
|
|
|
import { ToastService } from '../services/toast.service';
|
2021-10-19 09:41:06 +01:00
|
|
|
import { BackgroundService } from './background.service';
|
2021-09-21 06:09:41 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
import { EventTrigger } from '../services/eventTrigger.service';
|
2021-09-30 15:42:51 +01:00
|
|
|
import { SessionStore } from '../store/session.service';
|
2021-11-05 07:49:01 +01:00
|
|
|
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
|
2021-10-30 16:51:29 +01:00
|
|
|
|
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
|
|
|
|
2021-08-25 11:51:02 +01:00
|
|
|
callbacks: {
|
|
|
|
|
type: string,
|
|
|
|
|
funx: Function
|
2021-09-03 12:19:21 +01:00
|
|
|
id: string
|
2021-08-25 11:51:02 +01:00
|
|
|
}[] = []
|
|
|
|
|
|
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,
|
2021-04-12 15:39:44 +01:00
|
|
|
private modalController: ModalController,
|
2021-04-12 08:34:17 +01:00
|
|
|
public modalCtrl: AlertController,
|
2021-04-12 15:39:44 +01:00
|
|
|
private animationController: AnimationController,
|
2021-06-25 09:35:50 +01:00
|
|
|
private platform: Platform,
|
2021-11-03 12:21:19 +01:00
|
|
|
private router: Router,
|
2021-06-25 09:35:50 +01:00
|
|
|
private toastService: ToastService,
|
2021-07-07 15:28:29 +01:00
|
|
|
private zone: NgZone,
|
2021-07-29 16:02:39 +01:00
|
|
|
private activeroute: ActivatedRoute,
|
2021-10-19 09:41:06 +01:00
|
|
|
private eventtrigger: EventTrigger,
|
2021-10-30 16:51:29 +01:00
|
|
|
private backgroundservice: BackgroundService,
|
2021-11-04 06:39:39 +01:00
|
|
|
/* private fcm: FCM */) {
|
2021-08-25 11:51:02 +01:00
|
|
|
|
2021-08-31 13:29:26 +01:00
|
|
|
this.storageService.get("Notifications").then((value) => {
|
2021-11-04 10:41:43 +01:00
|
|
|
|
2021-08-31 14:43:03 +01:00
|
|
|
}).catch(()=>{
|
2021-08-25 11:51:02 +01:00
|
|
|
|
2021-11-03 12:21:19 +01:00
|
|
|
}).catch(() => {
|
2021-08-31 13:29:26 +01:00
|
|
|
|
2021-11-03 12:21:19 +01:00
|
|
|
this.storageService.store("Notifications", [])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2021-08-25 11:51:02 +01:00
|
|
|
|
2021-11-03 12:21:19 +01:00
|
|
|
registerCallback(type: string, funx: Function, object: any = {}) {
|
2021-08-25 11:51:02 +01:00
|
|
|
|
2021-09-03 12:19:21 +01:00
|
|
|
const id = uuidv4()
|
2021-11-03 12:21:19 +01:00
|
|
|
this.callbacks.push({ type, funx, id })
|
|
|
|
|
if (!object.hasOwnProperty('desktop') && object['desktop'] != false) {
|
|
|
|
|
this.backgroundservice.registerBackService('Notification', funx, type)
|
2021-08-30 10:24:46 +01:00
|
|
|
}
|
2021-02-01 13:31:33 +01:00
|
|
|
|
2021-09-03 12:19:21 +01:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteCallback(id) {
|
2021-11-03 12:21:19 +01:00
|
|
|
this.callbacks.forEach((e, index) => {
|
|
|
|
|
if (e.id == id) {
|
2021-09-03 12:19:21 +01:00
|
|
|
if (index > -1) {
|
|
|
|
|
this.callbacks.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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}`);
|
2021-11-05 07:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestPermissions() {
|
2022-04-01 09:06:57 +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
|
|
|
}
|
|
|
|
|
|
2021-11-05 07:49:01 +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';
|
|
|
|
|
PushNotifications.addListener('registration',
|
|
|
|
|
(token: Token) => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
this.storageService.store(username, token.value);
|
|
|
|
|
this.storageService.get(username).then(value => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
this.storageService.get(AuthConnstants.USER).then(res => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
|
|
|
|
|
const body = {
|
|
|
|
|
UserId: res.UserId,
|
|
|
|
|
TokenId: token.value,
|
|
|
|
|
Status: 1,
|
|
|
|
|
Service: 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.http.post<Tokenn>(`${geturl}`, body, { headers }).subscribe(data => {
|
2022-12-19 19:20:45 +01:00
|
|
|
//console.log(data)
|
2022-04-01 09:06:57 +01:00
|
|
|
}, (error) => {
|
2022-12-19 19:20:45 +01:00
|
|
|
//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
|
|
|
}
|
|
|
|
|
|
2021-11-05 07:49:01 +01:00
|
|
|
registrationError() {
|
2022-04-01 09:06:57 +01:00
|
|
|
PushNotifications.addListener('registrationError',
|
|
|
|
|
(error: any) => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
}
|
|
|
|
|
);
|
2021-11-05 07:49:01 +01:00
|
|
|
}
|
2021-11-03 12:21:19 +01:00
|
|
|
|
2021-11-05 07:49:01 +01:00
|
|
|
onReciveForeground() {
|
2022-04-01 09:06:57 +01:00
|
|
|
PushNotifications.addListener('pushNotificationReceived',
|
|
|
|
|
(notification: PushNotificationSchema) => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
this.DataArray.push(notification)
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
this.storageService.store("Notifications", this.DataArray)
|
|
|
|
|
this.eventtrigger.publishSomeData({
|
|
|
|
|
notification: "recive"
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-11-05 07:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onReciveBackground() {
|
2022-04-01 09:06:57 +01:00
|
|
|
PushNotifications.addListener('pushNotificationActionPerformed',
|
|
|
|
|
(notification: ActionPerformed) => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
/* this.DataArray.push(notification.notification)
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-04-01 09:06:57 +01:00
|
|
|
this.storageService.store("Notifications", this.DataArray)
|
|
|
|
|
this.eventtrigger.publishSomeData({
|
|
|
|
|
notification: "recive"
|
|
|
|
|
}) */
|
|
|
|
|
this.notificatinsRoutes(notification)
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-11-05 07:49:01 +01:00
|
|
|
}
|
2021-11-03 12:21:19 +01:00
|
|
|
|
|
|
|
|
tempClearArray() {
|
2021-11-05 07:49:01 +01:00
|
|
|
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
|
|
|
|
2021-11-05 07:49:01 +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
|
|
|
}
|