Ionic fcm plugin, onNotification function not working

I have found a solution. It is similar to this but, uses the main package instead of @ionic-native/fcm wrapper library.

The working code is as follows, to implement fcm, in an ionic project built over angular.

In cmd[your project directory]

ionic cordova plugin add [email protected]

Now, please make sure in your projects, within plugins folder, a folder named cordova-plugin-fcm-with-dependecy-updated is present and within it, there is a sub folder ionic and within this sub folder there is another sub folder ngx.

Now, you can use it in your code, I use it in app.component.ts

import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
// Initialise in constructor
.
.
.
.
this.fcm.getToken().then(function(token) {
    console.log(token);
}).catch(err) {
    console.log(err, 'Unable to get token');
});

this.fcm.onNotification().subscribe((data: any) => {
    console.log(data);
});

this.fcm.onTokenRefresh(). subscribe (async token => {
    console.log(token);
});

Now, it works properly. You can try sending a notification to on the token you receive.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top