I'm developing a Ionic 5 app both for Android and iOS. I'm using angularfire https://github.com/angular/angularfire/tree/v5 and I'm having some issues. In particular, this library works good when using Firestore, but when I try to use Cloud Messaging to receive notifications on my Android device, it raises an error: FirebaseError: Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser). I followed the tutorial available on the GitHub repository linked here and I tried to use it on browser and it works fine. My suspect is that this library doesn't work properly with Ionic, so some services, like Firestore, can be used and others not. Anyone has some ideas to solve this issue? Here follows the snippet of code of my service:
import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { AngularFireFunctions } from '@angular/fire/functions';
import { ToastController } from '@ionic/angular';
/* //Fixing temporary bug in AngularFire - Found on Internet
import * as app from 'firebase';
const _messaging = app.messaging();
_messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
_messaging.onMessage = _messaging.onMessage.bind(_messaging); */
@Injectable({
providedIn: 'root'
})
export class FcmService {
token: any;
constructor(private toastController: ToastController, private afm: AngularFireMessaging, private aff: AngularFireFunctions) { }
async makeToast(message: string){
const toast = await this.toastController.create({
duration: 5000,
message: message,
position: 'top',
buttons: [
{
side: 'end',
text: 'Close',
role: 'cancel',
handler: () => {
console.log('Favorite clicked');
}
}]
});
toast.present();
}
getPermission(){
this.afm.requestToken
.subscribe(
(token) => { console.log('Permission granted! Save to the server!', token); this.token = token;},
(error) => { console.error(error); },
);
}
showMessages(){
return this.afm.messages
.subscribe(
(message) => {console.log(message);}
);
}
subscribe(topic: string){
this.aff.httpsCallable('subscribeToTopic')({topic: topic, token: this.token})
.subscribe(
(_) => {this.makeToast("Notifications about "+topic+" activated.")},
(error) => {console.log(error)},
);
}
unsubscribe(topic: string){
this.aff.httpsCallable('unsubscribeFromTopic')({topic: topic, token: this.token})
.subscribe(
(_) => {this.makeToast("Notifications about "+topic+" deactivated.")},
(error) => {console.log(error)},
);
}
}
Thank you!