This commit is contained in:
Peter Maquiran
2023-09-19 10:21:23 +01:00
parent 527cc0f2a6
commit f51bd246fc
29 changed files with 754 additions and 261 deletions
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { NotificationsEndsPointsService } from './notifications-ends-points.service';
describe('NotificationsEndsPointsService', () => {
let service: NotificationsEndsPointsService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NotificationsEndsPointsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,40 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SessionStore } from 'src/app/store/session.service';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class NotificationsEndsPointsService {
constructor(
private http: HttpClient,
) { }
postToken(token) {
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const body = {
UserId: SessionStore.user.UserId,
TokenId: token,
Status: 1,
Service: 1
};
return this.http.post<Token>(`${environment.apiURL}+ 'notifications/token'`, body, { headers })
}
DeleteToken(token) {
const geturl = environment.apiURL + `notifications/token?userId=${SessionStore.user.UserId}&tokenId=${token}`;
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const body = {};
return this.http.delete<Token>(`${geturl}`, { headers, body })
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { NotificationsStoreService } from './notifications-store.service';
describe('NotificationsStoreService', () => {
let service: NotificationsStoreService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NotificationsStoreService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,9 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class NotificationsStoreService {
constructor() { }
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { NotificationsTriggerService } from './notifications-trigger.service';
describe('NotificationsTriggerService', () => {
let service: NotificationsTriggerService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NotificationsTriggerService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,70 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SessionStore } from 'src/app/store/session.service';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class NotificationsTriggerService {
constructor(
private http: HttpClient,
) { }
sendNotificationWithSend(userID, title, bodymsg, roomId) {
const geturl = environment.apiURL + `notifications/send`;
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const message = {
"Service": "chat",
"IdObject": roomId
}
let id = 437
this.http.post<Token>(geturl + `?userId=${userID}&title=${title}&body=${bodymsg}`, message, { headers }).subscribe(data => {
}, (error) => {
})
}
ChatSendMessageNotification(userID, title, bodymsg, roomId) {
const geturl = environment.apiURL + `notifications/sendbyUsername`;
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const message = {
"Service": "chat",
"IdObject": roomId
}
let id = 437
this.http.post<Token>(geturl + `?username=${userID}&title=${title}&body=${bodymsg}`, message, { headers }).subscribe(data => {
// this.active = true
}, (error) => {
})
}
ChatSendMessageNotificationGrup(usersID, title, bodymsg, roomId) {
const geturl = environment.apiURL + `notifications/sendByUsernames`;
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const message = {
"Users": usersID,
"NotificationData": {
"Service": "chat",
"IdObject": roomId
}
}
this.http.post<Token>(geturl + `?title=${title}&body=${bodymsg}`, message, { headers }).subscribe(data => {
// this.active = true
}, (error) => {
})
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { NotificationsService } from './notifications.service';
describe('NotificationsService', () => {
let service: NotificationsService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NotificationsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,200 @@
import { Injectable, NgZone } from '@angular/core';
import { AlertController, Platform } from '@ionic/angular';
import { Capacitor } from '@capacitor/core';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { environment } from 'src/environments/environment';
import { NotificationsEndsPointsService } from './notifications-ends-points.service'
import { AngularFireMessaging } from '@angular/fire/messaging';
import { NavigationExtras, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
isPushNotificationsAvailable = Capacitor.isPluginAvailable('PushNotifications');
active = false
constructor(
private platform: Platform,
private NotificationsEndsPointsService: NotificationsEndsPointsService,
private afMessaging: AngularFireMessaging,
private router: Router,
private zone: NgZone,
) { }
requestPermissions() {
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
}
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 {}
}
getToken(): Promise<string> {
return new Promise((resolve, reject) => {
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
}
PushNotifications.addListener('registration',
(token: Token) => {
resolve(token.value)
}
);
} else {
this.afMessaging.requestToken.subscribe(
(token) => {
resolve(token)
},
(error) => {
console.error('Permission denied:', error);
}
);
}
})
}
async registerToken(username) {
const token = await this.getToken()
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
}
this.NotificationsEndsPointsService.postToken(token)
} else {
this.afMessaging.requestToken.subscribe(
(token) => {
// Save the token to your server for sending notifications
console.log('Permission granted! Token:', token);
this.NotificationsEndsPointsService.postToken(token)
},
(error) => {
console.error('Permission denied:', error);
}
);
}
}
onReciveBackground() {
if(this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
}
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
this.active = true
console.log('NOtification Listener Backgroud', notification)
/* this.DataArray.push(notification.notification)
this.storageService.store("Notifications", this.DataArray)
this.eventtrigger.publishSomeData({
notification: "recive"
}) */
this.notificatinsRoutes(notification)
// this.runNotificationCallback(notification)
}
);
} else {
navigator.serviceWorker.onmessage = (event) => {
console.log('Mensagem recebida do Service Worker:', event.data.data);
let object = {
notification: event.data
}
if (event.data.notificationClicked) {
console.log('Notificação push do Firebase clicada em segundo plano!');
this.notificatinsRoutes(object)
}
};
}
}
registrationError() {
if (!this.isPushNotificationsAvailable) {
return false
}
PushNotifications.addListener('registrationError',
(error: any) => {
this.active = false
}
);
}
notificatinsRoutes = (notification) => {
console.log('BACK BACK',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 === "expedientes") {
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 === "diploma-revisao") {
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 === "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") {
let navigationExtras: NavigationExtras = { queryParams: { "roomId": notification.notification.data.IdObject, } };
this.zone.run(() => this.router.navigate(['/home/chat'], navigationExtras));
}
}
}