gpt4 book ai didi

java - 使用 aws sns 服务发送短信

转载 作者:行者123 更新时间:2023-11-30 06:46:34 24 4
gpt4 key购买 nike

我想编写一个代码来使用 AWS 服务向客户端发送 SMS 我已经编写了一个消息推送代码,它也给了我 200 成功响应。但无法获取向特定用户发送消息的方式。

公共(public)类亚马逊短信{

AWS 凭证 -- 替换为您的凭证静态字符串 ACCESS_KEY = "AKIAIQOC7Y************";静态字符串 SECRET_KEY = "S2e4CwxUaZJZc****************";

 Sender loop
public static void main(String... args) throws Exception {

// Create a client
AmazonSNSClient service = new AmazonSNSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));

// Create a topic
CreateTopicRequest createReq = new CreateTopicRequest()
.withName("MyTopic");
CreateTopicResult createRes = service.createTopic(createReq);

for (;;) {

// Publish to a topic
PublishRequest publishReq = new PublishRequest()
.withTopicArn(createRes.getTopicArn())
.withMessage("Example notification sent at " + new Date());
service.publish(publishReq);

Thread.sleep(1000);
}
}

}

提前致谢。

最佳答案

根据documentation ,为了向某个主题发送短信,您需要CreateTopic订阅一个电话号码(缺少),然后Publish 向订阅该主题的每个电话号码发送消息。要订阅您的主题的号码,您可以执行以下操作:

public static void main(String[] args) {
AmazonSNSClient snsClient = new AmazonSNSClient();
String phoneNumber = "+1XXX5550100";
String topicArn = createSNSTopic(snsClient);
subscribeToTopic(snsClient, topicArn, "sms", phoneNumber);
}

public static void subscribeToTopic(AmazonSNSClient snsClient, String topicArn, String protocol, String endpoint) {
SubscribeRequest subscribe = new SubscribeRequest(topicArn, protocol, endpoint);
SubscribeResult subscribeResult = snsClient.subscribe(subscribe);
System.out.println("Subscribe request: " +
snsClient.getCachedResponseMetadata(subscribe));
System.out.println("Subscribe result: " + subscribeResult);
}

您的其余代码看起来不错。

关于java - 使用 aws sns 服务发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43588091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com