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

227 lines
8.9 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-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';
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';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-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
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,
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-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) {
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
}
});
2021-02-01 13:31:33 +01:00
}
getAndpostToken(username) {
2021-11-03 12:21:19 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
console.log('Notifications not supported')
} else {
const geturl = environment.apiURL + 'notifications/token';
PushNotifications.addListener('registration',
(token: Token) => {
console.log('token: ', token.value)
this.storageService.store(username, token.value);
this.storageService.get(username).then(value => {
console.log('STORAGE TOKEN', value)
this.storageService.get(AuthConnstants.USER).then(res => {
console.log('USERID', res);
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 => {
console.log('TOKEN USER MIDLE', data);
}, (error) => {
console.log('Post token to backend', error)
})
});
2021-11-03 12:21:19 +01:00
});
}
);
}
2021-11-03 12:21:19 +01:00
2021-02-01 13:31:33 +01:00
}
registrationError() {
PushNotifications.addListener('registrationError',
(error: any) => {
console.log('Error on registration: ' + JSON.stringify(error));
}
);
}
2021-11-03 12:21:19 +01:00
onReciveForeground() {
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
console.log('Push received: ' + JSON.stringify(notification));
this.DataArray.push(notification)
2021-11-03 12:21:19 +01:00
console.log("On ReceiveNotification", this.DataArray)
2021-11-04 06:39:39 +01:00
this.storageService.store("Notifications", this.DataArray)
this.eventtrigger.publishSomeData({
notification: "recive"
})
}
);
}
onReciveBackground() {
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
console.log('Push action performed: ' + JSON.stringify(notification));
/* this.DataArray.push(notification.notification)
console.log("On ReceiveNotification", this.DataArray)
this.storageService.store("Notifications", this.DataArray)
this.eventtrigger.publishSomeData({
notification: "recive"
}) */
this.notificatinsRoutes(notification)
}
);
}
2021-11-03 12:21:19 +01:00
tempClearArray() {
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
console.log('Push action performed 2222: ' + JSON.stringify(notification.data));
if (notification.notification.data.Service === "agenda" && notification.notification.data.IdObject.length > 10) {
console.log("Id Lenght", notification.notification.data.IdObject.length)
this.zone.run(() => this.router.navigate(['/home/agenda', notification.notification.data.IdObject, 'agenda']));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
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']));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
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']));
2021-07-30 16:06:58 +01:00
2021-11-09 17:23:39 +01:00
} 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 }));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "parecer") {
2021-07-07 15:28:29 +01:00
2021-11-09 17:23:39 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.notification.data.IdObject, 'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "deferimento") {
2021-07-30 16:06:58 +01:00
2021-11-09 17:23:39 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.notification.data.IdObject, 'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "despachos-pr") {
2021-07-30 16:06:58 +01:00
2021-11-09 17:23:39 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos-pr', notification.notification.data.IdObject, 'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
else if (notification.notification.data.Service === "accoes" && notification.notification.data.Object === "accao") {
this.zone.run(() => this.router.navigate(['/home/publications', notification.notification.data.IdObject]));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
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]));
2021-06-25 09:35:50 +01:00
}
2021-11-09 17:23:39 +01:00
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']));
2021-07-06 16:17:03 +01:00
}
2021-11-09 17:23:39 +01:00
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']));
2021-07-06 16:17:03 +01:00
}
2021-11-09 17:23:39 +01:00
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-07-06 16:17:03 +01:00
}
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
}