

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

# Amazon SNS 行動應用程式屬性
<a name="sns-msg-status"></a>

Amazon Simple Notification Service (Amazon SNS) 提供支援記錄推送通知訊息的傳遞狀態。在您設定應用程式屬性後，對於從 Amazon SNS 傳送至行動端點的訊息，日誌項目將會傳送至 CloudWatch Logs。記錄訊息傳遞狀態有助於提供深入的營運見解，例如下列項目：
+ 得知推送通知訊息是否是從 Amazon SNS 傳送至推送通知服務。
+ 識別從推送通知服務傳送至 Amazon SNS 的回應。
+ 判定訊息暫留時間 (發佈時間戳記到就在遞交至推送通知服務前的時間)。

 若要設定訊息傳遞狀態的應用程式屬性，您可以使用 AWS 管理主控台、 AWS 軟體開發套件 (SDKs) 或查詢 API。

## 使用 設定訊息傳遞狀態屬性 AWS 管理主控台
<a name="sns-msg-console"></a>

1. 登入 [Amazon SNS 主控台](https://console.aws.amazon.com/sns/home)。

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

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

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

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

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

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

1. 現在，返回 **Delivery Status** (傳遞狀態) 對話方塊，在 **Percentage of Success to Sample (0-100)** (成功取樣的百分比 (0-100)) 欄位中針對您要接收 CloudWatch Logs 的成功傳送訊息的百分比輸入數字。
**注意**  
在您設定訊息傳遞狀態的應用程式屬性後，所有訊息傳送失敗的都會產生 CloudWatch Logs。

1. 最後，選擇 **Save Configuration** (儲存組態)。您現在可以檢視和剖析包含訊息傳遞狀態的 CloudWatch Logs。如需使用 CloudWatch 的詳細資訊，請參閱 [CloudWatch 文件](https://aws.amazon.com/documentation/cloudwatch)。

## Amazon SNS 訊息傳遞狀態 CloudWatch Logs 範例
<a name="sns-msg-examples"></a>

在您設定應用程式端點的訊息傳遞狀態屬性後，將會產生 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"
  }
}
```

如需推送通知服務回應代碼的清單，請參閱 [平台回應代碼](#platform-returncodes)。

## 使用 AWS SDKs 設定訊息傳遞狀態屬性
<a name="sns-msg-sdk"></a>

[AWS 開發套件](https://aws.amazon.com/tools/)提供數種語言的 API，以搭配 Amazon SNS 使用訊息傳遞狀態屬性。

以下 Java 範例顯示如何使用 `SetPlatformApplicationAttributes` API，為推送通知訊息的訊息傳遞狀態設定應用程式屬性。您可以使用下列訊息傳遞狀態的屬性：`SuccessFeedbackRoleArn`、`FailureFeedbackRoleArn` 和 `SuccessFeedbackSampleRate` `SuccessFeedbackRoleArn` 和 `FailureFeedbackRoleArn` 屬性是用來提供 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 的開發套件詳細資訊，請參閱 [適用於 Java 的 AWS SDK入門](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html)。

## 平台回應代碼
<a name="platform-returncodes"></a>

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


****  

| 推送通知服務 | 回應代碼 | 
| --- | --- | 
| Amazon Device Messaging (ADM) | 請參閱 ADM 說明文件中的[回應格式](https://developer.amazon.com/docs/adm/send-message.html#response-format)。 | 
| Apple Push Notification Service (APN) | 請參閱本機與遠端通知程式設計指南的[與 APN 通訊](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#/apple_ref/doc/uid/TP40008194-CH11-SW1)中的來自 APN 的 HTTP/2 回應。 | 
| Firebase Cloud Messaging (FCM) | 請參閱 Firebase Cloud Messaging 文件中[下游訊息錯誤回應代碼](https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes)。 | 
| 適用於 Windows 手機的微軟推送通知服務 (MPNS) | 請參閱 Windows 8 開發文件的 [Windows Phone 8 的推送通知服務回應代碼](https://msdn.microsoft.com/en-us/library/windows/apps/ff941100%28v=vs.105%29.aspx#BKMK_PushNotificationServiceResponseCodes)。 | 
| Windows 推送通知服務 (WNS) | 請參閱在 Windows 8 開發文件的[推送通知服務請求和回應標頭 (Windows Runtime Apps)](https://msdn.microsoft.com/en-us/library/windows/apps/hh465435.aspx) 中的「回應代碼」。 | 