From d4d3f3eb763d7003231cebb6dcd00391c2a12dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eudes=20In=C3=A1cio?= Date: Mon, 28 Jun 2021 13:36:08 +0100 Subject: [PATCH] Webnotification service --- src/app/home/home.page.ts | 22 +- .../services/webnotifications.service.spec.ts | 16 ++ src/app/services/webnotifications.service.ts | 230 ++++++++++++++++++ 3 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 src/app/services/webnotifications.service.spec.ts create mode 100644 src/app/services/webnotifications.service.ts diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index ed31cbc98..1675bf554 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -5,10 +5,11 @@ import { EventsService } from '../services/events.service'; import { Event } from '../models/event.model'; import { ProcessesService } from '../services/processes.service'; import { NotificationsService } from '../services/notifications.service'; +import { WebNotificationsService } from '../services/webnotifications.service'; import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular'; import { NavigationExtras,Router } from '@angular/router'; import { ToastService } from '../services/toast.service'; -import MFPPush from 'ibm-mfp-web-push'; + @Component({ selector: 'app-home', @@ -54,6 +55,7 @@ export class HomePage implements OnInit { private toastService: ToastService, private animationController: AnimationController, private notificatinsservice: NotificationsService, + private webnotificatinsservice: WebNotificationsService, private platform: Platform) { router.events.subscribe((val) => { @@ -68,14 +70,8 @@ export class HomePage implements OnInit { ngOnInit() { if(this.platform.is('desktop') || this.platform.is('mobileweb')) { - WL.Client.init(this.wlInitOptions) - - /* var pushInitOptions = { - appId: "com.gpr.gabinetedigital", - serverUrl:"https://mfp-server-url.com", - safariWebsitePushId:"web.com.mfp-server-url" - }; - MFPPush.initialize(pushInitOptions); */ + this.webnotificatinsservice.webconnection(); + this.webnotificatinsservice.onReceviNotificationWeb(); } else { this.mobilefirstConnect(); this.notificatinsservice.onReceviNotification(); @@ -143,11 +139,9 @@ export class HomePage implements OnInit { - wlInitOptions = { - mfpContextRoot : '/mfp', // "mfp" is the default context root in the Mobile Foundation - applicationId : 'com.gpr.gabinetedigital', // Replace with your own value. - sessionMode : true //This is an optional paramter. Setting this to true ensures that MFP related data is stored in the session rather than in the local storage. If this option is set to false or not set at all, default is local storage. -}; + + + diff --git a/src/app/services/webnotifications.service.spec.ts b/src/app/services/webnotifications.service.spec.ts new file mode 100644 index 000000000..5af6818b3 --- /dev/null +++ b/src/app/services/webnotifications.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { WebNotificationsService } from './webnotifications.service'; + +describe('WebNotificationsService', () => { + let service: WebNotificationsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(WebNotificationsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/webnotifications.service.ts b/src/app/services/webnotifications.service.ts new file mode 100644 index 000000000..265e88ef6 --- /dev/null +++ b/src/app/services/webnotifications.service.ts @@ -0,0 +1,230 @@ +import { Injectable, NgZone } from '@angular/core'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { environment } from 'src/environments/environment'; +import { StorageService } from 'src/app/services/storage.service'; +import { AuthConnstants } from 'src/app/config/auth-constants'; +import { Token } from '../models/token.model'; +import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular'; +import { NavigationExtras,Router } from '@angular/router'; +import { ToastService } from './toast.service'; +import MFPPush from 'ibm-mfp-web-push'; + +@Injectable({ + providedIn: 'root' +}) +export class WebNotificationsService { + + adding: "intervenient" | "CC" = "intervenient"; + folderId: string; + + constructor( + private http: HttpClient, + private storageService: StorageService, + private modalController: ModalController, + public modalCtrl: AlertController, + private animationController: AnimationController, + private platform: Platform, + private router: Router, + private toastService: ToastService, + private zone: NgZone) { } + + + webconnection() { + WL.Client.init(this.wlInitOptions) + + var pushInitOptions = { + appId: "com.gpr.gabinetedigital", + serverUrl:"http://gpr-dev-10.gabinetedigital.local:9080/", + //safariWebsitePushId:"web.com.mfp-server-url" + }; + MFPPush.initialize(pushInitOptions); + } + + wlInitOptions = { + mfpContextRoot : '/mfp', // "mfp" is the default context root in the Mobile Foundation + applicationId : 'com.gpr.gabinetedigital', // Replace with your own value. + sessionMode : true //This is an optional paramter. Setting this to true ensures that MFP related data is stored in the session rather than in the local storage. If this option is set to false or not set at all, default is local storage. +}; + +async onReceviNotificationWeb() { + 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)); + } + ); +} + + /* getTokenByUserIdAndId(user, userID) { + const geturl = environment.apiURL + 'notifications/user/' + userID; + + return this.http.get(`${geturl}`); + } */ + + /* getAndpostToken(username) { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + console.log('Notifications not supported') + } else { + + 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(`${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)); + } + ); + } + + } */ +/* + 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)); + } + ); + } */ + + /* 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'])); + }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)); + } + 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])); + } + } + + platformVerify() { + + if (this.platform.is('cordova')) { + } else { + } + } */ + +}