1
1
import { Component , OnInit } from '@angular/core' ;
2
2
import { AngularFireMessaging } from '@angular/fire/messaging' ;
3
3
import { trace } from '@angular/fire/performance' ;
4
- import { Observable , fromEvent } from 'rxjs' ;
5
- import { tap , map , filter } from 'rxjs/operators' ;
6
- import * as firebase from 'firebase/app' ;
7
- import 'firebase/messaging' ;
4
+ import { Observable } from 'rxjs' ;
5
+ import { tap } from 'rxjs/operators' ;
8
6
import { SwPush } from '@angular/service-worker' ;
7
+ import { environment } from '../../environments/environment' ;
9
8
10
9
@Component ( {
11
10
selector : 'app-messaging' ,
@@ -25,8 +24,15 @@ export class MessagingComponent implements OnInit {
25
24
message$ : Observable < any > ;
26
25
showRequest = false ;
27
26
28
- constructor ( public readonly messaging : AngularFireMessaging , swPush : SwPush ) {
29
- messaging . usePublicVapidKey ( 'BIDPctnXHQDIjcOXxDS6qQcz-QTws7bL8v7UPgFnS1Ky5BZL3jS3-XXfxwRHmAUMOk7pXme7ttOBvVoIfX57PEo' ) . then ( ( ) => {
27
+ constructor ( public readonly messaging : AngularFireMessaging , readonly swpush : SwPush ) {
28
+ swpush . messages . subscribe ( it => console . log ( 'swpush' , it ) ) ;
29
+ messaging . usePublicVapidKey ( environment . vapidKey ) . then ( async ( ) => {
30
+ if ( navigator && navigator . serviceWorker ) {
31
+ const registration = await navigator . serviceWorker . getRegistration ( ) ;
32
+ if ( registration ) {
33
+ await messaging . useServiceWorker ( registration ) ;
34
+ }
35
+ }
30
36
this . message$ = messaging . messages ;
31
37
this . token$ = messaging . tokenChanges . pipe (
32
38
trace ( 'token' ) ,
0 commit comments