I'm implementing firebase on an application using Ionic Angular (cordova). When the Application is launched it does not go past the SplashScreen and the error
ERROR TypeError: Cannot read properties of undefined (reading 'getToken')
is displayed on the console. Sometimes, If I close and re-launch the app loads and the firebase token is generated. Other times it remains stuck on the splashscreen.
A previous question recommended changing the FCM provider to
{ provide: FCM, useValue: { getToken: () => Promise.resolve(true) } }
But when I do that other FCM functions stop working. For example, I get:
this.fcm.onNotification() is not a function
This is the code block
this.fcm.getToken().then(token => {
console.log(token);
});
I was able to resolve the problem using
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
constructor(private fcm: FCM){}
private async pushSetup() { await this.platform.ready();
console.log('FCM SETUP INIT');
if (!this.platform.is('cordova')) {
return;
}
console.log('IN CORDOVA');
this.hasPermission = await this.fcm.requestPushPermission();
console.log('CHECK hasPermission:', this.hasPermission);
this.token = await this.fcm.getToken();
console.log('CHECK getToken: ' + this.token);
console.log('ON NOTIFICATION SUBSCRIBE');
this.fcm
.onTokenRefresh()
.subscribe((newToken) => console.log('NEW TOKEN:', newToken));
this.fcm
.onNotification()
.subscribe((payload: object) => console.log('ON NOTIFICATION:', payload));
}