mirror of
https://github.com/MikroWizard/mikrofront.git
synced 2025-12-28 21:09:31 +00:00
86 lines
No EOL
2.4 KiB
TypeScript
86 lines
No EOL
2.4 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { AngularFireDatabase } from '@angular/fire/database';
|
|
import { AngularFireAuth } from '@angular/fire/auth';
|
|
import { AngularFireMessaging } from '@angular/fire/messaging';
|
|
import { mergeMapTo } from 'rxjs/operators';
|
|
import { take } from 'rxjs/operators';
|
|
import { BehaviorSubject } from 'rxjs'
|
|
import { dataProvider } from './odoorpchttp/data';
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
@Injectable()
|
|
export class MessagingService {
|
|
|
|
currentMessage = new BehaviorSubject(null);
|
|
|
|
constructor(
|
|
private angularFireDB: AngularFireDatabase,
|
|
private angularFireAuth: AngularFireAuth,
|
|
private data_provider: dataProvider,
|
|
private toastr: ToastrService,
|
|
private angularFireMessaging: AngularFireMessaging,
|
|
) {
|
|
this.angularFireMessaging.messaging.subscribe(
|
|
(_messaging) => {
|
|
_messaging.onMessage = _messaging.onMessage.bind(_messaging);
|
|
_messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
|
|
}
|
|
)
|
|
}
|
|
|
|
/**
|
|
* update token in firebase database
|
|
*
|
|
* @param userId userId as a key
|
|
* @param token token as a value
|
|
*/
|
|
updateToken(userId, token) {
|
|
// we can change this function to request our backend service
|
|
this.angularFireAuth.authState.pipe(take(1)).subscribe(
|
|
() => {
|
|
const data = {};
|
|
data[userId] = token
|
|
this.angularFireDB.object('fcmTokens/').update(data)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* request permission for notification from firebase cloud messaging
|
|
*
|
|
* @param userId userId
|
|
*/
|
|
requestPermission(userId) {
|
|
this.angularFireMessaging.requestToken.subscribe(
|
|
(token) => {
|
|
this.data_provider.update_FBC_token(token).then(result => {
|
|
if(result.result!='success'){
|
|
console.error('Unable to write token.');
|
|
}
|
|
});
|
|
this.updateToken(userId, token);
|
|
},
|
|
(err) => {
|
|
console.error('Unable to get permission to notify.', err);
|
|
}
|
|
);
|
|
}
|
|
shownotification(payload){
|
|
if(typeof(payload) !== 'undefined'){
|
|
if(document.visibilityState){
|
|
this.toastr.warning(payload.notification.title, payload.notification.body,{
|
|
disableTimeOut:true,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* hook method when new notification received in foreground
|
|
*/
|
|
receiveMessage() {
|
|
this.angularFireMessaging.messages.subscribe(
|
|
(payload) => {
|
|
this.shownotification(payload);
|
|
this.currentMessage.next(payload);
|
|
})
|
|
}
|
|
} |