如何在 Angular2 中实例化 Firebase 云消息传递 [英] How to instantiate Firebase Cloud Messaging in Angular2
问题描述
如何在 Angular2/TypeScript/AngularFire2 中实例化 Firebase 云消息传递?
How to instantiate Firebase Cloud Messaging in Angular2 / TypeScript / AngularFire2 ?
此处针对 JavaScript 进行了描述:https://firebase.google.com/docs/cloud-messaging/js/客户
It's described here for JavaScript: https://firebase.google.com/docs/cloud-messaging/js/client
推荐答案
firebase.messaging()
函数将 Firebase 应用实例作为可选参数.
The firebase.messaging()
function takes the Firebase app instance as an optional parameter.
要将其与 AngularFire2
连接起来,您可以让 AngularFire2
执行应用初始化并创建 Firebase 应用实例,然后可以将应用实例注入(到服务中,例如示例)并将其传递给 firebase.messaging()
像这样:
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.
这篇关于如何在 Angular2 中实例化 Firebase 云消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!