angular - 未处理的 promise 拒绝 : this. _next 不是函数:@angular/fire/messaging 中的区域

标签 angular firebase push-notification firebase-cloud-messaging zone

当我在前台接收 Firebase 推送通知时,使用 @angular/fire/messaging .方法是:

  this.angularFireMessaging.messages.subscribe(
      (payload) => {
        console.log("new message received. ", payload);
        this.currentMessage.next(payload);
      })

让我分享我的完整代码:PushNotificationsService Code我已经写了。

我的 Angular-cli 版本:
Angular CLI: 8.1.3
Node: 12.4.0
OS: linux x64
Angular: 8.1.3
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.801.3
@angular-devkit/build-angular     0.801.3
@angular-devkit/build-optimizer   0.801.3
@angular-devkit/build-webpack     0.801.3
@angular-devkit/core              8.1.3
@angular-devkit/schematics        8.1.3
@angular/fire                     5.3.0
@ngtools/webpack                  8.1.3
@schematics/angular               8.1.3
@schematics/update                0.801.3
rxjs                              6.4.0
typescript                        3.4.5
webpack                           4.35.2

让我分享我的 package.json 文件:package.json

当我在前台收到消息时出现以下错误:
Unhandled Promise rejection: this._next is not a function ; Zone: <root> ; Task: ServiceWorkerContainer.addEventListener:message ; Value: TypeError: "this._next is not a function"
    next RxJS
    messageEventListener index.esm.js:1046
    step tslib.es6.js:99
    verb tslib.es6.js:80
    __awaiter tslib.es6.js:73
    ZoneAwarePromise Angular
    __awaiter tslib.es6.js:69
    messageEventListener index.esm.js:1035
    WindowController index.esm.js:876
    Angular 5
 next@http://localhost:4200/vendor.js:111323:18
./node_modules/@firebase/messaging/dist/index.esm.js/WindowController.prototype.messageEventListener/</<@http://localhost:4200/firebase-messaging.js:2418:34
step@http://localhost:4200/vendor.js:127272:23
verb/<@http://localhost:4200/vendor.js:127253:53
__awaiter/<@http://localhost:4200/vendor.js:127246:71
ZoneAwarePromise@http://localhost:4200/polyfills.js:3882:29
__awaiter@http://localhost:4200/vendor.js:127242:12
./node_modules/@firebase/messaging/dist/index.esm.js/WindowController.prototype.messageEventListener@http://localhost:4200/firebase-messaging.js:2407:71
WindowController/<@http://localhost:4200/firebase-messaging.js:2248:26
invokeTask@http://localhost:4200/polyfills.js:3397:31
runTask@http://localhost:4200/polyfills.js:3174:47
invokeTask@http://localhost:4200/polyfills.js:3471:34
invokeTask@http://localhost:4200/polyfills.js:4609:14
globalZoneAwareCallback@http://localhost:4200/polyfills.js:4635:27

任何帮助都是非常可观的! CODE: GITHUB REPO LINK

我已经在 GitHub 代码中从 firebase 更新了我的 key

最佳答案

有解决方法。

您需要稍微修改您的自定义消息服务。

在构造函数中,您需要替换以下代码

this.angularFireMessaging.messaging.subscribe(
    (_messaging: any) = > {
        _messaging.onMessage = _messaging.onMessage.bind(_messaging);
        messaging._next = (payload: any) = > {
            console.log(payload);
        };
        _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
    }
);

接着就,随即

this.angularFireMessaging.messaging.subscribe(
    (_messaging: any) = > {
        // _messaging.onMessage = _messaging.onMessage.bind(_messaging);
        _messaging._next = (payload: any) = > {
            console.log(payload);
        };
        _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
    }
);

然后,即使您处于前台,您也会收到推送通知。

关于angular - 未处理的 promise 拒绝 : this. _next 不是函数:@angular/fire/messaging 中的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59961620/

相关文章:

angular - 在运行时定义 BlockUi Angular 注释?

从父级到子级的 Angular 传递数据未定义

java - Android 版本 8.1,startForeground 的错误通知

ios - 推送通知不会振动且静音

javascript - Typescript - 在模块内找不到构造函数方法

javascript - ngrx : how the reducers function are invoked, 何时被调用?

java - Firebase - 新依赖项 - 问题 : NoClassDefFoundError: Failed resolution (QueryListenOptions)

html - 错误 : Reference. 推送失败:第一个参数在属性中包含一个函数 - firebase

android - 如何检查数据库引用子项是否存在

android - 重新安装 Android 应用程序后,该应用程序会收到针对旧安装的通知