如何在Angular2中实例化Firebase Cloud Messaging [英] How to instantiate Firebase Cloud Messaging in Angular2

查看:129
本文介绍了如何在Angular2中实例化Firebase Cloud Messaging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Angular2 / TypeScript / AngularFire2中实例化Firebase云消息传递?



这里描述了JavaScript:
https://firebase.google.com/docs/cloud-messaging/js/client


firebase.messaging()函数将Firebase应用程序实例作为可选参数。

使用 AngularFire2 来连接它,你可以让 AngularFire2 执行应用程序初始化并创建Firebase应用程序实例,然后可以将应用程序实例(例如,插入服务)传递给 firebase.messaging()这:

 从@ angular / core导入{Inject,Injectable}; 
从angularfire2导入{FirebaseApp};
从'firebase'导入*作为firebase;

@Injectable()
导出类SomeService {

private _messaging:firebase.messaging.Messaging;

构造函数(@Inject(FirebaseApp)private _firebaseApp:firebase.app.App){

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




$你需要设置网络应用清单,这是您引用的文章中提到的。这是我不熟悉的东西。


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

解决方案

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.

这篇关于如何在Angular2中实例化Firebase Cloud Messaging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆