【发布时间】:2020-07-11 16:29:54
【问题描述】:
我正在开发适用于 Android 和 iOS 的 Ionic 5 应用程序。我正在使用 angularfire https://github.com/angular/angularfire/tree/v5 并且遇到了一些问题。特别是,这个库在使用 Firestore 时运行良好,但是当我尝试使用 Cloud Messaging 在我的 Android 设备上接收通知时,它会引发错误:FirebaseError: Messaging: This browser does not support the API's required to use Firebase SDK。 (消息/不支持的浏览器)。我按照此处链接的 GitHub 存储库上提供的教程进行操作,并尝试在浏览器上使用它,它工作正常。我怀疑这个库不能与 Ionic 一起正常工作,因此可以使用某些服务,例如 Firestore,而其他服务则不能。有人有解决这个问题的想法吗?下面是我的服务代码的sn-p:
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)},
);
}
}
谢谢!
【问题讨论】:
标签: firebase ionic-framework firebase-cloud-messaging angularfire