mirror of
https://github.com/MikroWizard/mikrofront.git
synced 2026-01-04 00:09:32 +00:00
MikroWizard Initial commit | MikroFront Welcome to the world :)
This commit is contained in:
commit
b97aec6b97
203 changed files with 41097 additions and 0 deletions
86
src/app/providers/messaging.service.ts
Normal file
86
src/app/providers/messaging.service.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
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);
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue