教程:使用 Azure 通知中心向 iOS 应用发送推送通知Tutorial: Send push notifications to iOS apps using Azure Notification Hubs

本教程将介绍如何设置 Azure 通知中心,并展示如何配置凭据,以通过 Apple Push Notification 服务 (APNS) 将通知推送到 iOS 设备。This tutorial shows you how to set up Azure Notification Hubs and configure credentials to push notifications to an iOS device via the Apple Push Notification service (APNS).

为了后续学习 Objective C 和 Swift iOS 教程,必须运行此教程,它涵盖以下步骤:Running this tutorial is a prerequisite to the subsequent Objective C and Swift iOS tutorials, and covers the following steps:

  • 生成证书签名请求文件。Generate the certificate signing request file.
  • 为应用请求推送通知。Request your app for push notifications.
  • 为应用创建预配配置文件。Create a provisioning profile for the app.
  • 创建通知中心。Create a notification hub.
  • 使用 APNS 信息配置通知中心。Configure the notification hub with APNS information.

先决条件Prerequisites

要完成本教程,必须有一个有效的 Azure 帐户。To complete this tutorial, you must have an active Azure account. 如果没有帐户,只需花费几分钟就能创建一个免费试用帐户。If you don't have an account, you can create a free trial account in just a couple of minutes. 有关详细信息,请参阅 Azure 试用版For details, see Azure Trial.

还需要以下项:You'll also need the following:

  • 一个有效的 Apple 开发人员帐户。An active Apple Developer account.
  • 一部运行 Xcode 的 Mac,以及一个安装到密钥链的有效开发人员证书。A Mac running Xcode, along with a valid developer certificate installed into your Keychain.
  • 一部运行 iOS 10 或更高版本的 iPhone 或 iPad。An iPhone or iPad running iOS version 10 or later.
  • 一台在 Apple 门户 中注册并与证书关联的物理设备。Your physical device registered in the Apple Portal and associated with your certificate.

如果你不熟悉此服务,请务必阅读 Azure 通知中心概述Be sure to read the Azure Notification Hubs overview if you’re not familiar with the service.

备注

通知中心将配置为仅使用沙盒身份验证模式。The notification hub will be configured to use the Sandbox authentication mode only. 不得将此身份验证模式用于生产工作负载。You should not use this authentication mode for production workloads.

生成证书签名请求文件Generate the certificate-signing request file

Apple Push Notification 服务 (APNS) 使用证书对推送通知进行身份验证。The Apple Push Notification Service (APNS) uses certificates to authenticate your push notifications. 请遵照这些说明来创建用于发送和接收通知的所需推送证书。Follow these instructions to create the necessary push certificate to send and receive notifications. 有关这些概念的详细信息,请参阅 Apple Push Notification 服务 官方文档。For more information on these concepts, see the official Apple Push Notification Service documentation.

生成证书签名请求 (CSR) 文件,Apple 将使用该文件生成签名的推送证书:Generate the Certificate Signing Request (CSR) file, which Apple uses to generate a signed push certificate:

  1. 在 Mac 上,运行 Keychain Access 工具。On your Mac, run the Keychain Access tool. 可从启动板上的“Utilities”或“Other”文件夹中打开该工具。 ****   ****  It can be opened from the Utilities folder or the Other folder on the Launchpad.

  2. 选择“密钥链访问”,展开“证书助理”,然后选择“从证书颁发机构请求证书”。 ****  ****  ****Select Keychain Access, expand Certificate Assistant, and then select Request a Certificate from a Certificate Authority.

    密钥链访问

    备注

    默认情况下,Keychain Access 选择列表中的第一项。By default, Keychain Access selects the first item in the list. 如果你位于“Certificates”(证书)类别中,并且“Apple Worldwide Developer Relations Certification Authority”(Apple 全球开发者关系证书颁发机构)不是列表中的第一项,这可能会是个问题。This can be a problem if you're in the Certificates category and Apple Worldwide Developer Relations Certification Authority is not the first item in the list. 在生成 CSR(证书签名请求)之前,请确保已有非密钥项,或者已选择“Apple Worldwide Developer Relations Certification Authority”(Apple 全球开发者关系证书颁发机构)密钥。Make sure you have a non-key item, or the Apple Worldwide Developer Relations Certification Authority key is selected, before generating the CSR (Certificate Signing Request).

  3. 选择“用户电子邮件地址”,输入“公用名”值,确保指定“保存到磁盘”,然后选择“继续”。 ****  ****   ****  ****Select your User Email Address, enter your Common Name value, make sure that you specify Saved to disk, and then select Continue. 将“CA 电子邮件地址”留空,因为它不是必填字段 ****  。Leave CA Email Address blank as it isn't required.

    必需的证书信息

  4. 在“另存为”中为 CSR 文件输入一个名称,在“位置”中选择位置,然后选择“保存”。 ****  ****  ****Enter a name for the CSR file in Save As, select the location in Where, and then select Save.

    选择文件名

    此操作会将 CSR 文件保存到选定位置。This action saves the CSR file in the selected location. 默认位置为“桌面”。 ****The default location is Desktop. 请记住为此文件选择的位置。Remember the location chosen for the file.

接下来,向 Apple 注册应用,启用推送通知并上传导出的 CSR,以便创建推送证书。Next, register your app with Apple, enable push notifications, and upload the exported CSR to create a push certificate.

为推送通知注册应用程序Register your app for push notifications

若要将推送通知发送到 iOS 应用,请向 Apple 注册应用程序,还要注册推送通知。To send push notifications to an iOS app, register your application with Apple, and also register for push notifications.

  1. 如果尚未注册应用,请浏览到苹果开发者中心的  iOS 预配门户 。If you haven't already registered your app, browse to the iOS Provisioning Portal at the Apple Developer Center. 使用你的 Apple ID 登录到门户,然后选择“标识符” **** 。Sign in to the portal with your Apple ID, and select Identifiers. 接下来,选择“+注册新应用”。 ****  Then select + to register a new app.

    “应用 ID”页面

  2. 在“注册新的标识符”屏幕上,选择“应用 ID”单选按钮。 ****   ****  On the Register a New Identifier screen, select the App IDs radio button. 接下来,选择“继续”。 ****Then select Continue.

    “注册新 ID”页面

  3. 更新新应用的下面 3 个值,然后选择“继续”: ****Update the following three values for your new app, and then select Continue:

    • 说明:键入应用的描述性名称。Description: Type a descriptive name for your app.

    • 捆绑 ID:按 应用分发指南中所述,输入格式为 Organization Identifier.Product Name 的捆绑 ID。 ****  Bundle ID: Enter a Bundle ID of the form Organization Identifier.Product Name as mentioned in the App Distribution Guide. “组织标识符”和“产品名称”值必须与创建 Xcode 项目时使用的组织标识符与产品名称匹配。 ****   ****  The Organization Identifier and Product Name values must match the organization identifier and product name you use when you create your Xcode project. 在下面的屏幕截图中,“NotificationHubs”值用作组织标识符,“GetStarted”值用作产品名称。 ****   ****  In the following screenshot, the NotificationHubs value is used as an organization identifier and the GetStarted value is used as the product name. 确保“捆绑标识符”值与 Xcode 项目中的值匹配,这样 Xcode 才能使用正确的发布配置文件。 ****  Make sure the Bundle Identifier value matches the value in your Xcode project, so that Xcode uses the correct publishing profile.

      注册应用 ID

    • 推送通知:勾选“功能”部分中的“推送通知”选项。 ****   ****  Push Notifications: Check the Push Notifications option in the Capabilities section.

      注册新应用 ID

      此操作会生成应用 ID 并请求你确认信息。This action generates your App ID and requests that you confirm the information. 选择“继续”,然后选择“注册”以确认新的应用 ID。 ****  ****  Select Continue, then select Register to confirm the new App ID.

      确认新的应用 ID

      选择“注册”后,会在“证书、标识符和配置文件”页面上看到新的应用 ID 作为行项。 ****  ****  After you select Register, you see the new App ID as a line item on the Certificates, Identifiers & Profiles page.

  4. 在“证书、标识符和配置文件”页面的“标识符”下,找到你刚刚创建的应用 ID 行项,并选择其所在的行,显示“编辑应用 ID 配置”屏幕。 ****   ****  ****  In the Certificates, Identifiers & Profiles page, under Identifiers, locate the App ID line item that you just created, and select its row to display the Edit your App ID Configuration screen.

为通知中心创建证书Create a certificate for Notification Hubs

备注

在发布 iOS 13 后,只能使用基于令牌的身份验证接收无声通知。With the release of iOS 13, you can only receive silent notifications using token based authentication. 如果将基于证书的身份验证用于 APNS 凭据,则必须改用基于令牌的身份验证。If you are using certificate-based authentication for your APNS credentials, you must switch to using token-based authentication.

需要提供证书才能使通知中心与 APNS 一起使用。 ****A certificate is required to enable the notification hub to work with APNS. 可通过以下两种方式之一实现此目的:This can be done in one of two ways:

  • 创建可直接上传到通知中心的 .p12 文件。 ****Create a .p12 file that can be uploaded directly to Notification Hubs.

  • 创建可用于 基于令牌的身份验证 (新方法)的 .p8文件。 ****  Create a .p8 file that can be used for token-based authentication (the newer approach).

与使用证书相比,第二个选项有很多好处,如 APNS 的基于令牌的 (HTTP/2) 的身份验证中所述。The second option has a number of benefits compared to using certificates, as documented in Token-based (HTTP/2) authentication for APNS. 但是,为这两种方法都提供了步骤。However, steps are provided for both approaches.

选项 1:创建可直接上传到通知中心的 .p12 推送证书Option 1: Create a .p12 push certificate that can be uploaded directly to Notification Hubs

  1. 向下滚动到选中的“推送通知”选项,然后选择“配置”以创建证书。 ****   ****  Scroll down to the checked Push Notifications option, and then select Configure to create the certificate.

    应用 ID

  2. 此时将显示“Apple Push Notification 服务 SSL 证书”窗口。 ****  The Apple Push Notification service SSL Certificates window appears. 在“开发 SSL 证书”部分中,选择“创建证书”按钮。 ****   ****  Select the Create Certificate button in the Development SSL Certificate section.

    创建证书

    此时将显示“创建新证书”屏幕。 ****  The Create a new Certificate screen is displayed.

    备注

    本教程使用开发证书。This tutorial uses a development certificate. 注册生产证书时使用相同的过程。The same process is used when registering a production certificate. 请确保在发送通知时使用相同的证书类型。Make sure that you use the same certificate type when sending notifications.

  3. 勾选“选择文件”,浏览到在第一个任务中保存 CSR 文件的位置,然后双击证书名以加载该证书。 ****Select Choose File, browse to the location at which you saved the CSR file from the first task, and then double-click the certificate name to load it. 接下来,选择“继续”。 ****Then select Continue.

  4. 当门户创建证书后,选择“下载”按钮。 ****  After the portal creates the certificate, select the Download button. 保存证书,并记住保存证书的位置。Save the certificate, and remember the location to which it's saved.

    下载证书

    下载证书并将其保存在“Downloads”文件夹中。 ****  The certificate is downloaded and saved in your Downloads folder.

    查找证书文件

    默认情况下,下载的开发证书名为 aps_development.cerBy default, the downloaded development certificate is named aps_development.cer.

  5. 双击下载的推送证书 aps_development.cer。 ****Double-click the downloaded push certificate aps_development.cer. 此操作将在密钥链中安装新证书,如下图所示:This action installs the new certificate in the Keychain, as shown in the following image:

    密钥链访问

    证书中的名称可能会不同,但会以 Apple Development iOS Push Services 作为前缀。Although the name in your certificate might be different, the name will be prefixed with Apple Development iOS Push Services.

  6. 在“密钥链访问”中,右键单击在“证书”类别中创建的新推送证书。 ****  In Keychain Access, right-click the new push certificate that you created in the Certificates category. 选择“导出”,为文件命名,选择“.p12”格式,然后选择“保存”。 ****  ****   ****Select Export, name the file, select the .p12 format, and then select Save.

    导出证书

    可以选择使用密码保护证书,但这是可选的。You can choose to protect the certificate with a password, but this is optional. 如果要跳过密码创建,请单击“确定”。 ****  Click OK if you want to bypass password creation. 记下导出的 .p12 证书的文件名和位置。Make a note of the file name and location of the exported .p12 certificate. 它们用于启用 APNS 身份验证。They are used to enable authentication with APNS.

    备注

    你的 .p12 文件名和位置可能不同于本教程中所示的名称和位置。Your .p12 file name and location might be different than what is pictured in this tutorial.

选项 2:创建可用于基于令牌的身份验证的 p8 证书Option 2: Create a .p8 certificate that can be used for token-based authentication

  1. 请记下以下详细信息:Make note of the following details:

    • 应用 ID 前缀(这是团队 ID)  ****App ID Prefix (this is a Team ID)
    • 捆绑包 IDBundle ID
  2. 返回“证书,标识符和配置文件”,再单击“密钥”。 ****  ****Back in Certificates, Identifiers & Profiles, click Keys. 如果已为 APNS 配置了密钥,则可以重复使用在创建后立即下载的 .p8 证书。If you already have a key configured for APNS, you can re-use the .p8 certificate that you downloaded right after it was created. 如果已配置,则可忽略步骤 3-5。If so, you can ignore steps 3 through 5.

  3. 单击 + 按钮(或“创建密钥”按钮)以创建新密钥。 ****   ****  Click the + button (or the Create a key button) to create a new key.

  4. 提供合适的密钥名称值,选中“Apple Push Notification 服务(APNS)”选项,然后单击“继续”,在下一个屏幕上单击“注册”。 ****   ****   ****  ****  Provide a suitable Key Name value, check the Apple Push Notifications service (APNS) option, and then click Continue, followed by Register on the next screen.

  5. 单击“下载”,然后将 .p8 文件(前缀为 AuthKey_)移动到安全的本地目录,再单击“完成”。 ****   ****   ****Click Download and then move the .p8 file (prefixed with AuthKey_) to a secure local directory, then click Done.

    重要

    请确保将 .p8 文件保存在安全的地方(并保存备份)。Be sure to keep your .p8 file in a secure place (and save a backup). 密钥在下载后无法重新下载;服务器副本已被删除。After downloading your key, it cannot be re-downloaded; the server copy is removed.

  6. 在“密钥”上,单击刚创建的密钥(如果已选择使用现有密钥,则改为单击现有密钥)。 ****On Keys, click on the key that you just created (or an existing key if you have chosen to use that instead).

  7. 记下“密钥 ID”值。 ****  Make note of the Key ID value.

  8. 在所选的适当应用程序(如 Visual Studio Code)中打开 .p8 证书,然后记下密钥值。Open your .p8 certificate in a suitable application of your choice, such as Visual Studio Code, then make note of the key value. 这是“-----BEGIN PRIVATE KEY-----”和“-----END PRIVATE KEY-----”之间的值。 ****   ****  This is the value between -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- .

    -----BEGIN PRIVATE KEY-----
    <key_value>
    -----END PRIVATE KEY-----
    

    该令牌值稍后将用于配置通知中心。This is the token value that will be used later to configure Notification Hubs.

完成这些步骤后,你应具有稍后要在 使用 APNS 信息配置通知中心中用到的下列信息:At the end of these steps you should have the following information for use later in Configure your notification hub with APNS information:

  • 团队 ID(请参阅步骤 1) Team ID (see step 1)
  • 捆绑包 ID(请参阅步骤 1) Bundle ID (see step 1)
  • 密钥 ID(请参阅步骤 7) Key ID (see step 7)
  • 令牌值(即 .p8 密钥值,请参阅步骤 8) Token value (the .p8 key value, see step 8)

创建预配配置文件Create a provisioning profile

  1. 返回到 iOS 预配门户,选择“证书、标识符和配置文件”,从左侧菜单中选择“配置文件”,然后选择 + 来创建新的配置文件。 ****  ****   ****  Return to the iOS Provisioning Portal, select Certificates, Identifiers & Profiles, select Profiles from the left menu, and then select + to create a new profile. 此时将显示“注册新的预配配置文件”屏幕。 ****  The Register a New Provisioning Profile screen appears.

  2. 在“开发”下选择“iOS 应用开发”作为预配配置文件类型,然后选择“继续”。 ****   ****   ****Select iOS App Development under Development as the provisioning profile type, and then select Continue.

    预配配置文件列表

  3. 接下来,从“应用 ID”下拉列表中选择所创建的应用 ID,然后选择“继续”。 ****   ****Next, select the app ID you created from the App ID drop-down list, then select Continue.

    选择应用 ID

  4. 在“选择证书”窗口中,选择用于代码签名的开发证书,然后选择“继续”。 ****   ****In the Select certificates window, select the development certificate that you use for code signing, and select Continue. 此证书不是所创建的推送证书。This certificate isn't the push certificate you created. 如果不存在,则必须创建一个。If one does not exist, you must create it. 如果证书存在,请跳到下一步。If a certificate does exist, skip to the next step. 若要创建开发证书(如果开发证书不存在),请执行以下操作:To create a development certificate if one does not exist:

    1. 如果看到“没有可用的证书”,请选择“创建证书”。 ****  ****If you see No Certificates are available, select Create Certificate.
    2. 在“软件”部分中,选择“Apple 开发”。 ****   ****In the Software section, select Apple Development. 接下来,选择“继续”。 ****Then select Continue.
    3. 在“创建新证书”屏幕中,勾选“选择文件”。 ** **  ****In the Create a New Certificate screen, select Choose File.
    4. 浏览到之前创建的“证书签名请求”证书,选择该证书,然后选择“打开”。 ****   ****Browse to the Certificate Signing Request certificate you created earlier, select it, and then select Open.
    5. 选择“继续”。 ****Select Continue.
    6. 下载开发证书,并记住其保存位置。Download the development certificate, and remember the location in which it's saved.
  5. 返回到“证书、标识符和配置文件”页面,从左侧菜单中选择“配置文件”,然后选择 + 来创建新的配置文件”。 ****   ****   ****  Return to the Certificates, Identifiers & Profiles page, select Profiles from the left menu, and then select + to create a new profile. 此时将显示“注册新的预配配置文件”屏幕。 ****  The Register a New Provisioning Profile screen appears.

  6. 在“选择证书”窗口中,选择你刚刚创建的开发证书。 ****  In the Select certificates window, select the development certificate that you just created. 接下来,选择“继续”。 ****Then select Continue.

  7. 接下来,选择用于测试的设备,然后选择“继续”。 ****Next, select the devices to use for testing, and select Continue.

  8. 最后,在“预配配置文件名称”中为配置文件选择一个名称,然后选择“生成”。 ****  ****Finally, choose a name for the profile in Provisioning Profile Name, then select Generate.

    选择预配配置文件名称

  9. 创建新的预配配置文件后,选择“下载”。 ****When the new provisioning profile is created, select Download. 记住证书保存的位置。Remember the location in which it's saved.

  10. 浏览到预配配置文件所在的位置,然后双击该配置文件以将其安装在 Xcode 开发计算机上。Browse to the location of the provisioning profile, and then double-click it to install it on your Xcode development machine.

创建通知中心Create a notification hub

在本部分中,你将创建一个通知中心,并使用 .p12 推送证书或基于令牌的身份验证来配置 APNS 身份验证。In this section, you create a notification hub and configure authentication with APNS by using either the .p12 push certificate or token-based authentication. 如果想要使用已创建的通知中心,可以跳到步骤 5。If you want to use a notification hub that you've already created, you can skip to step 5.

  1. 登录  Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单上选择“所有服务”,然后在“移动”部分中选择“通知中心” ****   ****   ****  。Select All services on the left menu, and then select Notification Hubs in the Mobile section. 选择服务名称旁边的星形图标,将服务添加到左侧菜单上的“收藏夹”部分 ****  。Select the star icon next to the service name to add the service to the FAVORITES section on the left menu. 在将“通知中心”添加到“收藏夹”之后,将其选中。 ****   ****After you add Notification Hubs to FAVORITES, select it.

    Azure 门户

  3. 在“通知中心”页上,选择工具栏上的“添加” ****   ****  。On the Notification Hubs page, select Add on the toolbar.

    添加工具栏按钮

  4. 在“通知中心”页面上,执行以下步骤: ****  On the Notification Hubs page, do the following steps:

    1. 在“通知中心”中输入名称 **** 。Enter a name in Notification Hub.
    2. 在“创建新的命名空间”中输入名称 **** 。Enter a name in Create a new namespace. 命名空间包含一个或多个通知中心。A namespace contains one or more notification hubs.
    3. 从“位置”下拉列表中选择一个值。 ****  Select a value from the Location drop-down list. 此值指定要在其中创建通知中心的位置。This value specifies the location in which you want to create the notification hub.
    4. 选择“资源组”中的现有资源组,或创建新的资源组。 ****Select an existing resource group in Resource Group, or create a new resource group.
    5. 选择“创建”。 ****Select Create.

    设置属性

  5. 选择“通知”(钟形图标),然后选择“转到资源” ****   **** 。Select Notifications (the bell icon), and then select Go to resource. 还可以刷新“通知中心”页上的列表,然后选择相应中心 ****  。You can also refresh the list on the Notification Hubs page and select your hub.

    门户通知

  6. 从列表中选择“访问策略” ****  。Select Access Policies from the list. 记下向你提供的两个连接字符串。Note that the two connection strings are available to you. 稍后在处理推送通知时需要它们。You'll need them later to handle push notifications.

    重要

    请不要在应用程序中使用 DefaultFullSharedAccessSignature 策略。Do not use the DefaultFullSharedAccessSignature policy in your application. 这只能在后端使用。This is meant to be used in your back end only.

    连接字符串

使用 APNS 信息配置通知中心Configure the notification hub with APNS information

在“通知服务”下,选择“Apple (APNS)”,然后根据之前在 为通知中心创建证书部分中选择的方法,执行相应的步骤。 ****  ****  Under Notification Services, select Apple (APNS), then follow the appropriate steps based on the approach you chose previously in the Creating a Certificate for Notification Hubs section.

备注

仅当希望将推送通知发送给从应用商店购买应用的用户时,才对“应用程序模式”使用“生产”。 Use Production for Application Mode only if you want to send push notifications to users who purchased your app from the store.

选项 1:使用 .p12 推送证书Option 1: Use a .p12 push certificate

  1. 选择“证书”。 ****Select Certificate.

  2. 选择文件图标。Select the file icon.

  3. 选择前面导出的 .p12 文件,然后选择“打开”。 ****Select the .p12 file that you exported earlier, and then select Open.

  4. 如果需要,请指定正确的密码。If required, specify the correct password.

  5. 选择“沙盒”模式。  ****Select Sandbox mode.

    配置

  6. 选择“保存”。 ****Select Save.

选项 2:使用基于令牌的身份验证Option 2: Use token-based authentication

  1. 选择“令牌”。 ****Select Token.

  2. 输入前面获取的以下值:Enter the following values that you acquired earlier:

    • 密钥 IDKey ID
    • 捆绑包 IDBundle ID
    • 团队 IDTeam ID
    • 令牌Token
  3. 选择“沙盒” ****Choose Sandbox

  4. 选择“保存”。 ****Select Save.

你现已使用 APNS 配置了通知中心。You've now configured your notification hub with APNS. 此外,还具有注册应用和发送推送通知所需的连接字符串。You also have the connection strings needed to register your app and send push notifications.

后续步骤Next steps

在本教程中,你在 Azure 中创建并配置了一个通知中心,并已它其配置为允许通过 Apple Push Notification 服务 (APNS) 将通知发送到你的应用程序。In this tutorial, you created and configured a notification hub in Azure and configured it to allow notifications to be sent to your application through Apple Push Notification Service (APNS). 接下来,我们将创建一个示例 iOS 应用程序并集成 Azure 通知中心 SDK,使其能够接收通过 Azure 门户发送的推送通知。Next, we’ll create a sample iOS application and integrate the Azure Notifications Hubs SDK so that it can receive push notifications sent through the Azure portal. 根据所选的语言,前往以下教程:Advance to the following tutorial based on your language of choice: