ionic-framework - 离子原生推送通知(Firebase X)不显示应用程序是否在后台
问题描述
我通过 FirebaseX 在我的应用程序上启用了 Ionic Native Push 通知。我的问题是,如果应用程序在前台打开,我会收到通知,但如果它在后台,我什么也得不到。
当我打开应用程序时,它会提供。
任何人都可以发光吗?
这是我的服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FirebaseX } from '@ionic-native/firebase-x/ngx';
import { AngularFirestore } from '@angular/fire/firestore';
import { Platform } from '@ionic/angular';
import { LoginService } from './login.service';
@Injectable()
export class FCMProvider {
constructor(
public firebaseX: FirebaseX,
public afs: AngularFirestore,
private platform: Platform,
private loginService: LoginService
) {}
async getToken() {
let token;
if (this.platform.is('android')) {
token = await this.firebaseX.getToken();
}
if (this.platform.is('ios')) {
const hasPermission = await this.firebaseX.hasPermission();
if (!hasPermission) {
await this.firebaseX.grantPermission();
} else {
token = await this.firebaseX.getToken();
}
}
console.log(token);
return this.saveTokenToFirestore(token);
}
saveTokenToFirestore(token: string) {
if (!token) {
return;
}
const devicesRef = this.afs.collection('devices');
let userOs;
if (this.platform.is('ios')) {
userOs = 'ios';
} else {
userOs = 'android';
}
const docData = {
token,
os: userOs,
userID: this.loginService.getUID()
};
return devicesRef.doc(token).set(docData);
}
listenToNotifications() {
return this.firebaseX.onMessageReceived();
}
}
在我的 app.component.ts 上,我得到了这个deviceReady
:
pushNotification() {
this.fcProvider.getToken();
this.fcProvider.listenToNotifications()
.pipe(
tap(msg => {
const toast = this.toastCtrl.create({
message: msg.body,
duration: 4000
});
toast.then(a => a.present());
})
)
.subscribe();
}
我通过邮递员发送消息:
{
"to": "e749fugfe793hfhwifh8988799f:...",
"name": "my_notification",
"data": {
"title": "Test",
"body": "This is just a test notification",
"notification_foreground": "true",
"notification_body" : "Notification body",
"notification_title": "Notification title",
"forceStart": "1"
},
"priority": "high"
}
解决方案
我最近一直在为这个话题而苦苦挣扎,要在后台听到通知,您必须订阅您将在 platform.pause 中获得的订阅(平台是来自@ionic/angular 的平台类型变量)。
在此订阅中,您应该收听 firebaseX 通知。
希望我的回答对您有所帮助,如果您有任何问题,请随时问我。
推荐阅读
- javascript - 查找数组中的值满足条件的数组索引
- pyspark - Pyspark - 在火花数据框中获取值的第一列出现
- r - 在 R 中从头开始创建一个 ggplot() 来说明结果
- python - “没有模块命名...” - 尽管代码似乎可以工作,但编辑器中的错误
- c++ - 如何在 C++ 中使用带有“do while”循环的计数器?
- c++ - 使用固定宽度整数时的 C++98 迂腐错误
- php - 如果优惠券应用于 WooCommerce 中的购物车,则仅显示登录用户的 BACS 支付网关
- r - 一个 R 闪亮的应用程序,显示 ggplot 图和 plotly 图
- c - GCC 无法识别文件内的目录路径
- java - 如何通过按下按钮隐藏 JPopupMenu?