From d7adc0f51489d49956e9eaecea7a2bbea96ea40c 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 +++++++++++++++++++ src/firebase-messaging-sw.js | 15 +++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/app/service/messaging.service.spec.ts create mode 100644 src/app/service/messaging.service.ts create mode 100644 src/firebase-messaging-sw.js 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); + }); + } +} diff --git a/src/firebase-messaging-sw.js b/src/firebase-messaging-sw.js new file mode 100644 index 0000000..c40fe58 --- /dev/null +++ b/src/firebase-messaging-sw.js @@ -0,0 +1,15 @@ +importScripts('https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js'); +importScripts('https://www.gstatic.com/firebasejs/8.0.0/firebase-messaging.js'); + +firebase.initializeApp({ + apiKey: "AIzaSyB-iIcMH_eyfFT043_8pSGX3YYugpjm3Fg", + authDomain: "localspend-47012.firebaseapp.com", + databaseURL: "https://localspend-47012.firebaseio.com", + projectId: "localspend-47012", + storageBucket: "localspend-47012.appspot.com", + messagingSenderId: "469562689216", + appId: "1:469562689216:web:43378a652e829fed354f25", + measurementId: "G-8NMVL6RL8C" +}); + +const messaging = firebase.messaging();