

適用於 Xamarin 的 AWS Mobile SDK 現在已包含在 中 適用於 .NET 的 AWS SDK。本指南參考 Mobile SDK for Xamarin 的封存版本。

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

# 傳送推播通知 (Xamarin iOS)
<a name="sns-send-push-notifications-ios"></a>

本文件說明如何使用 Amazon Simple Notification Service (SNS) 和適用於 .NET 和 Xamarin 的 AWS Mobile SDK，將推送通知傳送至 Xamarin iOS 應用程式。

## 專案設定
<a name="project-setup"></a>

### 先決條件
<a name="prerequisites"></a>

開始本教學課程之前，您必須完成[設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK](setup.md) 上的所有說明。

### 設定 SNS 的許可
<a name="set-permissions-for-sns"></a>

請遵循[設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK](setup.md) 中的步驟 2，將下列政策連接至應用程式的角色。這將給予您的應用程式存取 SNS 的適當許可：

1. 前往 [IAM 主控台](https://console.aws.amazon.com/iam/home)，然後選取您要設定的 IAM 角色。

1. 按一下**連接政策**，選取 AmazonSNSFullAccess 政策，然後按一下**連接政策**。

**警告**  
在生產環境中不建議使用 AmazonSNSFullAccess。我們在這裡使用它，可讓您快速啟動和執行。如需指定 IAM 角色許可的詳細資訊，請參閱 [IAM 角色許可概觀](https://docs.aws.amazon.com/IAM/latest/UserGuide/policies_permissions.html)。

### 在 Apple iOS 開發人員計劃中取得成員資格
<a name="obtain-membership-in-the-apple-ios-developer-program"></a>

您需要在實體裝置上執行應用程式，才能接收推播通知。若要在裝置上執行應用程式，您必須擁有 [Apple iOS 開發人員計劃成員資格](https://developer.apple.com/programs/ios/)。擁有成員資格後，您可以使用 Xcode 產生簽署身分。如需詳細資訊，請參閱 Apple [的應用程式分佈 Quick Start](https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/AppStoreDistributionTutorial/Introduction/Introduction.html#//apple_ref/doc/uid/TP40013839) 文件。

### 建立 iOS 憑證
<a name="create-an-ios-certificate"></a>

首先，您需要建立 iOS 憑證。然後，您需要建立為推送通知設定的佈建設定檔。若要這麼做：

1. 前往 [Apple 開發人員成員中心](https://developer.apple.com/membercenter/index.action)，按一下**憑證、識別符和設定檔**。

1. 按一下 **iOS 應用程式**下的**識別符**，按一下網頁右上角的加號按鈕以新增 iOS 應用程式 ID，然後輸入應用程式 ID 描述。

1. 向下捲動至**新增 ID 尾碼**區段，然後選取**明確應用程式 ID**，然後輸入您的套件識別碼。

1. 向下捲動至**應用程式服務**區段，然後選取**推送通知**。

1. 按一下 **Continue (繼續)**。

1. 請按 **Submit** (提交)。

1. 按一下**完成**。

1. 選取您剛建立的應用程式 ID，然後按一下**編輯**。

1. 向下捲動至**推送通知**區段。按一下開發 SSL **憑證下的建立**憑證。 ****

1. 依照指示建立憑證簽署請求 (CSR)、上傳請求，以及下載將用於與 Apple Notification Service (APNS) 通訊的 SSL 憑證。

1. 返回**憑證、識別符和設定檔**頁面。按一下**佈建設定檔**下的**全部**。

1. 按一下右上角的加號按鈕，新增佈建設定檔。

1. 選取 **iOS 應用程式開發**，然後按一下**繼續**。

1. 選取您的應用程式 ID，然後按一下**繼續**。

1. 選取您的開發人員憑證，然後按一下**繼續**。

1. 選取您的裝置，然後按一下**繼續**。

1. 輸入設定檔名稱，然後按一下**產生**。

1. 下載並按兩下佈建檔案以安裝佈建設定檔。

如需佈建針對推送通知設定之設定檔的詳細資訊，請參閱 Apple 的設定[推送通知](https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/AppDistributionGuide/ConfiguringPushNotifications/ConfiguringPushNotifications.html#//apple_ref/doc/uid/TP40012582-CH32-SW1)文件。

### 使用憑證在 SNS 主控台中建立平台 ARN
<a name="use-certificate-to-create-platform-arn-in-sns-console"></a>

1. 執行 KeyChain 存取應用程式，選取畫面左下角的**我的憑證**，然後在您產生連線至 APNS 的 SSL 憑證上按一下滑鼠右鍵，然後選取**匯出**。系統會提示您指定檔案的名稱和密碼來保護憑證。憑證將儲存在 P12 檔案中。

1. 前往 [SNS 主控台](https://console.aws.amazon.com/sns/v2/home)，然後按一下畫面左側**的應用程式**。

1. 按一下**建立平台應用程式**以建立新的 SNS 平台應用程式。

1. 輸入**應用程式名稱**。

1. 選取 **Apple Development** for **Push 通知平台**。

1. 按一下**選擇檔案**，然後選取您在匯出 SSL 憑證時建立的 P12 檔案。

1. 輸入匯出 SSL 憑證時指定的密碼，然後按一下**從檔案載入憑證**。

1. 按一下**建立平台應用程式**。

1. 選取您剛建立的平台應用程式，並複製應用程式 ARN。在接下來的步驟中，您將需要此項目。

### 將 SNS 的 NuGet 套件新增至您的專案
<a name="add-nuget-package-for-sns-to-your-project"></a>

請遵循[設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK](setup.md) 中的步驟 4，將 Amazon Simple Notification Service NuGet 套件新增至您的專案。

## 建立 SNS 用戶端
<a name="create-an-sns-client"></a>

```
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, region);
```

## 註冊您的遠端通知應用程式
<a name="register-your-application-for-remote-notifications"></a>

若要註冊應用程式，請在 UIApplication 物件上呼叫 RegisterForRemoteNotifications，如下所示。在 AppDelegate.cs,在出現以下提示時插入您的平台應用程式 ARN：

```
public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
// do something
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
  UIUserNotificationType.Alert |
  UIUserNotificationType.Badge |
  UIUserNotificationType.Sound,
  null
);
app.RegisterUserNotifications(pushSettings);
app.RegisterForRemoteNotifications();
// do something
  return true;
}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData token) {
  var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", "");
  if (!string.IsNullOrEmpty(deviceToken)) {
    //register with SNS to create an endpoint ARN
    var response = await SnsClient.CreatePlatformEndpointAsync(
    new CreatePlatformEndpointRequest {
      Token = deviceToken,
      PlatformApplicationArn = "YourPlatformArn" /* insert your platform application ARN here */
    });
  }
}
```

## 從 SNS 主控台傳送訊息到您的端點
<a name="send-a-message-from-the-sns-console-to-your-endpoint"></a>

1. 前往 [SNS 主控台 > 應用程式](https://console.aws.amazon.com/sns/v2/home)。

1. 選取您的平台應用程式，選取端點，然後按一下**發佈至端點**。

1. 在文字方塊中輸入文字訊息，然後按一下**發佈訊息**以發佈訊息。