2021-06-25 09:35:50 +01:00
|
|
|
///<reference path="../../../plugins/cordova-plugin-mfp-push/typings/mfppush.d.ts" />
|
|
|
|
|
import { Injectable, NgZone } from '@angular/core';
|
2021-02-01 13:31:33 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
|
|
|
|
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-02-01 13:31:33 +01:00
|
|
|
import { Token } from '../models/token.model';
|
2021-04-12 15:39:44 +01:00
|
|
|
import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular';
|
2021-06-25 09:35:50 +01:00
|
|
|
import { NavigationExtras,Router } from '@angular/router';
|
|
|
|
|
import { ToastService } from '../services/toast.service';
|
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;
|
|
|
|
|
|
|
|
|
|
constructor(
|
2021-04-12 15:39:44 +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,
|
|
|
|
|
private router: Router,
|
|
|
|
|
private toastService: ToastService,
|
|
|
|
|
private zone: NgZone) { }
|
2021-02-01 13:31:33 +01:00
|
|
|
|
|
|
|
|
getTokenByUserIdAndId(user, userID) {
|
|
|
|
|
const geturl = environment.apiURL + 'notifications/user/' + userID;
|
|
|
|
|
|
|
|
|
|
return this.http.get<Token[]>(`${geturl}`);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
getAndpostToken(username) {
|
2021-06-25 14:37:52 +01:00
|
|
|
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
|
2021-06-25 09:35:50 +01:00
|
|
|
console.log('Notifications not supported')
|
2021-04-12 15:39:44 +01:00
|
|
|
} else {
|
2021-04-12 08:34:17 +01:00
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
const geturl = environment.apiURL + 'notifications/token';
|
|
|
|
|
|
|
|
|
|
WLAuthorizationManager.obtainAccessToken("push.mobileclient").then(
|
|
|
|
|
(token) => {
|
|
|
|
|
console.log('Push Notification: Success ' + token);
|
|
|
|
|
|
|
|
|
|
MFPPush.initialize(
|
|
|
|
|
function (successResponse) {
|
|
|
|
|
console.log("Push notification Successfully Service intialized: " + successResponse);
|
|
|
|
|
},
|
|
|
|
|
function (failureResponse) {
|
|
|
|
|
console.log("Push notification failure Service intialized: " + failureResponse);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
MFPPush.registerDevice(null, (successResponse) => {
|
|
|
|
|
console.log("Successfully registered: " + JSON.stringify(successResponse));
|
|
|
|
|
console.log('token: ', successResponse.deviceId)
|
|
|
|
|
this.storageService.store(username, successResponse.deviceId);
|
|
|
|
|
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': 'Basic cGF1bG8ucGludG9AZ2FiaW5ldGVkaWdpdGFsLmxvY2FsOnRhYnRlc3RlQDAwNg==' };
|
|
|
|
|
const body = {
|
|
|
|
|
UserId: res.UserId,
|
|
|
|
|
TokenId: successResponse.deviceId,
|
|
|
|
|
Status: 1,
|
|
|
|
|
Service: 1
|
|
|
|
|
};
|
|
|
|
|
this.http.post<Token>(`${geturl}`, body, { headers }).subscribe(data => {
|
|
|
|
|
console.log('TOKEN USER MIDLE', data);
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (failureResponse) {
|
|
|
|
|
console.log("Successfully failue: " + JSON.stringify(failureResponse));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}, (error) => {
|
|
|
|
|
console.log('Push notification recived: failure ' + error.responseText);
|
|
|
|
|
console.log(JSON.stringify(error));
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-04-12 08:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
async onReceviNotification() {
|
|
|
|
|
WLAuthorizationManager.obtainAccessToken("push.mobileclient").then(
|
|
|
|
|
(token) => {
|
|
|
|
|
console.log('Push Notification: Success ' + token);
|
|
|
|
|
|
|
|
|
|
MFPPush.initialize(
|
|
|
|
|
function (successResponse) {
|
|
|
|
|
console.log("Push notification Successfully intialized: " + successResponse);
|
|
|
|
|
MFPPush.registerNotificationsCallback(notificationReceived);
|
|
|
|
|
},
|
|
|
|
|
function (failureResponse) {
|
|
|
|
|
console.log("Push notification failure intialized: " + failureResponse);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
var notificationReceived = (message) => {
|
|
|
|
|
console.log(message);
|
|
|
|
|
var data = JSON.parse(message.payload);
|
|
|
|
|
console.log(data.Service);
|
|
|
|
|
console.log(data.IdObject);
|
|
|
|
|
console.log(data.Object);
|
|
|
|
|
|
|
|
|
|
if(message.actionName){
|
|
|
|
|
this.notificatinsRoutes(data);
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.notificationMessage(message.alert,this.notificatinsRoutes, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, (error) => {
|
|
|
|
|
console.log('Push notification recived: failure ' + error.responseText);
|
|
|
|
|
console.log(JSON.stringify(error));
|
2021-04-12 08:34:17 +01:00
|
|
|
}
|
2021-06-25 09:35:50 +01:00
|
|
|
);
|
2021-02-01 13:31:33 +01:00
|
|
|
}
|
2021-04-12 08:34:17 +01:00
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
notificatinsRoutes = (data) => {
|
|
|
|
|
if (data.Service === "agenda") {
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/agenda', data.IdObject, 'agenda']));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Service === "gabinete-digital" && data.Object === "expediente") {
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expediente',data.IdObject,'gabinete-digital']));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Service === "gabinete-digital" && data.Object === "event-list") {
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/event-list/approve-event',data.IdObject, 'gabinete-digital']));
|
2021-06-25 14:37:52 +01:00
|
|
|
}else if (data.Service === "gabinete-digital" && data.Object === "despachos") {
|
|
|
|
|
let navigationExtras: NavigationExtras = {
|
|
|
|
|
queryParams: {
|
|
|
|
|
"serialNumber": data.IdObject,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos/despacho'], navigationExtras));
|
2021-06-25 09:35:50 +01:00
|
|
|
}
|
|
|
|
|
else if (data.Service === "gabinete-digital" && data.Object === "parecer") {
|
|
|
|
|
let navigationExtras: NavigationExtras = {
|
|
|
|
|
queryParams: {
|
|
|
|
|
enterAnimation: "",
|
|
|
|
|
serialNumber: data.IdObject,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos/pedido'], navigationExtras));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Service === "gabinete-digital" && data.Object === "deferimento") {
|
|
|
|
|
let navigationExtras: NavigationExtras = {
|
|
|
|
|
queryParams: {
|
|
|
|
|
enterAnimation: "",
|
|
|
|
|
serialNumber: data.IdObject,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos/pedido'], navigationExtras));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Service === "accoes" && data.Object === "accao") {
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/publications',data.IdObject]));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Service === "accoes" && data.Object === "publicacao") {
|
|
|
|
|
this.zone.run(() => this.router.navigate(['/home/publications/view-publications',data.FolderId,data.IdObject]));
|
|
|
|
|
}
|
2021-04-12 08:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
platformVerify() {
|
2021-04-12 08:34:17 +01:00
|
|
|
|
2021-06-25 09:35:50 +01:00
|
|
|
if (this.platform.is('cordova')) {
|
|
|
|
|
} else {
|
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
|
|
|
}
|