Firebase web notifications added

This commit is contained in:
Eudes Inácio
2023-06-26 11:12:57 +01:00
parent 2178ad88c2
commit b9bd42e138
11 changed files with 253 additions and 503 deletions
+92 -91
View File
@@ -1,6 +1,6 @@
/* eslint-disable */
/* tslint:disable */
import { Injectable, NgZone } from '@angular/core';
import { EventEmitter, 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';
@@ -14,6 +14,8 @@ import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } fr
import { notificationObject } from '../models/notifications';
import { v4 as uuidv4 } from 'uuid'
import { Capacitor } from '@capacitor/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
@@ -34,6 +36,7 @@ export class NotificationsService {
active = false
isPushNotificationsAvailable = Capacitor.isPluginAvailable('PushNotifications');
notificationReceived: EventEmitter<void> = new EventEmitter<void>();
constructor(
private http: HttpClient,
@@ -42,44 +45,11 @@ export class NotificationsService {
private platform: Platform,
private router: Router,
private zone: NgZone,
private eventtrigger: EventTrigger,) {
private eventtrigger: EventTrigger,
private afMessaging: AngularFireMessaging,) {
}
// registerCallback({type, funx, id = uuidv4()} : {
// type: notificationObject,
// funx: Function
// id?: string
// }) {
// this.callbacks[id] = { funx, id, type}
// return id;
// }
// private async runNotificationCallback(notification) {
// for (const [key, value] of Object.entries(this.callbacks)) {
// if(value.type == notification.data.Object) {
// const dontRepeat = await value.funx(notification)
// if(dontRepeat) {
// delete this.callbacks[key]
// }
// } else if (value.type == '*') {
// const dontRepeat = await value.funx(notification)
// if(dontRepeat) {
// delete this.callbacks[key]
// }
// }
// }
// }
getTokenByUserIdAndId(user, userID) {
// const geturl = environment.apiURL + 'notifications/user/' + userID;
@@ -87,46 +57,63 @@ export class NotificationsService {
}
requestPermissions() {
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
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 {
}
}
getAndpostToken(username) {
if (!this.isPushNotificationsAvailable) {
return false
const geturl = environment.apiURL + 'notifications/token';
if (this.platform.is('mobile')) {
if (!this.isPushNotificationsAvailable) {
return false
}
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);
}
);
}
const geturl = environment.apiURL + 'notifications/token';
PushNotifications.addListener('registration',
(token: Token) => {
const headers = { 'Authorization': SessionStore.user.BasicAuthKey };
const body = {
UserId: SessionStore.user.UserId,
TokenId: token.value,
Status: 1,
Service: 1
};
this.http.post<Tokenn>(`${geturl}`, body, { headers }).subscribe(data => {
this.active = true
}, (error) => {
})
}
);
}
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) => {
})
}
registrationError() {
@@ -142,33 +129,47 @@ export class NotificationsService {
}
onReciveForeground() {
if (!this.isPushNotificationsAvailable) {
return false
}
if(this.platform.is('mobile')) {
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
this.active = true
console.log(notification)
this.storageService.get("Notifications").then((store) => {
store.push(notification)
this.storageService.store("Notifications", store)
}).catch((error) => {
if (!error) {
this.storageService.store("Notifications", [notification])
}
})
// this.runNotificationCallback(notification)
/* this.eventTriger.publish('notificatioRecive') */
this.eventtrigger.publishSomeData({
notification: "recive"
})
if (!this.isPushNotificationsAvailable) {
return false
}
);
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
this.active = true
this.storenotification(notification)
this.eventtrigger.publishSomeData({
notification: "recive"
})
}
);
} else {
this.afMessaging.messages.subscribe((notification) => {
this.storenotification(notification)
this.storenotification(notification)
this.notificationReceived.emit();
this.eventtrigger.publishSomeData({
notification: "recive"
})
// Handle the received message, e.g., show a notification
});
}
}
storenotification(notification) {
console.log(notification)
this.storageService.get("Notifications").then((store) => {
store.push(notification)
this.storageService.store("Notifications", store)
}).catch((error) => {
if (!error) {
this.storageService.store("Notifications", [notification])
}
})
}
onReciveBackground() {