how to subscribe background messages via firebase in angular service (Typescript file)

Viewed 409
import { Injectable } from "@angular/core";
import { AngularFireMessaging } from "@angular/fire/messaging";
import { BehaviorSubject, Subject } from "rxjs";
@Injectable()
export class AppService {
    currentMessage = new BehaviorSubject(null);
    fcm_key = new BehaviorSubject(null);

    private componentMethodCallSource = new Subject<any>();

    componentMethodCalled$ = this.componentMethodCallSource.asObservable();
    constructor(private angularFireMessaging: AngularFireMessaging) { }

    requestPermission() {
        this.angularFireMessaging.requestToken.subscribe(
            (token: any) => {
                this.fcm_key.next(token);
            },
            (err: any) => {
                console.error("Unable to get permission to notify.", err);
            }
        );
    }

    receiveMessage() {

        this.angularFireMessaging.messages.subscribe((payload: any) => {
            this.componentMethodCallSource.next(payload);
            this.currentMessage.next(payload);
        });
    }
}

In my application, I have subscribed to Firebase (Cloud base) for getting notification messages for both active as well as passive tab. In that, I'm triggering the firebase through PHP CURL (with the firebase key) to get notifications. I'm able to get the notifications using the below functions

Active tabs:

this.angularFireMessaging.messages.subscribe((payload: any) => {
        this.componentMethodCallSource.next(payload);
        this.currentMessage.next(payload);
    });

But I don't have any idea on how to use the below function in angular for getting notification messages even while my app tabs are not active.

this.angularFireMessaging.onBackgroundMessage
0 Answers
Related