错误TypeError:firebase.messaging不是函数

时间:2018-03-19 08:28:24

标签: angular

我收到错误,ERROR TypeError:firebase.messaging不是函数 这有什么不对吗?

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { AngularFireAuth} from 'angularfire2/auth';
import * as firebase from 'firebase/app';

import 'rxjs/add/operator/take';
import { BehaviorSubject} from 'rxjs/BehaviorSubject';

@Injectable()
export class MessagingService {

messaging = firebase.messaging();
currentMessage = new BehaviorSubject(null);

constructor(private db: AngularFireDatabase, private afAuth: 
AngularFireAuth) {
}

private updateToken(token) {
  this.afAuth.authState.take(1).subscribe(user => {
    if (!user) { return; }

    const data = { [user.uid]: token };
    this.db.object('fcmTokens/').update(data);
  });
}

getPermission() {
    this.messaging.requestPermission()
        .then(() => {
            console.log('Notification permission granted.');
            return this.messaging.getToken();
        })
        .then(token => {
            console.log(token);
            this.updateToken(token);
        })
        .catch((err) => {
            console.log('Unable to get permission to notify.', err);
        });
} 

receiveMessage() {
    this.messaging.onMessage((payload) => {
        console.log('Message received. ', payload);
        this.currentMessage.next(payload);
    });
  }
}

ERROR TypeError: firebase.messaging is not a function 我对此错误有很大的疑问,我不知道如何解决这个问题,请帮我修复该代码。我做过一切就像一个例子,但它不起作用。非常感谢你的回答。

1 个答案:

答案 0 :(得分:1)

尝试更改

import * as firebase from 'firebase/app';

import * as firebase from 'firebase';