2017-06-14 132 views
1

我試圖實施Amazon SNS服務到我的項目。我能夠創建主題並訂閱主題。在這裏,我使用sms作爲和phone number作爲endpoint。主要問題是我沒有得到我的手機號碼的確認並且無法發佈消息。我怎樣才能得到tokenconfirmSubscription//subscriptionRequest.withToken(token);亞馬遜SNS確認令牌

public class SNSServiceImpl implements SNSService { 
    private static final Logger logger = LoggerFactory.getLogger(SNSServiceImpl.class); 

    @Value("${AWS_ACCESS_KEY_ID:key}") 
    private String awsAccessKeyId; 
    @Value("${AWS_SECRET_ACCESS_KEY:secret}") 
    private String awsAccessKeySecret; 
    @Value("${AWS_REGION:us-west-2}") 
    private String awsRegion; 

    private AmazonSNS snsClient; 
    private String subscriptionArn; 

    @PostConstruct 
    public void setConnection() { 
     AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKeyId, awsAccessKeySecret); 
     AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials); 
     snsClient = AmazonSNSClient.builder() 
       .withRegion(awsRegion) 
       .withCredentials(awsCredentialsProvider) 
       .build(); 
    } 

    @Override 
    public String createTopic(String topicName) { 
     CreateTopicRequest topicRequest = new CreateTopicRequest(topicName); 
     CreateTopicResult topicResult = snsClient.createTopic(topicRequest); 
     return topicResult.getTopicArn(); 
    } 

    @Override 
    public void subscribeToTopic(String topicArn) { 
     SubscribeRequest subscribeRequest = new SubscribeRequest(); 
     subscribeRequest.setTopicArn(topicArn); 
     subscribeRequest.setProtocol("sms"); 
     subscribeRequest.setEndpoint("+14699011920"); 
     SubscribeResult subscribeResult = snsClient.subscribe(subscribeRequest); 

     subscriptionArn = subscribeResult.getSubscriptionArn(); 
    } 

    @Override 
    public void confirmSubscription(String topicArn) { 

     ConfirmSubscriptionRequest subscriptionRequest = new ConfirmSubscriptionRequest(); 
     subscriptionRequest.withTopicArn(topicArn); 
     //subscriptionRequest.withToken(token); 

     ConfirmSubscriptionResult confirmSubscriptionResult = snsClient.confirmSubscription(subscriptionRequest); 
     confirmSubscriptionResult.withSubscriptionArn(subscriptionArn); 
    } 

    @Override 
    public void publishMessage(String topicArn, String message) { 
     PublishRequest publishRequest = new PublishRequest(topicArn, message); 
     PublishResult publishResult = snsClient.publish(publishRequest); 

     System.out.println(publishResult); 

    } 
} 

回答

3

我只是改變了publishMessage方法,並從我的服務中刪除confirmSubscription此方法。

@Component 
public class SNSServiceImpl implements SNSService { 
    private static final Logger logger = LoggerFactory.getLogger(SNSServiceImpl.class); 

    @Value("${AWS_ACCESS_KEY_ID:key}") 
    private String awsAccessKeyId; 
    @Value("${AWS_SECRET_ACCESS_KEY:secret}") 
    private String awsAccessKeySecret; 
    @Value("${AWS_REGION:us-west-2}") 
    private String awsRegion; 

    private AmazonSNS snsClient; 

    @PostConstruct 
    public void setConnection() { 
     AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKeyId, awsAccessKeySecret); 
     AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials); 
     snsClient = AmazonSNSClient.builder() 
       .withRegion(awsRegion) 
       .withCredentials(awsCredentialsProvider) 
       .build(); 
    } 

    @Override 
    public String createTopic(String topicName) { 
     CreateTopicRequest topicRequest = new CreateTopicRequest(topicName); 
     CreateTopicResult topicResult = snsClient.createTopic(topicRequest); 

     logger.info("Create topic request: " + snsClient.getCachedResponseMetadata(topicRequest)); 
     logger.info("Create topic result: " + topicResult); 
     return topicResult.getTopicArn(); 
    } 

    @Override 
    public void subscribeSNSToTopic(String topicArn, String phoneNumber) { 
     String protocol = "sms"; 
     SubscribeRequest subscribeRequest = new SubscribeRequest(topicArn, protocol, phoneNumber); 
     SubscribeResult subscribeResult = snsClient.subscribe(subscribeRequest); 

     logger.info("Subscribe request: " + snsClient.getCachedResponseMetadata(subscribeRequest)); 
     logger.info("Subscribe result: " + subscribeResult); 
    } 

    @Override 
    public void sendSMSMessageToTopic(String topicArn, String message) { 
     Map<String, MessageAttributeValue> smsAttributes = 
       new HashMap<>(); 
     smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue() 
       .withStringValue("mySenderID") //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")); 

     PublishResult publishResult = snsClient.publish(new PublishRequest() 
       .withTopicArn(topicArn) 
       .withMessage(message) 
       .withMessageAttributes(smsAttributes)); 
     logger.info("Public Result: " + publishResult); 
    } 
}