본문 바로가기
SpringBoot

Send Global sms with AWS SMS

by ByteBridge 2018. 8. 23.
반응형




* 아마존 SMS 보내기

* 해당 기능은 현재 국내 Region 은 사용 할 수 없음.

* 제공되는 Region 을 선택 하도록 한다. (ex: AP_NORTHEAST_1)

* 보내는 사람의 전화번호는 설정 할수 없음(확인필요)

* AWS-SMS Dashboard:

    https://ap-northeast-1.console.aws.amazon.com/sns/v2/home?region=ap-northeast-1#/text-messaging

* 해당 기능을 사용하기 위해서는 IAM 설정과 ACCESSKEY,SECRETKEY 발급 필요

* 참고 URL:

    https://console.aws.amazon.com/iam/home?region=ap-northeast-1#/roles/sms


* maven dependency:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.11.255</version>
</dependency>

* Test 코드


@Test
public void awsSendSMS() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials("ACCESSKEY", "SECRETKEY");
AmazonSNSClient snsClient = new AmazonSNSClient(awsCreds);
snsClient.setRegion(Region.getRegion(Regions.MY_REGION));
String message = "My SMS message";
String phoneNumber = "+<Country calling code><phopnenumber>";
Map<String, MessageAttributeValue> smsAttributes = new HashMap<>();
smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
.withStringValue("Optional") //The sender ID shown on the device.
.withDataType("String"));
smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
.withStringValue("0.50") //Sets the max price to 0.50 USD.
.withDataType("Number"));
smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue()
.withStringValue("Promotional") //Sets the type to promotional.
.withDataType("String"));
//<set SMS attributes>
sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}
private 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.
}


반응형