amazonSNSを利用してSMSを送信するシステムを作っています。
送信することはかないまいしたが、配信失敗時のエラーをcatchすることができず、調べたところcloudwatchというサービスからJson形式の配信ステータスを取得できるようです。
Java
1 /** 2 * SMS送信 3 * @param customerId 顧客ID 4 * @param agreementNo 同意情報NO 5 * @param telephon 電話番号 6 * @return 検索結果のHTML 7 * @throws Exception 8 */ 9 @POST 10 @GET 11 @Path("sms") 12 @Consumes({ "application/json"}) 13 @Produces({ "application/json; charset=UTF-8" }) 14 //public RestApiResponse smssend(@PathParam("customerId") String customerId, SendSmsForm form, Long agreementNo) throws Exception{ 15 public RestApiResponse authenticate (@PathParam("customerId")String customerId, String Telephone) throws Exception { 16 17 18 RestApiResponse result = new RestApiResponse(); 19 String Message = reloadableConfig.getString("message"); 20 String Time = reloadableConfig.getString("time"); 21 int Count = reloadableConfig.getInt("count"); 22 int smsflag; 23 int i = Count; 24 25 if(Telephone == null){ 26 // パラメータなし → エラー 27 result.setNG("The phone number isn't input yet."); 28 return result; // 結果を返却 29 } 30 31 try { 32 33 String telephone = null; 34 String test = null; 35 /*メッセージ内容*/ 36 37 JSONObject json = new JSONObject(Telephone); 38 telephone = json.getString("Telephone"); 39 System.out.println(telephone); 40 if(telephone.length() == 0){ 41 result.setNG("The phone number isn't input yet."); 42 return result; // 結果を返却 43 } 44 45 String message = Message;//reloadable.properties参照 46 String phoneNumber = "+1" + telephone;//固定の"+1"を付ける 47 System.out.println(message); 48 System.out.println(phoneNumber); 49 Map<String, MessageAttributeValue> smsAttributes = 50 new HashMap<String, MessageAttributeValue>(); 51 //<set SMS attributes> 52 53 String status = sendSMSMessage(snsClient, message, phoneNumber, smsAttributes); 54 if (status == "FAILURE"){ 55 result.setNG("Sending the message is failed."); 56 return result; 57 } 58 59 60 result.setOK(); 61 smsflag = 1; 62 agreementService.smsupdate(smsflag,telephone); 63 }catch(Exception e){ 64 //result.setNG("Sending the message is failed."); 65 System.out.println(e.getMessage()); 66 } 67 68 return result; 69 } 70 71 public static String sendSMSMessage(AmazonSNSClient snsClient, String message, 72 String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) { 73 74 try{ 75 PublishResult result = snsClient.publish(new PublishRequest() 76 .withMessage(message) 77 .withPhoneNumber(phoneNumber) 78 .withMessageAttributes(smsAttributes)); 79 System.out.println(result); // Prints the message ID. 80 String id = result.getMessageId(); 81 System.out.println(id); 82 int statusCode = result .getSdkHttpMetadata().getHttpStatusCode(); 83 System.out.println(statusCode); 84 ResponseMetadata response = result.getSdkResponseMetadata(); 85 System.out.println(response); 86 87 SetPlatformApplicationAttributesRequest setPlatformApplicationAttributesRequest = new SetPlatformApplicationAttributesRequest(); 88 Map<String, String> attributes = new HashMap<>(); 89 attributes.put("SuccessFeedbackRoleArn", "arn:aws:iam::111122223333:role/SNS_CWlogs"); 90 attributes.put("FailureFeedbackRoleArn", "arn:aws:iam::111122223333:role/SNS_CWlogs"); 91 attributes.put("SuccessFeedbackSampleRate", "5"); 92 attributes.put("MessageID", id); 93 setPlatformApplicationAttributesRequest.withAttributes(attributes); 94 System.out.println(attributes); 95 setPlatformApplicationAttributesRequest.setPlatformApplicationArn("arn:aws:sns:us-west-2:111122223333:app/GCM/GCMPushApp"); 96 SetPlatformApplicationAttributesResult res = snsClient.setPlatformApplicationAttributes(setPlatformApplicationAttributesRequest); 97 ResponseMetadata obj = res.getSdkResponseMetadata(); 98 99 String status = null; 100 JSONObject json = new JSONObject(obj); 101 status = json.getString("status"); 102 System.out.println(status);
上記太字の部分を追加し、指定したメッセージの配信ステータスを取得したいのですが取得することが出来ません。
有識者の方、ご教授いただけないでしょうか。
使用言語はJavaです。
宜しくお願い致します。
あなたの回答
tips
プレビュー