sending sms from java web app using aws sns

后端 未结 4 1881
星月不相逢
星月不相逢 2021-02-10 18:50

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 m

4条回答
  •  无人及你
    2021-02-10 18:59

    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 smsAttributes = 
                new HashMap();
        //
        sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
    }
    
    public static void sendSMSMessage(AmazonSNSClient snsClient, String message, 
        String phoneNumber, Map 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.

提交回复
热议问题