From 55a3f43f6cc7b7883b2465f05a80f2fa77aebd23 Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Sun, 8 Nov 2020 17:38:37 +0000 Subject: [PATCH] Add messaging service --- .../send-push-notification.component.ts | 2 +- src/app/service/messaging.service.spec.ts | 12 +++++ src/app/service/messaging.service.ts | 44 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/app/service/messaging.service.spec.ts create mode 100644 src/app/service/messaging.service.ts diff --git a/src/app/dashboard/send-push-notification.component.ts b/src/app/dashboard/send-push-notification.component.ts index 865f2f0..024d2fd 100644 --- a/src/app/dashboard/send-push-notification.component.ts +++ b/src/app/dashboard/send-push-notification.component.ts @@ -16,7 +16,7 @@ export class SendPushNotificationComponent implements OnInit { constructor(private api: ApiService) { this.sendMessageForm = new FormGroup({ - messagetext: new FormControl('', Validators.required), + messagetext: new FormControl('', Validators.required), }); } diff --git a/src/app/service/messaging.service.spec.ts b/src/app/service/messaging.service.spec.ts new file mode 100644 index 0000000..135881a --- /dev/null +++ b/src/app/service/messaging.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { MessagingService } from './messaging.service'; + +describe('MessagingService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: MessagingService = TestBed.get(MessagingService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/service/messaging.service.ts b/src/app/service/messaging.service.ts new file mode 100644 index 0000000..200a1bb --- /dev/null +++ b/src/app/service/messaging.service.ts @@ -0,0 +1,44 @@ +import { Injectable } from '@angular/core'; +import { ApiService } from '../providers/api-service'; +import { AngularFireMessaging } from '@angular/fire/messaging'; +import { BehaviorSubject } from 'rxjs'; + +@Injectable() + +export class MessagingService { + currentMessage = new BehaviorSubject(null); + + constructor( + private angularFireMessaging: AngularFireMessaging, + private api: ApiService + ) { + this.angularFireMessaging.messages.subscribe((_messaging: AngularFireMessaging) => { + _messaging.onMessage = _messaging.onMessage.bind(_messaging); + _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging); + }) + } + + requestPermission() { + this.angularFireMessaging.requestToken.subscribe((token) => { + console.log("Device token: " + token); + return + this.api.addDeviceToken(token).subscribe( + result => { + console.log("Device registered successfully!"); + }, + error => { + console.error("Device could not be registered!", error._body); + } + ); + }, (err) => { + console.error('Unable to get permission to notify.', err); + }); + } + + receiveMessage() { + this.angularFireMessaging.messages.subscribe((payload) => { + console.log("new message received. ", payload); + this.currentMessage.next(payload); + }); + } +}