Push notifications with capacitor runing great

This commit is contained in:
Eudes Inácio
2021-11-05 07:49:01 +01:00
parent 84f2c1cc36
commit 1d58ef0615
12 changed files with 200 additions and 131 deletions
+129 -81
View File
@@ -6,7 +6,7 @@ import { HttpClient } 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 { Tokenn } from '../models/token.model';
import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular';
import { NavigationExtras, Router } from '@angular/router';
import { ToastService } from '../services/toast.service';
@@ -14,9 +14,7 @@ import { BackgroundService } from './background.service';
import { v4 as uuidv4 } from 'uuid';
import { EventTrigger } from '../services/eventTrigger.service';
import { SessionStore } from '../store/session.service';
//import { FCM } from '@ionic-native/fcm/ngx';
//import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
@Injectable({
providedIn: 'root'
@@ -82,80 +80,130 @@ export class NotificationsService {
getTokenByUserIdAndId(user, userID) {
const geturl = environment.apiURL + 'notifications/user/' + userID;
return this.http.get<Token[]>(`${geturl}`);
return this.http.get<Tokenn[]>(`${geturl}`);
}
/* getAndpostToken(username) {
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
}
});
}
getAndpostToken(username) {
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
};
return this.fcm.getToken().then(token => {
console.log('token: ', token)
this.storageService.store(username, token);
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,
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)
})
});
this.http.post<Token>(`${geturl}`, body, { headers }).subscribe(data => {
console.log('TOKEN USER MIDLE', data);
})
});
});
});
}
);
}
}
async onReceviNotification() {
this.fcm.onNotification().subscribe(data => {
if (data.click_action) {
console.log("Received in background: ", data);
registrationError() {
PushNotifications.addListener('registrationError',
(error: any) => {
console.log('Error on registration: ' + JSON.stringify(error));
}
);
}
this.notificatinsRoutes(data)
} else {
console.log("Received in foreground: ", data);
console.log(data.Service)
console.log(data.Object)
console.log(data.IdObject)
this.DataArray.push(data)
onReciveForeground() {
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
console.log('Push received: ' + JSON.stringify(notification));
this.DataArray.push(notification)
console.log("On ReceiveNotification", this.DataArray)
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.notificatinsRoutes(notification)
}
);
}
};
});
} */
/*
async onReceviNotification() {
this.fcm.onNotification().subscribe(data => {
if (data.click_action) {
console.log("Received in background: ", data);
this.notificatinsRoutes(data)
} else {
console.log("Received in foreground: ", data);
console.log(data.Service)
console.log(data.Object)
console.log(data.IdObject)
this.DataArray.push(data)
console.log("On ReceiveNotification", this.DataArray)
this.storageService.store("Notifications", this.DataArray)
};
});
} */
/////////////////////////////////////////////////////
/* getTokenByUserIdAndId(user, userID) {
const geturl = environment.apiURL + 'notifications/user/' + userID;
return this.http.get<Token[]>(`${geturl}`);
}
*/
*/
/* getAndpostToken(username) {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
//console.log('Notifications not supported')
} else {
const geturl = environment.apiURL + 'notifications/token';
if(window['WLAuthorizationManager']) {
if(window['WLAuthorizationManager'].obtainAccessToken) {
@@ -207,17 +255,17 @@ export class NotificationsService {
);
}
}
}
} */
tempClearArray() {
this.DataArray = [];
this.DataArray = new Array;
}
/* async onReceviNotification() {
if(window['WLAuthorizationManager']) {
if(window['WLAuthorizationManager'].obtainAccessToken) {
window['WLAuthorizationManager'].obtainAccessToken("push.mobileclient").then(
@@ -244,7 +292,7 @@ export class NotificationsService {
notification: "recive"
})
var data = JSON.parse(message.payload);
//synchro.$send(data)
console.log('data.Service', data.Service); // module
@@ -264,7 +312,7 @@ export class NotificationsService {
e.funx()
}
})
}
}, (error) => {
console.log('Push notification recived: failure ' + error.responseText);
@@ -273,54 +321,54 @@ export class NotificationsService {
);
}
}
} */
notificatinsRoutes = (data) => {
if (data.Service === "agenda") {
this.zone.run(() => this.router.navigate(['/home/agenda', data.IdObject, 'agenda']));
notificatinsRoutes = (notification) => {
if (notification.data.Service === "agenda") {
this.zone.run(() => this.router.navigate(['/home/agenda', notification.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 (notification.data.Service === "gabinete-digital" && notification.data.Object === "expediente") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expediente', notification.data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "agenda" && data.Object === "event-list") {
this.zone.run(() => this.router.navigate(['/home/agenda/event-list/approve-event', data.IdObject, 'agenda']));
} else if (data.Service === "gabinete-digital" && data.Object === "despachos") {
else if (notification.data.Service === "agenda" && notification.data.Object === "event-list") {
this.zone.run(() => this.router.navigate(['/home/agenda/event-list/approve-event', notification.data.IdObject, 'agenda']));
} else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "despachos") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos', data.IdObject, 'gabinete-digital'], { replaceUrl: true }));
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos', notification.data.IdObject, 'gabinete-digital'], { replaceUrl: true }));
}
else if (data.Service === "gabinete-digital" && data.Object === "parecer") {
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "parecer") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', data.IdObject, 'gabinete-digital']));
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "gabinete-digital" && data.Object === "deferimento") {
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "deferimento") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', data.IdObject, 'gabinete-digital']));
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos', notification.data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "gabinete-digital" && data.Object === "despachos-pr") {
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "despachos-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos-pr', data.IdObject, 'gabinete-digital']));
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos-pr', notification.data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "accoes" && data.Object === "accao") {
this.zone.run(() => this.router.navigate(['/home/publications', data.IdObject]));
else if (notification.data.Service === "accoes" && notification.data.Object === "accao") {
this.zone.run(() => this.router.navigate(['/home/publications', notification.data.IdObject]));
}
else if (data.Service === "accoes" && data.Object === "publicacao") {
this.zone.run(() => this.router.navigate(['/home/publications/view-publications', data.FolderId, data.IdObject]));
else if (notification.data.Service === "accoes" && notification.data.Object === "publicacao") {
this.zone.run(() => this.router.navigate(['/home/publications/view-publications', notification.data.FolderId, notification.data.IdObject]));
}
else if (data.Service === "gabinete-digital" && data.Object === "diplomas") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', data.IdObject, 'gabinete-digital']));
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "diplomas") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', notification.data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "gabinete-digital" && data.Object === "diplomas-assinar") {
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "diplomas-assinar") {
let navigationExtras: NavigationExtras = {
queryParams: {
"serialNumber": data.IdObject,
"serialNumber": notification.data.IdObject,
}
};
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas-assinar/diploma-assinar'], navigationExtras));
}
else if (data.Service === "gabinete-digital" && data.Object === "expedientes-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expedientes-pr', data.IdObject, 'gabinete-digital']));
else if (notification.data.Service === "gabinete-digital" && notification.data.Object === "expedientes-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expedientes-pr', notification.data.IdObject, 'gabinete-digital']));
}
}