如何使用Amazon的SNS SDK for Java处理传入的SNS通知?

huangapple go评论44阅读模式
英文:

How do I handle incoming SNS notifications using the Amazon's SNS SDK for Java?

问题

我有一个 Java 应用程序,需要处理来自 SNS 'topic' 的传入推送消息通知。这是我目前的代码:

    创建平台端点请求 endpointRequest = new CreatePlatformEndpointRequest() //https://docs.aws.amazon.com/sns/latest/dg/mobile-platform-endpoint.html
            .withToken(platformApplicationArnToken)
            .withPlatformApplicationArn(platformApplicationArn)
            .withCustomUserData("此平台端点由 " + applicationInstanceName + " 使用");
    CreatePlatformEndpointResult platformEndpoint = amazonSnsClient.createPlatformEndpoint(endpointRequest);
    log.info("此应用程序实例将使用的应用程序平台端点是 " + platformEndpoint.getEndpointArn());
    订阅请求 subscribeRequest = getSubscribeRequest(emailUnsubscribeARNSettingsMap.get("topicArn"),
                                                            emailUnsubscribeARNSettingsMap.get("protocol"),
                                                            platformEndpoint.getEndpointArn());
    订阅结果 result = amazonSnsClient.subscribe(subscribeRequest);

我可以使用 AmazonSNS 客户端的 subscribe 方法订阅主题。但是我不知道该如何编写一个处理程序,在应用程序通过其订阅接收到通知时自动调用。

我查看了 AmazonSNS 客户端具有的所有方法,但没有一个允许您为传入通知编写处理程序。您可以使用 publish() 方法从应用程序向已订阅的主题推送通知,但这不是我想要的。

因此,使用 SNS SDK,我该如何处理传入的推送通知?

附言:我正在使用推送消息,但我拥有的应用程序不是移动应用程序。它是运行在 EC2 实例上的常规 Java 应用程序。我想使用推送消息而不是通过 Http/s 发送通知,因为推送消息有更大的免费套餐,而且更便宜。

英文:

I have a java application that needs to process incoming push message notifications coming from an SNS topic. This is the code that I have so far:

    CreatePlatformEndpointRequest endpointRequest = new CreatePlatformEndpointRequest() //https://docs.aws.amazon.com/sns/latest/dg/mobile-platform-endpoint.html
            .withToken(platformApplicationArnToken)
            .withPlatformApplicationArn(platformApplicationArn)
            .withCustomUserData("This platform endpoint is used by " + applicationInstanceName);
    CreatePlatformEndpointResult platformEndpoint = amazonSnsClient.createPlatformEndpoint(endpointRequest);
    log.info("The Application Platform endpoint that this application instance will use is " + platformEndpoint.getEndpointArn());
    SubscribeRequest subscribeRequest = getSubscribeRequest(emailUnsubscribeARNSettingsMap.get("topicArn"),
                                                            emailUnsubscribeARNSettingsMap.get("protocol"),
                                                            platformEndpoint.getEndpointArn());
    SubscribeResult result = amazonSnsClient.subscribe(subscribeRequest);

I'm able to subscribe to a topic using the AmazonSNS client's subscribe method. But i have no idea how i should write a handler that is automatically invoked as soon as the app gets a notification through its subscription.

I've looked at all the methods that the AmazonSNS client has but none of them lets you write a handler for incoming notifications. You can push notifications from the app to the subscribed topic using the publish() method but that's not what I want.

So, using the SNS SDK, how do i process incoming push notifications?

PS: I am using Push messages but the app I have is not a mobile app. It is a regular java application that runs on an EC2 instance. I want to use Push messages instead of notifications via Http/s because push messages have a larger free tier and are also cheaper.

答案1

得分: 0

正如您所述 - SNS Java Client不支持您描述的功能。您可以使用该客户端订阅主题,发布消息等等。然而,它不支持事件处理程序的使用。

在使用Java和这个AWS服务时,您应该使用AWS SDK for Java v2。您目前使用的V1并非是最佳实践,如AWS SDK页面所述。第一个链接引用了V2的SNS Java客户端。

英文:

As you stated - the SNS Java Client does not support what you are describing. You can use the client to subscribe to topics, publish messages, and so on. However, it does not support use of an event handler.

When working with Java and this AWS service, you should use AWS SDK for Java v2. The V1 which you are using is NOT Best practice as described in this AWS SDK Page. The 1st link references the V2 SNS Java client.

huangapple
  • 本文由 发表于 2023年4月17日 07:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定