如何将 pushNotificationActionPerformed 中的标题和 body 数据访问到带有电容器的离子应用程序中
How can I access title and body data from pushNotificationActionPerformed into the ionic application with capacitor
我在我的应用程序中使用了 Capacitor Push Notifications。我已经按照电容器网站上的电容器教程进行操作,并实现了以下代码:
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);
});
}
有了这个,当应用程序在前台 运行(使用 pushNotificationReceived)时,我可以从推送通知访问数据标题和 body。但是当应用程序在后台 运行 时,我可以访问标题和 body 以保存在数组中,例如。
我尝试实现此代码:
// 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
但我没有得到任何功能性的答案。我一直在标题和 body.
中未定义
拜托,有人可以帮助我吗?
谢谢!
遗憾的是,这就是推送通知负载的工作方式,当在后台或关闭时,OS 处理它们,使用标题和 body 在托盘中显示通知,但是当您点击它们时,该信息不再适用于该应用程序。
使用数据负载传递任何你想要的数据。
您的负载应包含:
- notification_foreground,
- notification_title,
- notification_body。
你必须做到:
notification_foreground = true
我在我的应用程序中使用了 Capacitor Push Notifications。我已经按照电容器网站上的电容器教程进行操作,并实现了以下代码:
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);
});
}
有了这个,当应用程序在前台 运行(使用 pushNotificationReceived)时,我可以从推送通知访问数据标题和 body。但是当应用程序在后台 运行 时,我可以访问标题和 body 以保存在数组中,例如。
我尝试实现此代码:
// 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 但我没有得到任何功能性的答案。我一直在标题和 body.
中未定义拜托,有人可以帮助我吗? 谢谢!
遗憾的是,这就是推送通知负载的工作方式,当在后台或关闭时,OS 处理它们,使用标题和 body 在托盘中显示通知,但是当您点击它们时,该信息不再适用于该应用程序。
使用数据负载传递任何你想要的数据。
您的负载应包含:
- notification_foreground,
- notification_title,
- notification_body。
你必须做到:
notification_foreground = true