Aurimas Tunaitis
Aurimas Tunaitis

Reputation: 1

ERROR TypeError: firebase.messaging is not a function

I'm getting error, ERROR TypeError: firebase.messaging is not a function Whats wrong with it ?

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 I'm having big probleems with that error, I don't have any idea how to fix that, please help me repair that code. I've done everything like an example, but it doesn't work. thank you so much for your answers.

Upvotes: 0

Views: 2155

Answers (1)

user2158335
user2158335

Reputation: 41

Try changing

import * as firebase from 'firebase/app';

to

import * as firebase from 'firebase';

Upvotes: 2

Related Questions