最新消息:文章中包含代码时,请遵守代码高亮规范!

AWS:使用 SNS 发送短信【转载】

Java 姚, 伟 338浏览 0评论
public class ShortMessage {
 private Map<String, MessageAttributeValue> smsAttributes;


 public Map<String, MessageAttributeValue> getDefaultSMSAttributes() {
 if (smsAttributes == null) {
 smsAttributes = new HashMap<>();
 smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
 .withStringValue("1")
 .withDataType("String"));
 smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
 .withStringValue("0.05")
 .withDataType("Number"));
 smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue()
 .withStringValue("Transactional")
 .withDataType("String"));
 }
 return smsAttributes;
 }

public PublishResult sendSMSMessage(String phoneNumber, String message) {
 return sendSMSMessage(phoneNumber, message, getDefaultSMSAttributes());
 }

public PublishResult sendSMSMessage(String phoneNumber, String message, Map<String, MessageAttributeValue> smsAttributes) {
 AWSCredentials awsCredentials = new AWSCredentials() {
 @Override
 public String getAWSAccessKeyId() {
 return "xxxxxxxx"; // 带有发短信权限的 IAM 的 ACCESS_KEY
 }

@Override
 public String getAWSSecretKey() {
 return "xxxxxxxx"; // 带有发短信权限的 IAM 的 SECRET_KEY
 }
 };
 AWSCredentialsProvider provider = new AWSCredentialsProvider() {
 @Override
 public AWSCredentials getCredentials() {
 return awsCredentials;
 }

@Override
 public void refresh() {
 }
 };
 AmazonSNS amazonSNS = null;
 try {
 amazonSNS = AmazonSNSClientBuilder.standard().withCredentials(provider).withRegion("us-east-1").build();
 } catch (Exception e) {

}
 return amazonSNS.publish(
 new PublishRequest()
 .withMessage(message)
 .withPhoneNumber(phoneNumber)
 .withMessageAttributes(smsAttributes)
 );
 }

public static void main(String[] args) {
 //AmazonSNSClient client = new AmazonSNSClient(credentialsProvider);
 //AWSCredentialsProviderChain chain = new AWSCredentialsProviderChain(credentialsProvider);
 //chain.setReuseLastProvider(true);
 //credentialsProvider.getCredentials();
 ShortMessage shortMessage = new ShortMessage();
 PublishResult publishResult = shortMessage.sendSMSMessage("+8613958942952", "test1");
 System.out.println(publishResult);
 }

}

转载自:https://blog.csdn.net/qq_33811662/article/details/80794588

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24640.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者van的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!