Я использую Capacitor Push-уведомления в своем приложении. Я следовал руководству по конденсаторам с веб-сайта по конденсаторам и реализовал этот код:
onPushNotifications() {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
// On succcess, we should be able to receive notifications
PushNotifications.addListener('registration', (token: PushNotificationToken) => {
console.log('Push registration success, token: ' + token.value);
});
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError', (error: any) => {
console.log('Error on registration: ' + JSON.stringify(error));
this.showToast('Erro ao ativar as notificações push.');
});
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived', (notification: PushNotification) => {
this.showToast(notification.title + '\n' + notification.body);
this.addNewNotification(notification);
});
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed', (notification: PushNotificationActionPerformed) => {
this.addNewNotification(notification.notification.data);
});
}
. Таким образом, я мог получить доступ к заголовку данных и телу из push-уведомлений, когда приложение работает на переднем плане (используя pushNotificationReceived). Но когда приложение работает в фоновом режиме, я мог получить доступ к заголовку и телу для сохранения в массив, например.
Я пытался реализовать этот код:
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
let notif = this.state.notifications;
notif.push({ id: notification.notification.data.id, title: notification.notification.data.title, body: notification.notification.data.body })
this.setState({
notifications: notif
})
}
);
С этого сайта https://medium.com/enappd/firebase-push-notification-in-ionic-react-app-using-capacitor-b6726c71bda4 Но я не получил функционального ответа. Я продолжаю получать неопределенные заголовок и текст.
Пожалуйста, кто-нибудь может мне помочь? Спасибо!