简体   繁体   English

使用 aws sns 服务发送短信

[英]Sending SMS using aws sns service

I want to formulate a code to send the SMS to the client using AWS service i have written a code for message push and its also giving me 200 success response.我想制定一个代码来使用 AWS 服务将 SMS 发送到客户端我已经编写了一个消息推送代码,它也给了我 200 个成功响应。 But unable to get the way to send message to particular user.但无法获得向特定用户发送消息的方式。

public class Amazonsms {公共 class Amazonsms {

AWS credentials -- replace with your credentials static String ACCESS_KEY = "AKIAIQOC7Y**********"; AWS 凭据——替换为您的凭据 static String ACCESS_KEY = "AKIAIQOC7Y**********"; static String SECRET_KEY = "S2e4CwxUaZJZc***************"; static 字符串 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);
    }
}

} }

Thanks in Advance.提前致谢。

According to the documentation , in order to send an SMS to a topic, you need to CreateTopic , Subscribe a phone number to it (which is missing), and then Publish messages to each phone number subscribed to the topic.根据文档,为了向主题发送短信,您需要创建主题, CreateTopic Subscribe一个电话号码(缺少),然后向订阅该主题的每个电话号码Publish消息。 For subscribing a number to your topic you can do the following:要为您的主题订阅一个号码,您可以执行以下操作:

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);
}

The rest of your code seems fine.您的代码的 rest 似乎没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM