如何从 API 服务器删除特定的推送通知
How to delete specific push notification from API server
我正在使用 this PHP script 向 Firebase 发送通知:
...
$fields = [
'to' => DEVICE_TOKEN,
'notification' => [
'title' => "Title",
'body' => 'notification body',
'tag' => 'tag',
],
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
...
它在我的 android 上正常弹出。我还可以使用 tag
属性
编辑通知
但是如何删除通知?例如,如果有人在浏览器中阅读它,我需要从其他设备上删除此通知。
我试图在编辑后的通知中发送空 body
,但没有成功。
在 Cordova 前端,我正在使用 cordova-plugin-firebase,但我不确定问题出在后端还是前端。
我认为通知消息一旦通过 Firebase 发送到设备就无法删除 API。
我看到的一个可能的解决方案是在用户看到消息后创建一个 POST 通知,将其发送到您的后端,后端会发送一个带有特定 "flag" 的新通知,但不会显示这个新通知是因为 "flag"(你在你的应用程序中实现了一个 if 检查)。当设备读取该标志时,它会根据 "flag" 删除通知。您可以使用 NotificationManager
中的 cancel()
来做到这一点。 Documentation
最佳解决方案是使用 Firebase 的设备组选项。
通过这种方式,您可以轻松地在唯一密钥下注册同一用户的多个设备。因此,当您在两个不同的设备(例如浏览器和智能手机)上向该用户发送推送通知时,用户会从一台设备上读取通知,然后它会神奇地从另一台设备上消失。
如果您已经熟悉 cordova 和 Firebase 世界,这将非常简单。
引用如下:https://firebase.google.com/docs/cloud-messaging/js/device-group
我正在使用 this PHP script 向 Firebase 发送通知:
...
$fields = [
'to' => DEVICE_TOKEN,
'notification' => [
'title' => "Title",
'body' => 'notification body',
'tag' => 'tag',
],
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
...
它在我的 android 上正常弹出。我还可以使用 tag
属性
但是如何删除通知?例如,如果有人在浏览器中阅读它,我需要从其他设备上删除此通知。
我试图在编辑后的通知中发送空 body
,但没有成功。
在 Cordova 前端,我正在使用 cordova-plugin-firebase,但我不确定问题出在后端还是前端。
我认为通知消息一旦通过 Firebase 发送到设备就无法删除 API。
我看到的一个可能的解决方案是在用户看到消息后创建一个 POST 通知,将其发送到您的后端,后端会发送一个带有特定 "flag" 的新通知,但不会显示这个新通知是因为 "flag"(你在你的应用程序中实现了一个 if 检查)。当设备读取该标志时,它会根据 "flag" 删除通知。您可以使用 NotificationManager
中的 cancel()
来做到这一点。 Documentation
最佳解决方案是使用 Firebase 的设备组选项。
通过这种方式,您可以轻松地在唯一密钥下注册同一用户的多个设备。因此,当您在两个不同的设备(例如浏览器和智能手机)上向该用户发送推送通知时,用户会从一台设备上读取通知,然后它会神奇地从另一台设备上消失。
如果您已经熟悉 cordova 和 Firebase 世界,这将非常简单。
引用如下:https://firebase.google.com/docs/cloud-messaging/js/device-group