Unhandled Promise rejection: this._next is not a function : Zone in @angular/fire/messaging
Asked Answered
H

5

4

When I am receiving firebase push notifications in the foreground, by using @angular/fire/messaging. The method is:

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

Let me share my complete code: PushNotificationsService Code I have written.

My Angular-cli versions:

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

Let me share my package.json files: package.json

I am getting the below error when I receive message in the foreground:

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

Any help is highly appreciable! CODE: GITHUB REPO LINK

I have update my secret key from firebase in the GitHub code

Hostel answered 29/1, 2020 at 6:34 Comment(0)
H
0

Yes What I have realized that i had using the wrong version of "@angular/fire": "^5.3.0" which is not compatible with "firebase": "^7.6.2", in my package.json.

So, What i have did-

In firebase-messaging-sw.js or the service worker file?

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

should have the same version with your firebase on package.json

"dependencies": { ... "firebase": "^5.0.0", "@angular/fire": "^5.0.0", ... }

And it is working like a charm.

Hostel answered 16/2, 2020 at 6:38 Comment(0)
I
2

There is workaround for it.

You need to modify your custom messaging service little bit.

Inside constructor you need to replace following code

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);
    }
);

with that

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);
    }
);

Then you will get push notification even you are in fore-ground.

Incorruptible answered 3/2, 2020 at 12:15 Comment(0)
S
2

Try with this versions:

"dependencies": {
...
"firebase": "^7.6.0",
"@angular/fire": "^5.2.3",
...
}

it works perfectly...

Check this repo: Angular Push Notification

Surname answered 6/3, 2020 at 4:56 Comment(0)
H
0

Yes What I have realized that i had using the wrong version of "@angular/fire": "^5.3.0" which is not compatible with "firebase": "^7.6.2", in my package.json.

So, What i have did-

In firebase-messaging-sw.js or the service worker file?

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

should have the same version with your firebase on package.json

"dependencies": { ... "firebase": "^5.0.0", "@angular/fire": "^5.0.0", ... }

And it is working like a charm.

Hostel answered 16/2, 2020 at 6:38 Comment(0)
P
0

The solution is detailed here.

In short, you must have firebase library version greater than 7.16 inside your package.json and it should match the version in firebase-messaging-sw.js

Preventive answered 10/8, 2020 at 0:15 Comment(0)
P
0

The problem is indeed related to the version of Firebase! Finally, I found the solution is detailed here.

You must have firebase library version greater than 7.16 inside your package.json and it should match the version in firebase-messaging-sw.js

Preventive answered 10/8, 2020 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.