Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'messaging' does not exist on type 'AngularFireMessaging' using firebase 7.14.6 and @angular/fire 6.0.0 FCM web push notification

In implementing FCM Background Web Notification getting below error in my service

error TS2339: Property 'messaging' does not exist on type 'AngularFireMessaging'

messaging.service.ts

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { BehaviorSubject } from 'rxjs'

@Injectable()
export class MessagingService {
    currentMessage = new BehaviorSubject(null);
    constructor(private angularFireMessaging: AngularFireMessaging) {
         this.angularFireMessaging.messaging.subscribe(
             (_messaging) => {
                 _messaging.onMessage = _messaging.onMessage.bind(_messaging);
                 _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
             }
         )
    }
}

src/firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.6/firebase-messaging.js');


firebase.initializeApp({
    apiKey: "xxxxxxxxxxx",
    authDomain: "xxxxxxxxxxx",
    databaseURL: "xxxxxxxxxxx",
    projectId: "xxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxx",
    appId: "xxxxxxxxxxx"
});

const messaging = firebase.messaging();

installed packages

"@angular/fire": "^6.0.0",
"firebase": "^7.14.6",
"@angular/cli": "^9.1.7",

i also tried to play with downgrading @angular/fire and firebase but no success.

like image 550
Shiv Kumar Baghel Avatar asked Jun 04 '20 15:06

Shiv Kumar Baghel


4 Answers

For error "Property 'onMessage' does not exist on type '{}'" I just added the object type to _messaging and now application runs:

this.angularFireMessaging.messages.subscribe(
    (_messaging: AngularFireMessaging) => {
    _messaging.onMessage = _messaging.onMessage.bind(_messaging);
    _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
  })
}

Using this dependencies:

"@angular/fire": "^6.0.3",
"firebase": "^7.21.0",
like image 190
Astor Avatar answered Nov 19 '22 16:11

Astor


AngularFireMessaging does not contain a property called messaging, you need to use the property messages:

         this.angularFireMessaging.messages.subscribe(
             (_messaging) => {
                 _messaging.onMessage = _messaging.onMessage.bind(_messaging);
                 _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
             }
         )
    }

https://github.com/angular/angularfire/blob/master/docs/messaging/messaging.md#subscribing-to-foreground-messages

like image 5
Peter Haddad Avatar answered Nov 19 '22 15:11

Peter Haddad


This one worked for me:

this.angularFireMessaging.messages.subscribe(
    (_messaging: AngularFireMessaging) => {
    _messaging.onMessage = _messaging.onMessage.bind(_messaging);
    _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
  })
}
like image 3
Marcus Avatar answered Nov 19 '22 15:11

Marcus


try this it will work-

import { AngularFireMessaging } from '@angular/fire/compat/messaging';

like image 3
Bharath MR Avatar answered Nov 19 '22 14:11

Bharath MR