如何为使用 AWS SNS 创建的远程推送通知设置 APNS 通知标识符?

How can I set APNS notification identifiers for remote push notifications created using AWS SNS?

我在后端使用 AWS SNS SDK 向我的应用程序发送远程通知,并且我需要能够设置通知标识符,以便某些通知默认替换以前发送的副本,如 iOS developer docs:

If you use the same identifier when scheduling a new notification, the system removes the previously scheduled notification with that identifier and replaces it with the new one.

这对于本地通知很容易做到,因为它们是在应用程序中安排的,您可以在安排通知时设置通知标识符。

但是,对于远程通知,我似乎找不到任何方法来使用 SNS SDK(在用 .NET 编写的 Lambda 中)设置通知标识符。

根据 API 参考,SNS returns a response with a message ID(可能是通知标识符),但我自己看不到任何设置标识符的方法。

有人知道怎么做吗?还是 SNS 根本不允许这样做?

您可以自定义负载,如 AWS 所述:https://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html

When sending platform-specific payloads in messages using the Amazon SNS console, the data must be key-value pair strings and formatted as JSON with quotation marks escaped. The following example, including formatting and spaces for readability, shows a sample custom message for the GCM platform with key-value pair within the message body and formatted as JSON.

{ "GCM":"{ "data":{ "message":"Check out these awesome deals!", "url":"www.amazon.com" } }" }

您可以将您的 ID 添加为自定义字段。

编辑:如果您需要添加 headers,您可能希望通过 Lambda 函数代理请求:https://docs.aws.amazon.com/sns/latest/dg/sns-lambda.html 这是一个显示如何使用 Lambda 添加响应 headers 的线程:Setting http response header from AWS lambda

看来 AWS SNS 根本不允许完全自定义通知标识符,也不允许自定义发送到 APNSheaders 的 HTTP

使远程推送通知在到达时自行替换的唯一可行解决方案似乎是使用 Notification Service Extension to check delivered notifications whenever a new one arrives and replace any previous copies with the new version

它似乎始终如一地工作,您可以选择设置自定义负载 属性 或通知 JSON 中的 thread-id 属性 以便然后在发送每个通知之前检查它 on-device。