1
我試圖實施Amazon SNS
服務到我的項目。我能夠創建主題並訂閱主題。在這裏,我使用sms
作爲和phone number
作爲endpoint
。主要問題是我沒有得到我的手機號碼的確認並且無法發佈消息。我怎樣才能得到token
到confirmSubscription
? //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);
}
}