该问题的解决方案是在发送推送通知时,需在 data" 字段中附加要发送的数据对象。在接收到推送通知后,可以使用 "notificationClicks" 事件获取数据对象并进行处理。
以下是示例代码: // 发送推送通知 const pushPayload = { notification: { title: 'New Notification!', body: 'This is a notification message!', }, data: { id: 123, message: 'This is some data attached to the notification!', } }; this.swPush.requestSubscription(...) .then(sub => { // 将推送通知发送到客户端 this.http.post('/api/push', { sub, payload: pushPayload }) .subscribe(...); });
// 接收推送通知的数据 this.swPush.notificationClicks.subscribe(click => { const data = click.notification.data; // 处理接收到的数据 console.log('Received data:', data); }); "