0

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.

1
  • Any solution to this? Commented May 18, 2019 at 22:06

1 Answer 1

2

Try changing

import * as firebase from 'firebase/app';

to

import * as firebase from 'firebase';

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.