开发者控制台

实时通知示例

实时通知示例

此页面提供了发送至您的HTTPS终端节点的订阅、消费品和权利的实时通知JSON响应示例。

订阅

在此示例中,客户为您的应用创建了新的订阅,然后您会收到SUBSCRIPTION_PURCHASED实时通知。

{
 "Type" : "Notification",
 "MessageId" : "b1c4d9bb-0fd9-54b2-8f74-8e59c91e396a",
 "TopicArn" : "arn:aws:sns:us-east-1:033616359524:LT0XA",
 "Message" : "{\"appPackageName\":\"com.example.amazonsdkapivalidator3\",\"notificationType\":\"SUBSCRIPTION_PURCHASED\",\"appUserId\":\"io9oFdzmCSMuKk_pp9pYccoIHRfc6kSAAYaNo51xZrg=\",\"receiptId\":\"koxIn_LO2u7rc-_MF40hKz1vqSSJSlitfTNDeH_JAs8=:3:11\",\"relatedReceipts\":{},\"timestamp\":1600701569824,\"betaProductTransaction\":false}",
 "Timestamp" : "2020-09-21T15:19:35.686Z",
 "SignatureVersion" : "1",
 "Signature" : "EXAMPLEab123...",
 "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-a86cb10b4e1f29c941702d737128f7b6.pem",
 "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:033616359524:LT0XA:a745747c-8230-4cd2-84c6-d0e7df9f1ca1"
}

消费品

在此示例中,客户从您的应用中购买了消费品,然后您会收到CONSUMABLE_PURCHASED实时通知。

{
    "Type" : "Notification",
    "MessageId" : "22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324",
    "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
    "Message": "{\"receiptId\":\"bsw3Wi6w17Ngdabggckh61kmmFhfwlfT9nPZeLfh83k=:1:11+Sheet2!A1+\",\"relatedReceipts\": {},\"appUserId\":\"kyplB1fdRX1m6/GNAVW0l6jv9Nl1V/GemSopKBHM84w=\",\"notificationType\":\"CONSUMABLE_PURCHASED\",\"appPackageName\":\"com.playrix.gardenscapes.amazon\",\"timestamp\":1588086967189, \"betaProductTransaction\":false,}",
    "Timestamp" : "2012-05-02T00:54:06.655Z",
    "SignatureVersion" : "1",
    "Signature" : "EXAMPLEw6JRN...",
    "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
    "UnsubscribeURL" : "UnsubscribeUrl"
}

权利

在此示例中,客户从您的应用中购买了权利,然后您会收到ENTITLEMENT_PURCHASED实时通知。

{
    "Type" : "Notification",
    "MessageId" : "22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324",
    "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
    "Message": "{\"receiptId\":\"bsw3Wi6w17Ngdabggckh61kmmFhfwlfT9nPZeLfh83k=:1:11+Sheet2!A1+\",\"relatedReceipts\": {},\"appUserId\":\"kyplB1fdRX1m6/GNAVW0l6jv9Nl1V/GemSopKBHM84w=\",\"notificationType\":\"ENTITLEMENT_PURCHASED\",\"appPackageName\":\"com.playrix.gardenscapes.amazon\",\"timestamp\":1588086967189, \"betaProductTransaction\":false,}",
    "Timestamp" : "2012-05-02T00:54:06.655Z",
    "SignatureVersion" : "1",
    "Signature" : "EXAMPLEw6JRN...",
    "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
    "UnsubscribeURL" : "UnsubscribeUrl"
}

重复通知

通常,每条实时通知只会递送至每个指定终端节点一次。然而,由于Amazon SNS的分布式本质以及瞬态网络条件,偶尔可能会出现重复消息。请务必对应用进行相应设计,避免重复通知消息造成错误或不一致。

亚马逊AWS SDK的代码片段

AWS参考文档:

public class SnsServletProcessor implements HttpServletProcessor {
    private final SnsMessageManager messageParser = new SnsMessageManager("us-east-1");

    @Override
    public void process(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
        messageParser.handleMessage(httpRequest.getInputStream(), new DefaultSnsMessageHandler() {
            @Override
            public void handle(SnsNotification snsNotification) {
                  // 用于处理通知消息的业务逻辑                   
            }
        };
    }
}

Last updated: 2021年12月8日