Amazon SNS examples using SDK for Java 2.x - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

Amazon SNS examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Amazon SNS.

Actions are code excerpts that show you how to call individual Amazon SNS functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon SNS functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to add tags to an Amazon SNS topic.

SDK for Java 2.x

public static void addTopicTags(SnsClient snsClient, String topicArn) { try { Tag tag = Tag.builder() .key("Team") .value("Development") .build(); Tag tag2 = Tag.builder() .key("Environment") .value("Gamma") .build(); List<Tag> tagList = new ArrayList<>(); tagList.add(tag); tagList.add(tag2); TagResourceRequest tagResourceRequest = TagResourceRequest.builder() .resourceArn(topicArn) .tags(tagList) .build(); snsClient.tagResource(tagResourceRequest); System.out.println("Tags have been added to "+topicArn); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see TagResource in AWS SDK for Java 2.x API Reference.

The following code example shows how to check whether a phone number is opted out of receiving Amazon SNS messages.

SDK for Java 2.x

public static void checkPhone(SnsClient snsClient, String phoneNumber) { try { CheckIfPhoneNumberIsOptedOutRequest request = CheckIfPhoneNumberIsOptedOutRequest.builder() .phoneNumber(phoneNumber) .build(); CheckIfPhoneNumberIsOptedOutResponse result = snsClient.checkIfPhoneNumberIsOptedOut(request); System.out.println(result.isOptedOut() + "Phone Number " + phoneNumber + " has Opted Out of receiving sns messages." + "\n\nStatus was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to confirm the owner of an endpoint wants to receive Amazon SNS messages by validating the token sent to the endpoint by an earlier Subscribe action.

SDK for Java 2.x

public static void confirmSub(SnsClient snsClient, String subscriptionToken, String topicArn ) { try { ConfirmSubscriptionRequest request = ConfirmSubscriptionRequest.builder() .token(subscriptionToken) .topicArn(topicArn) .build(); ConfirmSubscriptionResponse result = snsClient.confirmSubscription(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription Arn: \n\n" + result.subscriptionArn()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to create an Amazon SNS topic.

SDK for Java 2.x

public static String createSNSTopic(SnsClient snsClient, String topicName ) { CreateTopicResponse result = null; try { CreateTopicRequest request = CreateTopicRequest.builder() .name(topicName) .build(); result = snsClient.createTopic(request); return result.topicArn(); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see CreateTopic in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete an Amazon SNS subscription.

SDK for Java 2.x

public static void unSub(SnsClient snsClient, String subscriptionArn) { try { UnsubscribeRequest request = UnsubscribeRequest.builder() .subscriptionArn(subscriptionArn) .build(); UnsubscribeResponse result = snsClient.unsubscribe(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription was removed for " + request.subscriptionArn()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Unsubscribe in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete an Amazon SNS topic and all subscriptions to that topic.

SDK for Java 2.x

public static void deleteSNSTopic(SnsClient snsClient, String topicArn ) { try { DeleteTopicRequest request = DeleteTopicRequest.builder() .topicArn(topicArn) .build(); DeleteTopicResponse result = snsClient.deleteTopic(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DeleteTopic in AWS SDK for Java 2.x API Reference.

The following code example shows how to get the properties of an Amazon SNS topic.

SDK for Java 2.x

public static void getSNSTopicAttributes(SnsClient snsClient, String topicArn ) { try { GetTopicAttributesRequest request = GetTopicAttributesRequest.builder() .topicArn(topicArn) .build(); GetTopicAttributesResponse result = snsClient.getTopicAttributes(request); System.out.println("\n\nStatus is " + result.sdkHttpResponse().statusCode() + "\n\nAttributes: \n\n" + result.attributes()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see GetTopicAttributes in AWS SDK for Java 2.x API Reference.

The following code example shows how to get the settings for sending Amazon SNS SMS messages.

SDK for Java 2.x

public static void getSNSAttrutes(SnsClient snsClient,String topicArn ) { try { GetSubscriptionAttributesRequest request = GetSubscriptionAttributesRequest.builder() .subscriptionArn(topicArn) .build(); // Get the Subscription attributes GetSubscriptionAttributesResponse res = snsClient.getSubscriptionAttributes(request); Map<String, String> map = res.attributes(); // Iterate through the map Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue()); } } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("\n\nStatus was good"); }
  • Find instructions and more code on GitHub.

  • For API details, see GetSMSAttributes in AWS SDK for Java 2.x API Reference.

The following code example shows how to list phone numbers that are opted out of receiving Amazon SNS messages.

SDK for Java 2.x

public static void listOpts( SnsClient snsClient) { try { ListPhoneNumbersOptedOutRequest request = ListPhoneNumbersOptedOutRequest.builder().build(); ListPhoneNumbersOptedOutResponse result = snsClient.listPhoneNumbersOptedOut(request); System.out.println("Status is " + result.sdkHttpResponse().statusCode() + "\n\nPhone Numbers: \n\n" + result.phoneNumbers()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to retrieve the list of subscribers of an Amazon SNS topic.

SDK for Java 2.x

public static void listSNSSubscriptions( SnsClient snsClient) { try { ListSubscriptionsRequest request = ListSubscriptionsRequest.builder() .build(); ListSubscriptionsResponse result = snsClient.listSubscriptions(request); System.out.println(result.subscriptions()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see ListSubscriptions in AWS SDK for Java 2.x API Reference.

The following code example shows how to list Amazon SNS topics.

SDK for Java 2.x

public static void listSNSTopics(SnsClient snsClient) { try { ListTopicsRequest request = ListTopicsRequest.builder() .build(); ListTopicsResponse result = snsClient.listTopics(request); System.out.println("Status was " + result.sdkHttpResponse().statusCode() + "\n\nTopics\n\n" + result.topics()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see ListTopics in AWS SDK for Java 2.x API Reference.

The following code example shows how to publish SMS messages using Amazon SNS.

SDK for Java 2.x

public static void pubTextSMS(SnsClient snsClient, String message, String phoneNumber) { try { PublishRequest request = PublishRequest.builder() .message(message) .phoneNumber(phoneNumber) .build(); PublishResponse result = snsClient.publish(request); System.out.println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Publish in AWS SDK for Java 2.x API Reference.

The following code example shows how to publish messages to an Amazon SNS topic.

SDK for Java 2.x

public static void pubTopic(SnsClient snsClient, String message, String topicArn) { try { PublishRequest request = PublishRequest.builder() .message(message) .topicArn(topicArn) .build(); PublishResponse result = snsClient.publish(request); System.out.println(result.messageId() + " Message sent. Status is " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Publish in AWS SDK for Java 2.x API Reference.

The following code example shows how to set an Amazon SNS filter policy.

SDK for Java 2.x

public static void usePolicy(SnsClient snsClient, String subscriptionArn) { try { SNSMessageFilterPolicy fp = new SNSMessageFilterPolicy(); // Add a filter policy attribute with a single value fp.addAttribute("store", "example_corp"); fp.addAttribute("event", "order_placed"); // Add a prefix attribute fp.addAttributePrefix("customer_interests", "bas"); // Add an anything-but attribute fp.addAttributeAnythingBut("customer_interests", "baseball"); // Add a filter policy attribute with a list of values ArrayList<String> attributeValues = new ArrayList<>(); attributeValues.add("rugby"); attributeValues.add("soccer"); attributeValues.add("hockey"); fp.addAttribute("customer_interests", attributeValues); // Add a numeric attribute fp.addAttribute("price_usd", "=", 0); // Add a numeric attribute with a range fp.addAttributeRange("price_usd", ">", 0, "<=", 100); // Apply the filter policy attributes to an Amazon SNS subscription fp.apply(snsClient, subscriptionArn); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to set the default settings for sending SMS messages using Amazon SNS.

SDK for Java 2.x

public static void setSNSAttributes( SnsClient snsClient, HashMap<String, String> attributes) { try { SetSmsAttributesRequest request = SetSmsAttributesRequest.builder() .attributes(attributes) .build(); SetSmsAttributesResponse result = snsClient.setSMSAttributes(request); System.out.println("Set default Attributes to " + attributes + ". Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see SetSmsAttributes in AWS SDK for Java 2.x API Reference.

The following code example shows how to set Amazon SNS topic attributes.

SDK for Java 2.x

public static void setTopAttr(SnsClient snsClient, String attribute, String topicArn, String value) { try { SetTopicAttributesRequest request = SetTopicAttributesRequest.builder() .attributeName(attribute) .attributeValue(value) .topicArn(topicArn) .build(); SetTopicAttributesResponse result = snsClient.setTopicAttributes(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nTopic " + request.topicArn() + " updated " + request.attributeName() + " to " + request.attributeValue()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see SetTopicAttributes in AWS SDK for Java 2.x API Reference.

The following code example shows how to subscribe a Lambda function so it receives notifications from an Amazon SNS topic.

SDK for Java 2.x

public static String subLambda(SnsClient snsClient, String topicArn, String lambdaArn) { try { SubscribeRequest request = SubscribeRequest.builder() .protocol("lambda") .endpoint(lambdaArn) .returnSubscriptionArn(true) .topicArn(topicArn) .build(); SubscribeResponse result = snsClient.subscribe(request); return result.subscriptionArn(); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see Subscribe in AWS SDK for Java 2.x API Reference.

The following code example shows how to subscribe an HTTP or HTTPS endpoint so it receives notifications from an Amazon SNS topic.

SDK for Java 2.x

public static void subHTTPS(SnsClient snsClient, String topicArn, String url ) { try { SubscribeRequest request = SubscribeRequest.builder() .protocol("http") .endpoint(url) .returnSubscriptionArn(true) .topicArn(topicArn) .build(); SubscribeResponse result = snsClient.subscribe(request); System.out.println("Subscription ARN is " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Subscribe in AWS SDK for Java 2.x API Reference.

The following code example shows how to subscribe an email address to an Amazon SNS topic.

SDK for Java 2.x

public static void subEmail(SnsClient snsClient, String topicArn, String email) { try { SubscribeRequest request = SubscribeRequest.builder() .protocol("email") .endpoint(email) .returnSubscriptionArn(true) .topicArn(topicArn) .build(); SubscribeResponse result = snsClient.subscribe(request); System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Subscribe in AWS SDK for Java 2.x API Reference.