Add messaging service
This commit is contained in:
parent
659b37b589
commit
d7adc0f514
4 changed files with 72 additions and 1 deletions
|
@ -16,7 +16,7 @@ export class SendPushNotificationComponent implements OnInit {
|
||||||
|
|
||||||
constructor(private api: ApiService) {
|
constructor(private api: ApiService) {
|
||||||
this.sendMessageForm = new FormGroup({
|
this.sendMessageForm = new FormGroup({
|
||||||
messagetext: new FormControl('', Validators.required),
|
messagetext: new FormControl('', Validators.required),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/app/service/messaging.service.spec.ts
Normal file
12
src/app/service/messaging.service.spec.ts
Normal file
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
44
src/app/service/messaging.service.ts
Normal file
44
src/app/service/messaging.service.ts
Normal file
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
15
src/firebase-messaging-sw.js
Normal file
15
src/firebase-messaging-sw.js
Normal file
|
@ -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();
|
Reference in a new issue