AWS SNS推送服务

pom.xml

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

nacos配置自定义参数
电话国家代码参考

aws:
  sns:
    access-key-id: ****
    secret-key: ****
    region: ap-northeast-1
    phone-country-code: +44
@Data
@Component
@ConfigurationProperties(prefix = "aws.sns")
public class AwsSNSProperties {

	@ApiModelProperty("accessKeyId")
	private String accessKeyId;
	
	@ApiModelProperty("secretKey")
	private String secretKey;
	
	@ApiModelProperty("地区")
	private String region;
	
	@ApiModelProperty("电话国家代码(中国 +44)")
	private String phoneCountryCode;
}

入参实体类

@Setter
@Getter
@ToString
public class SmsVo {

	@ApiModelProperty(value = "手机号码")
	private String phoneNumber;

	@ApiModelProperty(value = "消息")
	private String messages;
}

实现工具类

@Log4j2
public class AwSNSUtil {

	private static AwsSNSProperties awsSNSProperties = SpringContextHolder.getBean(AwsSNSProperties.class);

	public static R sendSMS(SmsVo smsVo) {

		if (smsVo == null) {
			return R.failed(MessageUtils.message("upms.validation.msg0023"));
		}
		/**
		 * 短信属性的设置
		 */
		Map<String, MessageAttributeValue> smsAttributes = Maps.newHashMap();
		// 设置发件人 ID
		smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
				.withStringValue("1")
				.withDataType("String"));
		// 设置源电话号码
		smsAttributes.put("AWS.MM.SMS.OriginationNumber", new MessageAttributeValue()
				.withStringValue("13333333333")
				.withDataType("String"));
		// 设置单条最高价格
		smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
				.withStringValue("0.07")
				.withDataType("Number"));
		// 设置发送消息类型
		smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue()
				.withStringValue("Transactional")
				.withDataType("String"));

		// 通过 accessKeyId、secretKey 生成认证的aws凭证对象
		BasicAWSCredentials credentials = new BasicAWSCredentials(
				awsSNSProperties.getAccessKeyId(), awsSNSProperties.getSecretKey());

		// 通过凭证对象 awsCredentials 跟 区域 region 生成sns的服务端
		AmazonSNS sns = AmazonSNSClientBuilder.standard()
				.withRegion(awsSNSProperties.getRegion())
				.withCredentials(new AWSStaticCredentialsProvider(credentials))
				.build();
		try {
			PublishRequest request = new PublishRequest()
					.withMessage(smsVo.getMessages())
					.withPhoneNumber(smsVo.getPhoneNumber());
//					.withMessageAttributes(smsAttributes);

			PublishResult result = sns.publish(request);
			String messageId = result.getMessageId();
			return R.ok(messageId);

		} catch (Exception e) {
			log.error("AWS sendSms " + smsVo.getPhoneNumber() + " error={}", e.getMessage());
			return R.failed(MessageUtils.message("upms.validation.msg0024"));

		} finally {
			sns.shutdown();
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值