Xamarin 中的交互式通知用户界面Interactive notification user interfaces in Xamarin.iOS

在 iOS 10 中引入的通知内容扩展可以为通知创建自定义用户界面。Notification content extensions, introduced in iOS 10, make it possible to create custom user interfaces for notifications. 从 iOS 12 开始,通知用户界面可包含交互式元素,如按钮和滑块。Starting with iOS 12, notification user interfaces can contain interactive elements such as buttons and sliders.

示例应用: RedGreenNotificationsSample App: RedGreenNotifications

RedGreenNotifications示例应用包含一个包含交互式用户界面的通知内容扩展。The RedGreenNotifications sample app contains a notification content extension with an interactive user interface.

本指南中的代码片段来自于此示例。Code snippets in this guide come from this sample.

通知内容扩展 info.plist 文件Notification content extension Info.plist file

在示例应用程序中, RedGreenNotificationsContentExtension项目中的info.plist文件包含以下配置:In the sample app, the Info.plist file in the RedGreenNotificationsContentExtension project contains the following configuration:

<!-- ... -->
<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>UNNotificationExtensionCategory</key>
        <array>
            <string>red-category</string>
            <string>green-category</string>
        </array>
        <key>UNNotificationExtensionUserInteractionEnabled</key>
        <true/>
        <key>UNNotificationExtensionDefaultContentHidden</key>
        <true/>
        <key>UNNotificationExtensionInitialContentSizeRatio</key>
        <real>0.6</real>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.usernotifications.content-extension</string>
    <key></key>
    <true/>
</dict>
<!-- ... -->

请注意以下功能:Note the following features:

  • UNNotificationExtensionCategory 数组指定内容扩展处理的通知类别的类型。The UNNotificationExtensionCategory array specifies the type of notification categories the content extension handles.
  • 为了支持交互式内容,通知内容扩展会将 UNNotificationExtensionUserInteractionEnabled 密钥设置为 trueIn order to support interactive content, the notification content extension sets the UNNotificationExtensionUserInteractionEnabled key to true.
  • UNNotificationExtensionInitialContentSizeRatio 键指定内容扩展接口的初始高度/宽度比。The UNNotificationExtensionInitialContentSizeRatio key specifies the initial height/width ratio for the content extension's interface.

交互式接口Interactive interface

为通知内容扩展定义接口的 MainInterface 是一个包含单个视图控制器的标准情节提要 MainInterface.storyboard, which defines the interface for a notification content extension, is a standard storyboard containing a single view controller. 在示例应用中,视图控制器的类型为 NotificationViewController,它包含一个图像视图、三个按钮和一个滑块。In the sample app, the view controller is of type NotificationViewController, and it contains an image view, three buttons, and a slider. 情节提要将这些控件与在NotificationViewController.cs中定义的处理程序相关联:The storyboard associates these controls with handlers defined in NotificationViewController.cs:

  • "启动应用程序" 按钮处理程序对 ExtensionContext调用 PerformNotificationDefaultAction 操作方法,这将启动应用程序:The Launch App button handler calls the PerformNotificationDefaultAction action method on ExtensionContext, which launches the app:

    partial void HandleLaunchAppButtonTap(UIButton sender)
    {
        ExtensionContext.PerformNotificationDefaultAction();
    }
    

    在应用程序中,用户通知中心的 Delegate (在示例应用程序中,AppDelegate)可响应 DidReceiveNotificationResponse 方法中的交互:In the app, the user notification center's Delegate (in the sample app, the AppDelegate) can respond to the interaction in the DidReceiveNotificationResponse method:

    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
    {
        if (response.IsDefaultAction)
        {
            Console.WriteLine("ACTION: Default");
            // ...
    
  • 消除通知按钮处理程序对 ExtensionContext调用 DismissNotificationContentExtension,这会关闭通知:The Dismiss Notification button handler calls DismissNotificationContentExtension on ExtensionContext, which closes the notification:

    partial void HandleDismissNotificationButtonTap(UIButton sender)
    {
        ExtensionContext.DismissNotificationContentExtension();
    }
    
  • 删除通知按钮处理程序将关闭通知并将其从通知中心删除:The Remove Notification button handler dismisses the notification and removes it from Notification Center:

    partial void HandleRemoveNotificationButtonTap(UIButton sender)
    {
        ExtensionContext.DismissNotificationContentExtension();
        UNUserNotificationCenter.Current.RemoveDeliveredNotifications(new string[] { notification.Request.Identifier });
    }
    
  • 处理滑块上值更改的方法更新在通知的接口中显示的图像的 alpha:The method that handles value changes on the slider updates the alpha of the image displayed in the notification's interface:

    partial void HandleSliderValueChanged(UISlider sender)
    {
        Xamagon.Alpha = sender.Value;
    }