将推送通知添加到 Xamarin.iOS 应用

概述

在本教程中,将推送通知添加到 Xamarin.iOS 快速入门 项目,以便在每次插入记录时向设备发送推送通知。

如果不使用下载的快速入门服务器项目,则需要推送通知扩展包。 有关详细信息 ,请参阅使用适用于 Azure 移动应用的 .NET 后端服务器 SDK

先决条件

在 Apple 开发人员门户中注册应用以推送通知

  • 为应用注册应用 ID。 创建显式应用 ID(而不是通配符应用 ID),对于 捆绑 ID,请使用 Xcode 快速入门项目中的确切捆绑 ID。 选择 “推送通知 ”选项也至关重要。
  • 接下来, 若要准备配置推送通知,请创建“开发”或“分发”SSL 证书。

配置移动应用以发送推送通知

  1. 在 Mac 上,启动 密钥链访问。 在左侧导航栏的 “类别”下,打开 “我的证书”。 找到在上一部分下载的 SSL 证书,然后披露其内容。 仅选择证书(不选择私钥)。 然后 导出它
  2. Azure 门户中,选择“ 浏览所有>应用服务”。 然后选择移动应用后端。
  3. “设置”下,选择 “应用服务推送”。 然后选择通知中心名称。
  4. 转到 Apple 推送通知服务>上传证书。 上传 .p12 文件,选择正确的 模式 (具体取决于先前的客户端 SSL 证书是生产还是沙盒)。 保存任何更改。

服务现已配置为在 iOS 上使用推送通知。

更新服务器项目以发送推送通知

在本部分中,将更新现有移动应用后端项目中的代码,以每次添加新项时发送推送通知。 此过程由 Azure 通知中心的 模板 功能提供支持,该功能支持跨平台推送。 各种客户端都使用模板注册推送通知,单个通用推送可以访问所有客户端平台。

选择以下与后端项目类型匹配的过程之一,即 .NET 后端或 Node.js 后端

.NET 后端项目

  1. 在 Visual Studio 中,右键单击服务器项目。 然后选择“ 管理 NuGet 包”。 搜索 Microsoft.Azure.NotificationHubs,然后选择“ 安装”。 此过程安装通知中心库,用于从后端发送通知。

  2. 在服务器项目中,打开 控制器>TodoItemController.cs。 然后添加以下使用语句:

    using System.Collections.Generic;
    using Microsoft.Azure.NotificationHubs;
    using Microsoft.Azure.Mobile.Server.Config;
    
  3. PostTodoItem 方法中,在调用 InsertAsync 后添加以下代码:

    // Get the settings for the server project.
    HttpConfiguration config = this.Configuration;
    MobileAppSettingsDictionary settings =
        this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
    
    // Get the Notification Hubs credentials for the mobile app.
    string notificationHubName = settings.NotificationHubName;
    string notificationHubConnection = settings
        .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
    
    // Create a new Notification Hub client.
    NotificationHubClient hub = NotificationHubClient
    .CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
    
    // Send the message so that all template registrations that contain "messageParam"
    // receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations.
    Dictionary<string,string> templateParams = new Dictionary<string,string>();
    templateParams["messageParam"] = item.Text + " was added to the list.";
    
    try
    {
        // Send the push notification and log the results.
        var result = await hub.SendTemplateNotificationAsync(templateParams);
    
        // Write the success result to the logs.
        config.Services.GetTraceWriter().Info(result.State.ToString());
    }
    catch (System.Exception ex)
    {
        // Write the failure result to the logs.
        config.Services.GetTraceWriter()
            .Error(ex.Message, null, "Push.SendAsync Error");
    }
    

    此过程发送包含项的模板通知。插入新项时的文本。

  4. 重新发布服务器项目。

Node.js 后端项目

  1. 搭建你的后端项目。

  2. 将 todoitem.js 中的现有代码替换为以下代码:

    var azureMobileApps = require('azure-mobile-apps'),
    promises = require('azure-mobile-apps/src/utilities/promises'),
    logger = require('azure-mobile-apps/src/logger');
    
    var table = azureMobileApps.table();
    
    table.insert(function (context) {
    // For more information about the Notification Hubs JavaScript SDK,
    // see https://aka.ms/nodejshubs.
    logger.info('Running TodoItem.insert');
    
    // Define the template payload.
    var payload = '{"messageParam": "' + context.item.text + '" }';  
    
    // Execute the insert. The insert returns the results as a promise.
    // Do the push as a post-execute action within the promise flow.
    return context.execute()
        .then(function (results) {
            // Only do the push if configured.
            if (context.push) {
                // Send a template notification.
                context.push.send(null, payload, function (error) {
                    if (error) {
                        logger.error('Error while sending push notification: ', error);
                    } else {
                        logger.info('Push notification sent successfully!');
                    }
                });
            }
            // Don't forget to return the results from the context.execute().
            return results;
        })
        .catch(function (error) {
            logger.error('Error while running context.execute: ', error);
        });
    });
    
    module.exports = table;  
    

    此过程在插入新项时发送包含 item.text 的模板通知。

  3. 在本地计算机上编辑该文件时,请重新发布服务器项目。

配置 Xamarin.iOS 项目

在 Xamarin Studio 中配置 iOS 项目

  1. 在 Xamarin.Studio 中,打开 Info.plist,并使用先前使用您的新应用 ID 创建的 包 ID 更新捆绑标识符。

  2. 向下滚动到 后台模式。 选择“ 启用后台模式 ”框和 “远程通知 ”框。

  3. 双击解决方案面板中的项目以打开 项目选项

  4. “生成”下,选择 “iOS 包签名”,然后选择刚刚为此项目设置的相应标识和配置描述文件。

    这可确保项目使用新的签名资料进行代码签名。 有关官方 Xamarin 设备预配文档,请参阅 Xamarin 设备预配

在 Visual Studio 中配置 iOS 项目

  1. 在 Visual Studio 中,右键单击项目,然后单击“ 属性”。

  2. 在属性页中,单击 “iOS 应用程序 ”选项卡,并使用之前创建的 ID 更新 标识符

  3. “iOS 捆绑签名 ”选项卡中,选择刚刚为此项目设置的相应标识和预配配置文件。

    这可确保项目使用新的签名资料进行代码签名。 有关官方 Xamarin 设备预配文档,请参阅 Xamarin 设备预配

  4. 双击 Info.plist 将其打开,然后在后台模式下启用 RemoteNotification

向应用添加推送通知

  1. QSTodoService 中,添加以下属性,以便 AppDelegate 可以获取移动客户端:

    public MobileServiceClient GetClient {
        get
        {
            return client;
        }
        private set
        {
            client = value;
        }
    }
    
  2. 将以下 using 语句添加到 AppDelegate.cs 文件的顶部。

    using Microsoft.WindowsAzure.MobileServices;
    using Newtonsoft.Json.Linq;
    
  3. AppDelegate 中,重写 FinishedLaunching 事件:

     public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
     {
         // registers for push for iOS8
         var settings = UIUserNotificationSettings.GetSettingsForTypes(
             UIUserNotificationType.Alert
             | UIUserNotificationType.Badge
             | UIUserNotificationType.Sound,
             new NSSet());
    
         UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
         UIApplication.SharedApplication.RegisterForRemoteNotifications();
    
         return true;
     }
    
  4. 在同一文件中,覆盖事件 RegisteredForRemoteNotifications 。 在此代码中,你将注册一个简单的模板通知,该通知将由服务器在所有受支持的平台上发送。

    有关包含通知中心的模板的详细信息,请参阅 模板

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        MobileServiceClient client = QSTodoService.DefaultService.GetClient;
    
        const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";
    
        JObject templates = new JObject();
        templates["genericMessage"] = new JObject
        {
            {"body", templateBodyAPNS}
        };
    
        // Register for push with your mobile app
        var push = client.GetPush();
        push.RegisterAsync(deviceToken, templates);
    }
    
  5. 然后,重写 DidReceivedRemoteNotification 事件:

     public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
     {
         NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
    
         string alert = string.Empty;
         if (aps.ContainsKey(new NSString("alert")))
             alert = (aps [new NSString("alert")] as NSString).ToString();
    
         //show alert
         if (!string.IsNullOrEmpty(alert))
         {
             UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
             avAlert.Show();
         }
     }
    

应用现已更新以支持推送通知。

在应用中测试推送通知

  1. “运行 ”按钮生成项目并在支持 iOS 的设备中启动应用,然后单击“ 确定 ”接受推送通知。

    注释

    必须显式接受来自应用的推送通知。 此请求仅在应用首次运行时发生。

  2. 在应用中,键入任务,然后单击加号(+)图标。

  3. 验证是否收到通知,然后单击“ 确定 ”以消除通知。

  4. 重复步骤 2 并立即关闭应用,然后验证是否显示通知。

你已成功完成本教程。