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

295 lines
11 KiB
TypeScript
Raw Normal View History

2021-07-08 14:07:33 +01:00
/* eslint-disable */
2021-07-08 14:45:38 +01:00
/* tslint:disable */
2023-06-26 11:12:57 +01:00
import { EventEmitter, Injectable, NgZone } from '@angular/core';
2023-01-30 15:08:00 +01:00
import { HttpClient, HttpHeaders, HttpParams } 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';
2023-02-02 18:24:26 +01:00
import { NavigationExtras, 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';
2023-02-01 11:08:41 +01:00
import { v4 as uuidv4 } from 'uuid'
2023-02-03 19:37:47 +01:00
import { Capacitor } from '@capacitor/core';
2023-06-26 11:12:57 +01:00
import { AngularFireMessaging } from '@angular/fire/messaging';
import { Subject } from 'rxjs';
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
2023-02-06 15:34:46 +01:00
callbacks: {
[key: string]: {
type: notificationObject,
funx: Function
id: string
}
} = {}
2021-08-25 11:51:02 +01:00
2023-01-03 21:08:49 +01:00
active = false
2023-02-03 19:37:47 +01:00
isPushNotificationsAvailable = Capacitor.isPluginAvailable('PushNotifications');
2023-06-26 11:12:57 +01:00
notificationReceived: EventEmitter<void> = new EventEmitter<void>();
2023-01-03 21:08:49 +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,
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-06-26 11:12:57 +01:00
private eventtrigger: EventTrigger,
private afMessaging: AngularFireMessaging,) {
2021-08-25 11:51:02 +01:00
2023-02-01 11:08:41 +01:00
}
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-02-03 19:37:47 +01:00
2023-06-26 11:12:57 +01:00
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
2023-02-03 19:37:47 +01:00
}
2023-06-26 11:12: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
}
});
} else {
}
2021-02-01 13:31:33 +01:00
}
getAndpostToken(username) {
2023-02-03 19:37:47 +01:00
const geturl = environment.apiURL + 'notifications/token';
2023-06-26 11:12:57 +01:00
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
2023-02-03 19:37:47 +01:00
}
2023-02-06 15:34:46 +01:00
2023-06-26 11:12:57 +01:00
PushNotifications.addListener('registration',
(token: Token) => {
this.postToken(token.value, geturl)
}
);
} else {
this.afMessaging.requestToken.subscribe(
(token) => {
// Save the token to your server for sending notifications
console.log('Permission granted! Token:', token);
this.postToken(token, geturl)
},
(error) => {
console.error('Permission denied:', error);
}
);
}
}
2021-11-03 12:21:19 +01:00
2023-06-26 11:12:57 +01:00
postToken(token, geturl) {
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const body = {
UserId: SessionStore.user.UserId,
TokenId: token,
Status: 1,
Service: 1
};
this.http.post<Tokenn>(`${geturl}`, body, { headers }).subscribe(data => {
this.active = true
}, (error) => {
})
2021-02-01 13:31:33 +01:00
}
registrationError() {
2023-02-06 15:34:46 +01:00
if (!this.isPushNotificationsAvailable) {
return false
2023-02-03 19:37:47 +01:00
}
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-02-03 19:37:47 +01:00
2023-06-26 11:12:57 +01:00
if(this.platform.is('mobile')) {
2023-05-26 14:23:37 +01:00
2023-06-26 11:12:57 +01:00
if (!this.isPushNotificationsAvailable) {
return false
}
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
this.active = true
2023-07-26 09:47:31 +01:00
console.log('NOtification Listener', notification)
2023-06-26 11:12:57 +01:00
this.storenotification(notification)
this.eventtrigger.publishSomeData({
notification: "recive"
})
}
);
} else {
this.afMessaging.messages.subscribe((notification) => {
this.storenotification(notification)
this.notificationReceived.emit();
this.eventtrigger.publishSomeData({
notification: "recive"
})
// Handle the received message, e.g., show a notification
});
}
}
2023-02-06 15:34:46 +01:00
2023-06-26 11:12:57 +01:00
storenotification(notification) {
2023-07-26 09:47:31 +01:00
console.log('Store Notification ',notification)
2023-06-26 11:12:57 +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])
2023-02-06 15:34:46 +01:00
}
2023-06-26 11:12:57 +01:00
})
}
onReciveBackground() {
2023-02-06 15:34:46 +01:00
if (!this.isPushNotificationsAvailable) {
return false
2023-02-03 19:37:47 +01:00
}
2023-02-06 15:34:46 +01:00
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
2023-01-24 15:56:47 +01:00
this.active = true
2023-07-26 09:47:31 +01:00
console.log('NOtification Listener Backgroud', notification)
2023-01-24 15:56:47 +01:00
/* 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"
}) */
2023-02-06 15:34:46 +01:00
this.notificatinsRoutes(notification)
// this.runNotificationCallback(notification)
}
);
}
2021-11-03 12:21:19 +01:00
2023-02-06 15:34:46 +01:00
ChatSendMessageNotification(userID, title, bodymsg, roomId) {
2023-02-03 14:40:09 +01:00
const geturl = environment.apiURL + `notifications/sendbyUsername`;
2023-01-30 15:08:00 +01:00
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
2023-02-06 15:34:46 +01:00
const message = {
"Service": "chat",
"IdObject": roomId
}
let id = 437
this.http.post<Tokenn>(geturl + `?username=${userID}&title=${title}&body=${bodymsg}`, message, { headers }).subscribe(data => {
this.active = true
}, (error) => {
2023-05-26 14:23:37 +01:00
2023-02-06 15:34:46 +01:00
})
2023-02-03 14:40:09 +01:00
}
2023-02-06 15:34:46 +01:00
ChatSendMessageNotificationGrup(usersID, title, bodymsg, roomId) {
const geturl = environment.apiURL + `notifications/sendByUsernames`;
2023-02-03 14:40:09 +01:00
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
2023-02-06 15:34:46 +01:00
const message = {
"Users": usersID,
"NotificationData": {
"Service": "chat",
"IdObject": roomId
}
}
2023-05-26 14:23:37 +01:00
2023-02-06 15:34:46 +01:00
this.http.post<Tokenn>(geturl + `?title=${title}&body=${bodymsg}`, message, { headers }).subscribe(data => {
this.active = true
}, (error) => {
2023-05-26 14:23:37 +01:00
2023-02-06 15:34:46 +01:00
})
2023-01-30 15:08:00 +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
2023-02-06 15:34:46 +01:00
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']));
}
2023-06-11 13:54:53 +01:00
else if (notification.notification.data.Service === "gabinete-digital" && notification.notification.data.Object === "diploma-revisao") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', notification.notification.data.IdObject, 'gabinete-digital']));
}
2023-02-06 15:34:46 +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']));
} else if (notification.notification.data.Service === "chat") {
2023-02-02 18:24:26 +01:00
let navigationExtras: NavigationExtras = { queryParams: { "roomId": notification.notification.data.IdObject, } };
this.zone.run(() => this.router.navigate(['/home/chat'], navigationExtras));
2023-01-30 15:08:00 +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
}