行動應用程式屬性 - Amazon Simple Notification Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

行動應用程式屬性

Amazon Simple Notification Service (Amazon SNS) 提供支援記錄推送通知訊息的傳遞狀態。在您設定應用程式屬性後,對於從 Amazon SNS 傳送至行動端點的訊息,日誌項目將會傳送至 CloudWatch Logs。記錄訊息傳遞狀態有助於提供深入的營運見解,例如下列項目:

  • 得知推送通知訊息是否是從 Amazon SNS 傳送至推送通知服務。

  • 識別從推送通知服務傳送至 Amazon SNS 的回應。

  • 判定訊息暫留時間 (發佈時間戳記到就在遞交至推送通知服務前的時間)。

若要設定訊息傳遞狀態的應用程式屬性,您可以使用 AWS Management Console、AWS 軟體開發套件 (SDK) 或查詢 API。

使用 AWS Management Console 設定訊息傳遞狀態屬性

  1. 登入 Amazon SNS 主控台

  2. 在導覽面板中,選擇 Mobile (行動裝置)、Push notifications (推送通知)。

  3. 平台應用程式區段中,選擇應用程式,而此應用程式包含您想要接收 CloudWatch Logs 的端點。

  4. 選擇 Application Actions (應用程式動作),然後選擇 Delivery Status (交付狀態)。

  5. Delivery Status (交付狀態) 對話方塊中,選擇 Create IAM Roles (建立 IAM 角色)。

    然後您會被重新引導至 IAM 主控台。

  6. 選擇 Allow (允許) 以提供代表您使用 CloudWatch Logs 的 Amazon SNS 寫入存取權。

  7. 現在,返回 Delivery Status (傳遞狀態) 對話方塊,在 Percentage of Success to Sample (0-100) (成功取樣的百分比 (0-100)) 欄位中針對您要接收 CloudWatch Logs 的成功傳送訊息的百分比輸入數字。

    注意

    在您設定訊息傳遞狀態的應用程式屬性後,所有訊息傳送失敗的都會產生 CloudWatch Logs。

  8. 最後,選擇 Save Configuration (儲存組態)。您現在可以檢視和剖析包含訊息傳遞狀態的 CloudWatch Logs。如需使用 CloudWatch 的詳細資訊,請參閱 CloudWatch 文件

Amazon SNS 訊息傳遞狀態 CloudWatch Logs 範例

在您設定應用程式端點的訊息傳遞狀態屬性後,將會產生 CloudWatch Logs。JSON 格式的範例日誌顯示如下:

成功

{ "status": "SUCCESS", "notification": { "timestamp": "2015-01-26 23:07:39.54", "messageId": "9655abe4-6ed6-5734-89f7-e6a6a42de02a" }, "delivery": { "statusCode": 200, "dwellTimeMs": 65, "token": "Examplei7fFachkJ1xjlqT64RaBkcGHochmf1VQAr9k-IBJtKjp7fedYPzEwT_Pq3Tu0lroqro1cwWJUvgkcPPYcaXCpPWmG3Bqn-wiqIEzp5zZ7y_jsM0PKPxKhddCzx6paEsyay9Zn3D4wNUJb8m6HXrBf9dqaEw", "attempts": 1, "providerResponse": "{\"multicast_id\":5138139752481671853,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1422313659698010%d6ba8edff9fd7ecd\"}]}", "destination": "arn:aws:sns:us-east-2:111122223333:endpoint/FCM/FCMPushApp/c23e42de-3699-3639-84dd-65f84474629d" } }

失敗

{ "status": "FAILURE", "notification": { "timestamp": "2015-01-26 23:29:35.678", "messageId": "c3ad79b0-8996-550a-8bfa-24f05989898f" }, "delivery": { "statusCode": 8, "dwellTimeMs": 1451, "token": "examp1e29z6j5c4df46f80189c4c83fjcgf7f6257e98542d2jt3395kj73", "attempts": 1, "providerResponse": "NotificationErrorResponse(command=8, status=InvalidToken, id=1, cause=null)", "destination": "arn:aws:sns:us-east-2:111122223333:endpoint/APNS_SANDBOX/APNSPushApp/986cb8a1-4f6b-34b1-9a1b-d9e9cb553944" } }

如需推送通知服務回應代碼的清單,請參閱 平台回應代碼

使用 AWS 開發套件設定訊息傳遞狀態屬性

AWS 開發套件提供數種語言的 API,以搭配 Amazon SNS 使用訊息傳遞狀態屬性。

以下 Java 範例顯示如何使用 SetPlatformApplicationAttributes API,為推送通知訊息的訊息傳遞狀態設定應用程式屬性。您可以使用下列訊息傳遞狀態的屬性:SuccessFeedbackRoleArnFailureFeedbackRoleArnSuccessFeedbackSampleRate SuccessFeedbackRoleArnFailureFeedbackRoleArn 屬性是用來提供 Amazon SNS 寫入存取權,以代表您使用 CloudWatch Logs。SuccessFeedbackSampleRate 屬性用於指定成功傳送訊息的取樣率百分比 (0-100)。在您設定 FailureFeedbackRoleArn 屬性後,則所有傳送失敗的訊息都會產生 CloudWatch Logs。

SetPlatformApplicationAttributesRequest setPlatformApplicationAttributesRequest = new SetPlatformApplicationAttributesRequest(); Map<String, String> attributes = new HashMap<>(); attributes.put("SuccessFeedbackRoleArn", "arn:aws:iam::111122223333:role/SNS_CWlogs"); attributes.put("FailureFeedbackRoleArn", "arn:aws:iam::111122223333:role/SNS_CWlogs"); attributes.put("SuccessFeedbackSampleRate", "5"); setPlatformApplicationAttributesRequest.withAttributes(attributes); setPlatformApplicationAttributesRequest.setPlatformApplicationArn("arn:aws:sns:us-west-2:111122223333:app/FCM/FCMPushApp"); sns.setPlatformApplicationAttributes(setPlatformApplicationAttributesRequest);

如需適用於 Java 的開發套件詳細資訊,請參閱 AWS SDK for Java 入門

平台回應代碼

以下是推送通知服務回應代碼的連結清單:

推送通知服務 回應代碼
Amazon Device Messaging (ADM) 請參閱 ADM 說明文件中的回應格式
Apple Push Notification Service (APN) 請參閱本機與遠端通知程式設計指南與 APN 通訊中的來自 APN 的 HTTP/2 回應
Firebase Cloud Messaging (FCM) 請參閱 Firebase Cloud Messaging 文件中下游訊息錯誤回應代碼
適用於 Windows 手機的微軟推送通知服務 (MPNS) 請參閱 Windows 8 開發文件的 Windows Phone 8 的推送通知服務回應代碼
Windows 推送通知服務 (WNS) 請參閱在 Windows 8 開發文件的推送通知服務請求和回應標頭 (Windows Runtime Apps) 中的「回應代碼」。