sending sms from java web app using aws sns

只谈情不闲聊 提交于 2021-02-06 13:00:42

问题


A user will create an account on my web app. The app will need to authenticate the user by sending a text message to the mobile phone number that the user provides. The text message is a short unique code, which the user will need to type in to the web browser in order for the app to authenticate the user.

How can I configure Amazon AWS SNS for this use case?

From what I have read, SNS works by the webmaster selecting a topic and then each user subscribes to that topic. Then the webmaster sends messages broadcast style to all the subscribers to the topic. This would not meet my requirements of sending a unique message to each user. Alternatively, creating a separate topic for every phone number would be too cumbersome, not to mention creating security issues with respect to protecting the privacy of all the phone numbers.

Can Amazon AWS SNS be configured for this use case? If so, how?

I am using Java.


回答1:


SNS topics are only nedded when you need to send a SMS message to multiple mobile numbers. If you want to send a SMS message to a single number you can use the Amazon AWS SDK.

Docs are available at http://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk.

Example code:

public static void main(String[] args) {
    AWSCredentials awsCredentials = new BasicAWSCredentials("YOUR_Client_ID", "YOUR_Client_secret");
    final AmazonSNSClient client = new AmazonSNSClient(awsCredentials);
    client.setRegion(Region.getRegion(Regions.REGION_YOU_WANT_TO_USE));

    AmazonSNSClient snsClient = new AmazonSNSClient(awsCredentials);
    String message = "My SMS message";
    String phoneNumber = "+1XXX5550100";
    Map<String, MessageAttributeValue> smsAttributes = 
            new HashMap<String, MessageAttributeValue>();
    //<set SMS attributes>
    sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}

public static void sendSMSMessage(AmazonSNSClient snsClient, String message, 
    String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
    PublishResult result = snsClient.publish(new PublishRequest()
                    .withMessage(message)
                    .withPhoneNumber(phoneNumber)
                    .withMessageAttributes(smsAttributes));
    System.out.println(result); // Prints the message ID.
}

Remember to create an user on IAM console adding a permission to access the SNS Service. You need to add AmazonSNSFullAccess role.

Replace YOUR_Client_ID and YOUR_Client_secret with user credentials you have created.




回答2:


Some methods have been deprecated in AWS SDK.

BasicAWSCredentials awsCredentials = new BasicAWSCredentials("CLIENT-ID", "SECRET-KEY");
AmazonSNS snsClient = AmazonSNSClientBuilder.standard()
                     .withRegion(Regions.fromName("YOUR_REGION"))
     .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();

Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
smsAttributes.put("AWS.SNS.SMS.SenderID",new MessageAttributeValue().withStringValue("SENDER-ID").withDataType("String");
smsAttributes.put("AWS.SNS.SMS.SMSType",new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));

PublishRequest request = new PublishRequest();
request.withMessage("YOUR MESSAGE")
.withPhoneNumber("E.164-PhoneNumber")
.withMessageAttributes(smsAttributes);
PublishResult result=snsClient.publish(request);



回答3:


Sending an SMS using the AWS SDK for Java:

public static void main(String[] args) {
        AmazonSNSClient snsClient = new AmazonSNSClient();
        String message = "My SMS message";
        String phoneNumber = "+1XXX5550100";
        Map<String, MessageAttributeValue> smsAttributes = 
                new HashMap<String, MessageAttributeValue>();
        //<set SMS attributes>
        sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}

public static void sendSMSMessage(AmazonSNSClient snsClient, String message, 
        String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
        PublishResult result = snsClient.publish(new PublishRequest()
                        .withMessage(message)
                        .withPhoneNumber(phoneNumber)
                        .withMessageAttributes(smsAttributes));
        System.out.println(result); // Prints the message ID.
}

For this particular use case you should consider the AWS Customer Engagement Service Amazon Pinpoint. It allows sending SMS, emails and push notifications, with tools for marketing campaigns as well as analytics:

Amazon Pinpoint Developer Guide

The documentation includes a tutorial for your specific use case:

Setting Up an SMS Registration System Using Amazon Pinpoint

For what you described, while it could be done using SNS with the AWS SDK for Java, Pinpoint is designed to interact with application users, so it's probably a better alternative




回答4:


Individual SMS/MMS messaging is outside the apparent use cases of AWS SNS.

If you need to send proper SMS/MMS text messages to arbitrary phone numbers, you'll need to turn to other platforms, such as Twilio or Bandwidth.



来源:https://stackoverflow.com/questions/31466916/sending-sms-from-java-web-app-using-aws-sns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!