简体   繁体   English

使用 firebase 7.14.6 和 @angular/fire 6.0.0 FCM web 推送通知的类型“AngularFireMessaging”上不存在属性“消息”

[英]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在实施 FCM Background Web Notification在我的服务中低于错误

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

messaging.service.ts消息服务.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 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.我也尝试过降级@angular/firefirebase但没有成功。

For error "Property 'onMessage' does not exist on type '{}'" I just added the object type to _messaging and now application runs:对于错误“类型'{}'上不存在属性'onMessage'”,我刚刚将object类型添加到_messaging ,现在应用程序运行:

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",

AngularFireMessaging does not contain a property called messaging , you need to use the property messages : AngularFireMessaging不包含名为messaging的属性,您需要使用属性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 https://github.com/angular/angularfire/blob/master/docs/messaging/messaging.md#subscribing-to-foreground-messages

This one worked for me:这个对我有用:

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

I change the version of @angular/fire and its work我更改了@angular/fire 的版本及其工作

npm i @angular/fire@5.1.1 --save

I try this,我试试这个

npm i @angular/fire@5.1.1 --save

and, maybe when running, just found error also like"Property 'automaticDataCollectionEnabled' is missing in type 'FirebaseApp'"并且,也许在运行时,刚刚发现错误也像“'FirebaseApp'类型中缺少属性'automaticDataCollectionEnabled'”

and then i open file firebase.app.module.d.ts just edit like this.然后我打开文件 firebase.app.module.d.ts 就像这样编辑。

export declare class FirebaseApp implements app.App {

installations(): import("firebase").installations.Installations;
performance(): import("firebase").performance.Performance;
remoteConfig(): import("firebase").remoteConfig.RemoteConfig;
analytics(): import("firebase").analytics.Analytics;
name: string;
options: {};
auth: () => FirebaseAuth;
database: (databaseURL?: string) => FirebaseDatabase;
messaging: () => FirebaseMessaging;
storage: (storageBucket?: string) => FirebaseStorage;
delete: () => Promise<void>;
firestore: () => FirebaseFirestore;
functions: (region?: string) => FirebaseFunctions;
}

try this it will work-试试这个它会工作 -

import { AngularFireMessaging } from '@angular/fire/compat/messaging';从“@angular/fire/compat/messaging”导入 { AngularFireMessaging };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM