我有一个 Ionic Angular (v.6) Android 和 iOS 应用程序,我想向它们发送推送通知。我正在使用 Capacitor Push Notifications 库。我的电容版本是3.
当我的应用程序打开时,我可以从 pushNotificationReceived 侦听器获取通知。但是,当我的应用程序被杀死/后台时,我无法在系统托盘中收到任何通知。我都尝试过从 API 和 FireBase 控制台发送推送通知,但没有成功。我错过了什么?
constructor(private platform: Platform) {
if (this.platform.is('ios') || this.platform.is('android')) {
let permissions = await PushNotifications.checkPermissions();
if (permissions.receive == "granted") {
await this.setupFirebase();
} else {
let permissions = await PushNotifications.requestPermissions();
if (permissions.receive == "granted") {
await this.setupFirebase();
}
}
}
}
private async setupFirebase() {
await PushNotifications.addListener("registration", (token) => {
this.h.Toast(this.toastController, token.value);
});
await PushNotifications.addListener("registrationError", (error) => {
this.h.Toast(this.toastController, error.error);
});
await PushNotifications.addListener("pushNotificationReceived", (notification) => {
this.h.Toast(this.toastController, JSON.stringify(notification));
});
await PushNotifications.addListener("pushNotificationActionPerformed", (notification) => {
this.h.Toast(this.toastController, JSON.stringify(notification));
});
PushNotifications.register().then(() => {
PushNotifications.createChannel({id: "test", name: "test", importance: 5});
});
}
我通过实现这两行解决了我的问题:
implementation "com.google.firebase:firebase-iid:21.1.0"
implementation "com.google.firebase:firebase-messaging:22.0.0"