How to instantiate Firebase Cloud Messaging in Angular2

巧了我就是萌 提交于 2019-12-21 12:29:48

问题


How to instantiate Firebase Cloud Messaging in Angular2 / TypeScript / AngularFire2 ?

It's described here for JavaScript: https://firebase.google.com/docs/cloud-messaging/js/client


回答1:


The firebase.messaging() function takes the Firebase app instance as an optional parameter.

To wire it up with AngularFire2, you could let AngularFire2 perform the app initialization and create the Firebase app instance and could then inject the app instance (into a service, for example) and pass it to firebase.messaging() like this:

import { Inject, Injectable } from "@angular/core";
import { FirebaseApp } from "angularfire2";
import * as firebase from 'firebase';

@Injectable()
export class SomeService {

    private _messaging: firebase.messaging.Messaging;

    constructor(@Inject(FirebaseApp) private _firebaseApp: firebase.app.App) {

        this._messaging = firebase.messaging(this._firebaseApp);
        this._messaging.requestPermission()
            .then(() => { ... })
            .catch((error) => { ... });
    }
}

You'll need to setup the web app manifest that's mentioned in the article you referenced. That's something with which I am unfamiliar.




回答2:


There seems to be an import that is required:

import '@firebase/messaging';


来源:https://stackoverflow.com/questions/40460240/how-to-instantiate-firebase-cloud-messaging-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!