Tutorial:使用适用于 Apple 的 Azure 通知中心 SDK 向 iOS 应用发送推送通知Tutorial: Send push notifications to iOS apps using Azure Notification Hubs SDK for Apple

本教程演示如何使用适用于 Apple 的 Azure 通知中心 SDK 和 Azure 通知中心向 iOS 应用程序发送推送通知。This tutorial shows you how to use Azure Notification Hubs to send push notifications to an iOS application, using the Azure Notification Hubs SDK for Apple.

本教程涵盖以下步骤:This tutorial covers the following steps:

  • 创建示例 iOS 应用。Create a sample iOS app.
  • 将 iOS 应用连接到 Azure 通知中心。Connect your iOS app to Azure Notification Hubs.
  • 发送测试推送通知。Send test push notifications.
  • 验证应用是否可以接收通知。Verify that your app receives notifications.

可以从 GitHub 下载本教程的完整代码。You can download the complete code for this tutorial from GitHub.

先决条件Prerequisites

要完成本教程,需要具备以下先决条件:To complete this tutorial, you’ll need the following prerequisites:

  • 运行 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.

在继续操作之前,请务必完成上一篇教程,了解如何开始使用适用于 iOS 应用的 Azure 通知中心,并在通知中心设置和配置推送凭据。Before you proceed, be sure to go through the previous tutorial on getting started with Azure Notification Hubs for iOS apps, to set up and configure push credentials in your notification hub. 即使没有 iOS 开发经验,也应该能够按照这些步骤操作。Even if you have no prior experience with iOS development, you should be able to follow these steps.

备注

由于推送通知的配置要求,必须在物理 iOS 设备(iPhone 或 iPad)而不是在 iOS 仿真器上部署和测试推送通知。Because of configuration requirements for push notifications, you must deploy and test push notifications on a physical iOS device (iPhone or iPad), instead of the iOS emulator.

将 iOS 应用连接到通知中心Connect your iOS app to Notification Hubs

  1. 在 Xcode 中,创建新的 iOS 项目,然后选择“单视图应用程序”模板 。In Xcode, create a new iOS project and select the Single View Application template.

    选择模板

  2. 设置新项目的选项时,请务必使用在 Apple 开发人员门户中设置捆绑标识符时使用的同一 产品名称组织标识符When setting the options for your new project, make sure to use the same Product Name and Organization Identifier that you used when you set the bundle identifier in the Apple Developer portal.

  3. 在“项目导航器”的“目标”下选择项目名称,然后选择“签名和功能”选项卡 。确保为 Apple 开发人员帐户选择适当的“团队”。Under Project Navigator, select your project name under Targets, then select the Signing & Capabilities tab. Make sure you select the appropriate Team for your Apple Developer account. XCode 会根据捆绑标识符自动下拉以前创建的预配配置文件。XCode should automatically pull down the Provisioning Profile you created previously based on your bundle identifier.

    如果屏幕未显示在 Xcode 中创建的新预配配置文件,请尝试刷新签名标识的配置文件。If you don't see the new provisioning profile that you created in Xcode, try refreshing the profiles for your signing identity. 单击菜单栏上的“Xcode”,再依次单击“首选项”、“帐户”选项卡、“查看详细信息”按钮、签名标识,然后单击右下角的刷新按钮。Click Xcode on the menu bar, click Preferences, click the Account tab, click the View Details button, click your signing identity, and then click the refresh button in the bottom-right corner.

    查看详细信息

  4. 在“签名和功能”选项卡中,选择“+ 功能”。In the Signing & Capabilities tab, select + Capability. 双击“推送通知”以启用它。Double-click Push Notifications to enable it.

    功能

  5. 添加 Azure 通知中心 SDK 模块。Add the Azure Notification Hubs SDK modules.

    可以将 Azure 通知中心 SDK 集成到应用,方法是使用 Cocoapods 或者手动将二进制文件添加到项目。You can integrate the Azure Notification Hubs SDK into your app by using Cocoapods or by manually adding the binaries to your project.

    • 通过 Cocoapods 集成:将以下依赖项添加到 podfile,以便将 Azure 通知中心 SDK 包含到应用中:Integration via Cocoapods: Add the following dependencies to your podfile to include Azure Notification Hubs SDK in your app:

      pod 'AzureNotificationHubs-iOS'
      
      • 运行 pod 安装,安装新定义的 pod 并打开 .xcworkspace。Run pod install to install your newly defined pod and open your .xcworkspace.

        如果在运行 pod 安装时看到错误(例如找不到 AzureNotificationHubs-iOS 的规范),请运行 pod repo update 以从 Cocoapods 存储库获取最新的 pod,然后运行 pod 安装。If you see an error such as Unable to find a specification for AzureNotificationHubs-iOS while running pod install, run pod repo update to get the latest pods from the Cocoapods repository, and then run pod install.

    • 通过 Carthage 集成:将以下依赖项添加到 Cartfile,以便将 Azure 通知中心 SDK 包含到应用中:Integration via Carthage: Add the following dependencies to your Cartfile to include the Azure Notification Hubs SDK in your app:

      github "Azure/azure-notificationhubs-ios"
      
      • 接下来,更新生成依赖项:Next, update build dependencies:
      $ carthage update
      

      有关使用 Carthage 的详细信息,请参阅 Carthage GitHub 存储库For more information about using Carthage, see the Carthage GitHub repository.

    • 通过将二进制文件复制到项目中进行集成:Integration by copying the binaries into your project:

      可以通过将二进制文件复制到项目中来进行集成,如下所示:You can integrate by copying the binaries into your project, as follows:

      • 下载以 zip 文件形式提供的 Azure 通知中心 SDK 框架,然后将其解压缩。Download the Azure Notification Hubs SDK framework provided as a zip file, and unzip it.

      • 在 Xcode 中,右键单击项目,然后单击“将文件添加到”选项,将 WindowsAzureMessaging.framework 文件夹添加到 Xcode 项目。In Xcode, right-click your project and click the Add Files to option to add the WindowsAzureMessaging.framework folder to your Xcode project. 选择“选项”,确保选中“根据需要复制项目”,然后单击“添加”。Select Options and make sure Copy items if needed is selected, and then click Add.

        添加框架

  6. 添加或编辑名为 DevSettings.plist 的文件,其包含两个属性,即 Azure 通知中心的连接字符串 CONNECTION_STRING,和 Azure 通知中心名称 HUB_NAMEAdd or edit a file called DevSettings.plist which contains two properties, CONNECTION_STRING for the connection string to the Azure Notification Hub, and HUB_NAME for the Azure Notification Hub name.

  7. 在相应的 <string></string> 部分中添加连接到 Azure 通知中心的信息。Add the information for connecting to Azure Notification Hubs in the appropriate <string></string> section. 将字符串字面量占位符 --HUB-NAME----CONNECTION-STRING-- 分别替换为之前从门户中获取的中心名称和 DefaultListenSharedAccessSignature:Replace the string literal placeholders --HUB-NAME-- and --CONNECTION-STRING-- with the hub name and the DefaultListenSharedAccessSignature, respectively, as you previously obtained from the portal:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>HUB_NAME</key>
        <string>--HUB-NAME--</string>
        <key>CONNECTION_STRING</key>
        <string>--CONNECTION-STRING--</string>
    </dict>
    </plist>
    
  8. 在同一 AppDelegate.m 文件中,将 didFinishLaunchingWithOptions 后面的所有代码替换为以下代码:In the same AppDelegate.m file, replace all the code after didFinishLaunchingWithOptions with the following code:

    #import <WindowsAzureMessaging/WindowsAzureMessaging.h>
    #import <UserNotifications/UserNotifications.h>
    
    // Extend the AppDelegate to listen for messages using MSNotificationHubDelegate and User Notification Center
    @interface AppDelegate () <MSNotificationHubDelegate>
    
    @end
    
    @implementation AppDelegate
    
    @synthesize notificationPresentationCompletionHandler;
    @synthesize notificationResponseCompletionHandler;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"DevSettings" ofType:@"plist"];
        NSDictionary *configValues = [NSDictionary dictionaryWithContentsOfFile:path];
    
        NSString *connectionString = [configValues objectForKey:@"CONNECTION_STRING"];
        NSString *hubName = [configValues objectForKey:@"HUB_NAME"];
    
        if([connectionString length] != 0 && [hubName length] != 0) {
            [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
            [MSNotificationHub setDelegate:self];
            [MSNotificationHub initWithConnectionString:connectionString withHubName:hubName];
    
            return YES;
        }
    
        NSLog(@"Please setup CONNECTION_STRING and HUB_NAME in DevSettings.plist and restart application");
    
        exit(-1);
    }
    
    - (void)notificationHub:(MSNotificationHub *)notificationHub didReceivePushNotification:(MSNotificationHubMessage *)message {
        // Send message using NSNotificationCenter with the message
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:message forKey:@"message"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:nil userInfo:userInfo];
    }
    
    @end
    

    此代码使用在 DevSettings.plist 中指定的连接信息连接到通知中心。This code connects to the notification hub using the connection information you specified in DevSettings.plist. 然后,它向通知中心提供设备令牌,使中心能够发送通知。It then gives the device token to the notification hub, so that the hub can send notifications.

创建 NotificationDetailViewController 头文件Create NotificationDetailViewController header file

  1. 与之前的说明类似,添加另一个名为 SetupViewController.h 的头文件。Similar to the previous instructions, add another header file named SetupViewController.h. 将新的头文件的内容替换为以下代码:Replace the contents of the new header file with the following code:

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SetupViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END
    
  2. 添加实现文件 SetupViewController.m。Add the implementation file SetupViewController.m. 将该文件的内容替换为以下代码,此代码可实现 UIViewController 方法:Replace the contents of the file with the following code, which implements the UIViewController methods:

    #import "SetupViewController.h"
    
     static NSString *const kNHMessageReceived = @"MessageReceived";
    
     @interface SetupViewController ()
    
     @end
    
     @implementation SetupViewController
    
     - (void)viewDidLoad {
         [super viewDidLoad];
    
         // Listen for messages using NSNotificationCenter
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceivePushNotification:) name:kNHMessageReceived object:nil];
     }
    
     - (void)dealloc {
         // Clean up subscription to NSNotificationCenter
         [[NSNotificationCenter defaultCenter] removeObserver:self name:kNHMessageReceived object:nil];
     }
    
     - (void)didReceivePushNotification:(NSNotification *)notification {
         MSNotificationHubMessage *message = [notification.userInfo objectForKey:@"message"];
    
         // Create UI Alert controller with message title and body
         UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message.title
                              message:message.body
                       preferredStyle:UIAlertControllerStyleAlert];
         [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];
         [self presentViewController:alertController animated:YES completion:nil];
    
         // Dismiss after 2 seconds
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [alertController dismissViewControllerAnimated:YES completion: nil];
         });
    
     }
    
     @end
    
  3. 若要验证是否没有故障,请在设备上生成并运行应用。To verify there are no failures, build and run the app on your device.

发送测试推送通知Send test push notifications

可以在 Azure 门户中使用“测试性发送”选项,在应用中测试通知的发送。You can test receiving notifications in your app with the Test Send option in the Azure portal. 它会向设备发送测试性的推送通知。It sends a test push notification to your device.

测试发送

通常,推送通知是在后端服务(例如,移动应用,或者使用兼容库的 ASP.NET)中发送的。Push notifications are normally sent in a back-end service like Mobile Apps or ASP.NET using a compatible library. 如果后端没有可用库,也可使用 REST API 直接发送通知消息。If a library isn't available for your back end, you can also use the REST API directly to send notification messages.

下面是可能需要查看的有关发送通知的其他教程列表:Here is a list of some other tutorials you might want to review for sending notifications:

验证应用可以接收推送通知Verify that your app receives push notifications

要在 iOS 上测试推送通知,必须将应用部署到物理 iOS 设备。To test push notifications on iOS, you must deploy the app to a physical iOS device. 不能使用 iOS 模拟器发送 Apple 推送通知。You cannot send Apple push notifications by using the iOS simulator.

  1. 运行应用并验证注册是否成功,并按“确定”。Run the app and verify that registration succeeds, and then press OK.

    注册

  2. 如上一部分所述,接下来可以从 Azure 门户发送测试推送通知。Next, send a test push notification from the Azure portal, as described in the previous section.

  3. 该推送通知会从给定通知中心发送到所有已注册接收通知的设备。The push notification is sent to all devices that are registered to receive the notifications from the given notification hub.

    发送测试

后续步骤Next steps

在这个简单的示例中,已将推送通知广播到所有已注册的 iOS 设备。In this simple example, you broadcast push notifications to all your registered iOS devices. 要了解如何向特定 iOS 设备发送推送通知,请转到以下教程:To learn how to send push notifications to specific iOS devices, advance to the following tutorial:

教程:向特定设备推送通知Tutorial: Push notifications to specific devices

有关详细信息,请参阅以下文章:For more information, see the following articles: