共用方式為


Xamarin.iOS 中的增強使用者通知

IOS 10 的新功能是使用者通知架構,可讓您傳遞和處理本機和遠端通知。 使用此架構,應用程式或應用程式延伸模組可以藉由指定一組條件,例如位置或一天中的時間,來排程本機通知的傳遞。

關於使用者通知

如上所述,新的使用者通知架構允許傳遞和處理本機和遠端通知。 使用此架構,應用程式或應用程式延伸模組可以藉由指定一組條件,例如位置或一天中的時間,來排程本機通知的傳遞。

此外,當應用程式或擴充功能傳遞至使用者的iOS裝置時,應用程式或擴充功能可以同時接收本機和遠端通知。

新的使用者通知 UI 架構可讓應用程式或應用程式延伸模組在向用戶呈現時自定義本機和遠端通知的外觀。

此架構提供下列方式,讓應用程式可以將通知傳遞給使用者:

  • 視覺警示 - 通知從畫面頂端向下滾落為橫幅的位置。
  • 音效和震動 - 可以與通知產生關聯。
  • 應用程式圖示錯誤 - 應用程式圖示顯示可用的新內容徽章,例如未讀取的電子郵件訊息數目。

此外,視使用者目前的內容而定,將會以不同的方式呈現通知:

  • 如果裝置解除鎖定,通知會從畫面頂端向下滾下來作為橫幅。
  • 如果裝置已鎖定,通知將會顯示在用戶的鎖定畫面上。
  • 如果使用者錯過通知,他們可以開啟通知中心並檢視任何可用的等候通知。

Xamarin.iOS 應用程式有兩種類型的使用者通知,可以傳送:

  • 本機通知 - 這些是由在使用者裝置本機安裝的應用程式所傳送。
  • 遠端通知 - 會從遠端伺服器傳送,並呈現給使用者,或觸發應用程式內容的背景更新。

關於本機通知

iOS 應用程式可傳送的本機通知具有下列功能和屬性:

  • 這些應用程式是由用戶裝置上的本機應用程式傳送。
  • 它們可以設定為使用時間或位置型觸發程式。
  • 應用程式會使用使用者的裝置排程通知,並在符合觸發程式條件時顯示。
  • 當使用者與通知互動時,應用程式會收到回呼。

本機通知的一些範例包括:

  • 行事曆警示
  • 提醒警示
  • 位置感知觸發程式

如需詳細資訊,請參閱Apple的 本機和遠端通知程序設計指南 檔。

關於遠端通知

iOS 應用程式可以傳送的遠端通知具有下列功能和屬性:

  • 應用程式具有與其通訊的伺服器端元件。
  • Apple 推播通知服務 (APNs) 可用來從開發人員的雲端式伺服器,將遠端通知傳遞至使用者的裝置。
  • 當應用程式收到遠端通知時,它會顯示給使用者。
  • 當使用者與通知互動時,應用程式會收到回呼。

遠端通知的一些範例包括:

  • 新聞警示
  • 體育 更新
  • 立即訊息

iOS 應用程式可以使用兩種類型的遠端通知:

  • 使用者面向 - 這些會顯示給裝置上的使用者。
  • 無訊息 更新 - 這些會提供一個機制,以在背景更新 iOS 應用程式的內容。 收到無訊息更新時,應用程式可以連絡遠端伺服器以提取最新的內容。

如需詳細資訊,請參閱Apple的 本機和遠端通知程序設計指南 檔。

關於現有的通知 API

在 iOS 10 之前,iOS 應用程式會用來 UIApplication 向系統註冊通知,並排程應如何觸發該通知(依時間或位置)。

使用現有的通知 API 時,開發人員可能會遇到數個問題:

  • 本機或遠端通知需要不同的回呼,這可能會導致程式代碼重複。
  • 應用程式在系統排程通知之後,對通知的控制有限。
  • 所有 Apple 現有平臺都有不同的支援層級。

關於新的使用者通知架構

使用 iOS 10,Apple 引進了新的使用者通知架構,以取代上述現有的 UIApplication 方法。

使用者通知架構提供下列專案:

  • 熟悉的 API,其中包含與先前方法的功能同位,可讓您輕鬆地從現有的架構移植程式碼。
  • 包含擴充的內容選項集,可讓更豐富的通知傳送給使用者。
  • 本機和遠端通知都可以由相同的程式代碼和回呼處理。
  • 簡化當使用者與通知互動時傳送至應用程式之回呼的程式。
  • 增強對擱置和已傳遞通知的管理,包括移除或更新通知的能力。
  • 新增執行應用程式內通知呈現的功能。
  • 新增從應用程式延伸模組內排程和處理通知的功能。
  • 新增通知本身的新擴充點。

新的使用者通知架構可在 Apple 支援的多個平臺上提供統一的通知 API,包括:

  • iOS - 管理及排程通知的完整支援。
  • tvOS - 新增本機和遠端通知徽章應用程式圖示的功能。
  • watchOS - 將使用者配對 iOS 裝置的通知轉寄至其 Apple Watch 的功能,並讓手錶應用程式能夠直接在手錶本身上執行本機通知。
  • macOS - 管理及排程通知的完整支援。

如需詳細資訊,請參閱 Apple 的 UserNotifications Framework 參考UserNotificationsUI 檔。

準備通知傳遞

在 iOS 應用程式可以傳送通知給使用者之前,應用程式必須向系統註冊,而且因為通知對使用者造成中斷,應用程式必須先明確要求許可權,才能傳送。

使用者可核准應用程式的通知要求有三個不同層級:

  • 橫幅顯示。
  • 音效警示。
  • 錯誤應用程式圖示。

此外,必須針對本機和遠端通知要求和設定這些核准層級。

只要應用程式啟動時,應要求通知許可權,方法是將下列程式代碼新增至 FinishedLaunching 的方法 AppDelegate ,並設定所需的通知類型 (UNAuthorizationOptions):

注意

UNUserNotificationCenter 僅適用於 iOS 10+。 因此,在傳送要求之前,最好先檢查macOS版本。

using UserNotifications;
...

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    // Version check
    if (UIDevice.CurrentDevice.CheckSystemVersion (10, 0)) {
        // Request notification permissions from the user
        UNUserNotificationCenter.Current.RequestAuthorization (UNAuthorizationOptions.Alert, (approved, err) => {
            // Handle approval
        });
    }

    return true;
}

由於此 API 是統一的,而且也適用於 Mac 10.14+,如果您的目標是 macOS,您也必須儘快檢查通知許可權:

using UserNotifications;
...

public override void DidFinishLaunching (NSNotification notification)
{
    // Check we're at least v10.14
    if (NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion (new NSOperatingSystemVersion (10, 14, 0))) {
        // Request notification permissions from the user
        UNUserNotificationCenter.Current.RequestAuthorization (UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (approved, err) => {
            // Handle approval
        });
    }
}

> [!NOTE]
> With MacOS apps, for the permission dialog to appear, you must sign your macOS app, even if building locally in DEBUG mode. Therefore, **Project->Options->Mac Signing->Sign the application bundle** must be checked.

Additionally, a user can always change the notification privileges for an app at any time using the **Settings** app on the device. The app should check for the user's requested notification privileges before presenting a notification using the following code:

```csharp
// Get current notification settings
UNUserNotificationCenter.Current.GetNotificationSettings ((settings) => {
    var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled);
});    

設定遠端通知環境

iOS 10 的新手,開發人員必須通知操作系統在開發或生產環境中執行哪些環境推播通知。 若無法提供這項資訊,可能會導致應用程式在提交至 iTune App Store 時遭到拒絕,通知類似如下:

遺漏推播通知權利 - 您的應用程式包含Apple推播通知服務的 API,但 aps-environment 應用程式簽章中缺少權利。

若要提供必要的權利,請執行下列動作:

  1. 按兩下 Entitlements.plist Solution Pad 中的檔案,將其開啟以供編輯。

  2. 切換至 [來源] 檢視:

    [來源] 檢視

  3. +按下按鈕以新增金鑰。

  4. 針對 [屬性] 輸入 aps-environment ,將 [類型] 保留String ,然後針對 [值] 輸入 developmentproduction

    aps-environment 屬性

  5. 儲存對檔案所做的變更。

註冊遠端通知

如果應用程式將傳送和接收遠端通知,它仍然需要使用現有的 API 進行令牌註冊UIApplication 此註冊需要裝置具有即時網路連線存取APN,這會產生將傳送至應用程式的必要令牌。 然後,應用程式必須將此令牌轉送給開發人員的伺服器端應用程式,以註冊遠端通知:

令牌註冊概觀

使用下列程式代碼來初始化必要的註冊:

UIApplication.SharedApplication.RegisterForRemoteNotifications ();

傳送至開發人員伺服器端應用程式的令牌必須包含在傳送遠端通知時從伺服器傳送到 APN 的通知承載的一部分:

包含在通知承載中的令牌

令牌可作為將通知與用來開啟或回應通知的應用程式系結在一起的密鑰。

如需詳細資訊,請參閱Apple的 本機和遠端通知程序設計指南 檔。

通知傳遞

應用程式已完整註冊,以及使用者要求和授與的必要許可權,應用程式現在已準備好傳送和接收通知。

提供通知內容

iOS 10 的新功能,所有通知都包含一律會與通知內容的本文一起顯示的標題子標題。 此外,您也可以將媒體附件新增至通知內容。

若要建立本機通知的內容,請使用下列程序代碼:

var content = new UNMutableNotificationContent();
content.Title = "Notification Title";
content.Subtitle = "Notification Subtitle";
content.Body = "This is the message body of the notification.";
content.Badge = 1;

針對遠端通知,此程式類似:

{
    "aps":{
        "alert":{
            "title":"Notification Title",
            "subtitle":"Notification Subtitle",
            "body":"This is the message body of the notification."
        },
        "badge":1
    }
}

排程傳送通知時

建立通知的內容後,應用程式必須藉由設定 觸發程式來排程通知向用戶顯示的時間。 iOS 10 提供四種不同的觸發程式類型:

  • 推播通知 - 專用於遠端通知,並在 APN 將通知套件傳送至裝置上執行的應用程式時觸發。
  • 時間間隔 - 允許從時間間隔開始排程本機通知,從現在開始,並結束一些未來點。 例如,var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger (5, false);
  • 行事曆日期 - 允許排程特定日期和時間的本機通知。
  • 位置型 - 允許在 iOS 裝置進入或離開特定地理位置時排程本機通知,或位於任何 藍牙 指標的鄰近位置。

當本機通知就緒時,應用程式必須呼叫 Add 物件的方法 UNUserNotificationCenter ,以將它的顯示排程給使用者。 針對遠端通知,伺服器端應用程式會將通知承載傳送至 APN,然後將封包傳送至使用者的裝置。

將所有部分整合在一起,範例本機通知看起來可能像這樣:

using UserNotifications;
...

var content = new UNMutableNotificationContent ();
content.Title = "Notification Title";
content.Subtitle = "Notification Subtitle";
content.Body = "This is the message body of the notification.";
content.Badge = 1;

var trigger =  UNTimeIntervalNotificationTrigger.CreateTrigger (5, false);

var requestID = "sampleRequest";
var request = UNNotificationRequest.FromIdentifier (requestID, content, trigger);

UNUserNotificationCenter.Current.AddNotificationRequest (request, (err) => {
    if (err != null) {
        // Do something with error...
    }
});

處理前景應用程式通知

iOS 10 的新功能,當應用程式在前景和觸發通知時,可以以不同的方式處理通知。 藉由提供 UNUserNotificationCenterDelegate 並實作 WillPresentNotification 方法,應用程式可以接管顯示通知的責任。 例如:

using System;
using UserNotifications;

namespace MonkeyNotification
{
    public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        #region Constructors
        public UserNotificationCenterDelegate ()
        {
        }
        #endregion

        #region Override Methods
        public override void WillPresentNotification (UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            // Do something with the notification
            Console.WriteLine ("Active Notification: {0}", notification);

            // Tell system to display the notification anyway or use
            // `None` to say we have handled the display locally.
            completionHandler (UNNotificationPresentationOptions.Alert);
        }
        #endregion
    }
}

此程式代碼只會將 的內容 UNNotification 寫出至應用程式輸出,並要求系統顯示通知的標準警示。

如果應用程式想要在前景顯示通知本身,而不要使用系統預設值,請傳遞 None 至完成處理程式。 範例:

completionHandler (UNNotificationPresentationOptions.None);

在此程式代碼就緒后,請開啟 AppDelegate.cs 檔案進行編輯,並變更 FinishedLaunching 方法,如下所示:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    // Request notification permissions from the user
    UNUserNotificationCenter.Current.RequestAuthorization (UNAuthorizationOptions.Alert, (approved, err) => {
        // Handle approval
    });

    // Watch for notifications while the app is active
    UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate ();

    return true;
}

此程式代碼會將上述的自定義 UNUserNotificationCenterDelegate 附加至目前的 UNUserNotificationCenter ,讓應用程式可以在作用中且在前景時處理通知。

通知管理

iOS 10 的新功能通知管理提供擱置和傳遞通知的存取權,並新增移除、更新或升級這些通知的功能。

通知管理的一個重要部分是 使用系統建立和排程時指派給通知的要求標識碼 。 針對遠端通知,這會透過 HTTP 要求標頭中的新 apps-collapse-id 欄位指派。

要求標識碼是用來選取應用程式想要執行通知管理的通知。

拿掉通知

若要從系統移除擱置的通知,請使用下列程序代碼:

var requests = new string [] { "sampleRequest" };
UNUserNotificationCenter.Current.RemovePendingNotificationRequests (requests);

若要移除已傳遞的通知,請使用下列程序代碼:

var requests = new string [] { "sampleRequest" };
UNUserNotificationCenter.Current.RemoveDeliveredNotifications (requests);

更新現有的通知

若要更新現有的通知,只要使用修改所需的參數建立新的通知(例如新的觸發時間),然後將它新增至系統,其要求標識符與需要修改的通知相同。 範例:

using UserNotifications;
...

// Rebuild notification
var content = new UNMutableNotificationContent ();
content.Title = "Notification Title";
content.Subtitle = "Notification Subtitle";
content.Body = "This is the message body of the notification.";
content.Badge = 1;

// New trigger time
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger (10, false);

// ID of Notification to be updated
var requestID = "sampleRequest";
var request = UNNotificationRequest.FromIdentifier (requestID, content, trigger);

// Add to system to modify existing Notification
UNUserNotificationCenter.Current.AddNotificationRequest (request, (err) => {
    if (err != null) {
        // Do something with error...
    }
});

對於已傳遞的通知,如果使用者已經讀取通知,現有的通知將會更新並升階到 [首頁] 和 [鎖定] 畫面上列表頂端。

使用通知動作

在 iOS 10 中,傳遞給使用者的通知不是靜態的,並提供數種方式讓使用者可以與其互動(從內建到自定義動作)。

iOS 應用程式可以回應的動作有三種類型:

  • 默認動作 - 當使用者點選 [通知] 以開啟應用程式並顯示指定通知的詳細數據時,就會發生這種情況。
  • 自定義動作 - 這些已新增在 iOS 8 中,並提供快速的方式讓使用者直接從通知執行自定義工作,而不需要啟動應用程式。 這些按鈕可以呈現為具有可自定義標題的按鈕清單,或可在背景中執行的文字輸入欄位(其中應用程式會提供少量時間來完成要求)或前景(應用程式在前景啟動以履行要求)。 iOS 和 watchOS 都提供自定義動作。
  • 關閉動作 - 當使用者關閉指定的通知時,此動作會傳送至應用程式。

建立自定義動作

若要建立及註冊系統的自訂動作,請使用下列程式代碼:

// Create action
var actionID = "reply";
var title = "Reply";
var action = UNNotificationAction.FromIdentifier (actionID, title, UNNotificationActionOptions.None);

// Create category
var categoryID = "message";
var actions = new UNNotificationAction [] { action };
var intentIDs = new string [] { };
var categoryOptions = new UNNotificationCategoryOptions [] { };
var category = UNNotificationCategory.FromIdentifier (categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

// Register category
var categories = new UNNotificationCategory [] { category };
UNUserNotificationCenter.Current.SetNotificationCategories (new NSSet<UNNotificationCategory>(categories));

建立新的 UNNotificationAction時,會獲派唯一標識符和按鈕上顯示的唯一標識碼和標題。 根據預設,動作會建立為背景動作,不過可以提供選項來調整動作的行為(例如將它設定為前景動作)。

每個建立的動作都必須與類別相關聯。 建立新的 UNNotificationCategory時,它會獲指派唯一標識符、可執行的動作清單、意圖標識符清單,以提供有關 Category 中動作意圖的詳細資訊,以及控制 Category 行為的一些選項。

最後,所有 Categories 都會使用 SetNotificationCategories 方法向系統註冊。

呈現自定義動作

建立並註冊系統一組自定義動作和類別之後,即可從本機或遠端通知呈現它們。

針對 [遠端通知],在 [遠端通知承載] 中設定 category ,符合上面建立的其中一個類別。 例如:

{
    aps:{
        alert:"Hello world!",
        category:"message"
    }
}

針對 [本機通知],設定 CategoryIdentifier 對象的 屬性 UNMutableNotificationContent 。 例如:

var content = new UNMutableNotificationContent ();
content.Title = "Notification Title";
content.Subtitle = "Notification Subtitle";
content.Body = "This is the message body of the notification.";
content.Badge = 1;
content.CategoryIdentifier = "message";
...

同樣地,此標識碼必須符合上面建立的其中一個類別。

處理關閉動作

如上所述,當使用者關閉通知時,可以傳送關閉動作給應用程式。 由於這不是標準動作,因此必須在建立類別時設定選項。 例如:

var categoryID = "message";
var actions = new UNNotificationAction [] { action };
var intentIDs = new string [] { };
var categoryOptions = new UNNotificationCategoryOptions [] { };
var category = UNNotificationCategory.FromIdentifier (categoryID, actions, intentIDs, UNNotificationCategoryOptions.CustomDismissAction);

處理動作回應

當使用者與上面建立的自定義動作和類別互動時,應用程式必須滿足要求的工作。 這是藉由提供 UNUserNotificationCenterDelegate 和實作 UserNotificationCenter 方法來完成。 例如:

using System;
using UserNotifications;

namespace MonkeyNotification
{
    public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        ...

        #region Override Methods
        public override void DidReceiveNotificationResponse (UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            // Take action based on Action ID
            switch (response.ActionIdentifier) {
            case "reply":
                // Do something
                break;
            default:
                // Take action based on identifier
                if (response.IsDefaultAction) {
                    // Handle default action...
                } else if (response.IsDismissAction) {
                    // Handle dismiss action
                }
                break;
            }

            // Inform caller it has been handled
            completionHandler();
        }
        #endregion
    }
}

傳入 UNNotificationResponse 類別的屬性 ActionIdentifier 可以是預設動作或關閉動作。 使用 response.Notification.Request.Identifier 來測試任何自定義動作。

屬性 UserText 會保留任何使用者文字輸入的值。 屬性 Notification 會保存原始通知,其中包含具有觸發程式和通知內容的要求。 應用程式可以根據觸發程式類型來決定它是否為本機或遠端通知。

注意

iOS 12 可讓自訂通知 UI 在運行時間修改其動作按鈕。 如需詳細資訊,請參閱 動態通知動作按鈕 檔。

使用服務延伸模組

使用遠端通知時, 服務延伸模組 提供在通知承載內啟用端對端加密的方式。 服務延伸模組是非使用者介面延伸模組(可在 iOS 10 中提供),可在背景中執行,主要目的是在向使用者呈現通知之前,先增強或取代通知的可見內容。

服務延伸模組概觀

服務延伸模組的目的是要快速執行,而且只會提供系統執行的時間短。 如果服務延伸模組在分配的時間量中無法完成其工作,將會呼叫後援方法。 如果後援失敗,則會向用戶顯示原始通知內容。

服務延伸模組的一些可能用途包括:

  • 提供遠端通知內容的端對端加密。
  • 將附件新增至遠端通知以擴充它們。

實作服務擴充功能

若要在 Xamarin.iOS 應用程式中實作服務擴充功能,請執行下列動作:

  1. 在 Visual Studio for Mac 中開啟應用程式的方案。

  2. 以滑鼠右鍵按兩下 Solution Pad 中的 [方案名稱],然後選取 [新增>新增專案]。

  3. 選取 [iOS>擴充功能通知服務延伸模組>],然後按下一步] 按鈕:

    選取 [通知服務延伸模組]

  4. 輸入延伸模組的 [名稱],然後按兩下一步[ 下一步] 按鈕:

    輸入延伸模組的 [名稱]

  5. 視需要調整 [項目名稱] 和/或 [方案名稱],然後按兩下 [建立] 按鈕:

    調整項目名稱和/或方案名稱

重要

服務延伸模組的套件組合標識碼應該符合主要應用程式的套件組合標識碼,並 .appnameserviceextension 附加至結尾。 例如,如果主要應用程式具有的套件組合標識符 com.xamarin.monkeynotify,服務延伸模組應該具有的套件組合標識符 com.xamarin.monkeynotify.monkeynotifyserviceextension。 當擴充功能新增至解決方案時,應該會自動設定此設定。

通知服務延伸模組中有一個主要類別需要修改,以提供所需的功能。 例如:

using System;
using Foundation;
using UIKit;
using UserNotifications;

namespace MonkeyChatServiceExtension
{
    [Register ("NotificationService")]
    public class NotificationService : UNNotificationServiceExtension
    {
        #region Computed Properties
        public Action<UNNotificationContent> ContentHandler { get; set; }
        public UNMutableNotificationContent BestAttemptContent { get; set; }
        #endregion

        #region Constructors
        protected NotificationService (IntPtr handle) : base (handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }
        #endregion

        #region Override Methods
        public override void DidReceiveNotificationRequest (UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
        {
            ContentHandler = contentHandler;
            BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy ();

            // Modify the notification content here...
            BestAttemptContent.Title = $"{BestAttemptContent.Title}[modified]";

            ContentHandler (BestAttemptContent);
        }

        public override void TimeWillExpire ()
        {
            // Called just before the extension will be terminated by the system.
            // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

            ContentHandler (BestAttemptContent);
        }
        #endregion
    }
}

第一個方法 DidReceiveNotificationRequest會透過 request 對象傳遞通知標識碼和通知內容。 必須呼叫傳入 contentHandler 的 ,才能向用戶顯示通知。

第二個方法 TimeWillExpire會在時間即將用完之前呼叫,服務擴充功能才能處理要求。 如果服務擴充功能在分配的時間量中呼叫 無法呼叫 contentHandler ,則會向用戶顯示原始內容。

觸發服務擴充功能

使用應用程式建立並傳遞服務延伸模組後,即可修改傳送至裝置的遠端通知承載來觸發它。 例如:

{
    aps : {
        alert : "New Message Available",
        mutable-content: 1
    },
    encrypted-content : "#theencryptedcontent"
}

新的 mutable-content 金鑰會指定必須啟動服務延伸模組,才能更新遠端通知內容。 金鑰 encrypted-content 會保存服務延伸模組可以在向用戶呈現之前解密的加密數據。

請檢視下列服務延伸模組範例:

using UserNotification;

namespace myApp {
    public class NotificationService : UNNotificationServiceExtension {

        public override void DidReceiveNotificationRequest(UNNotificationRequest request, contentHandler) {
            // Decrypt payload
            var decryptedBody = Decrypt(Request.Content.UserInfo["encrypted-content"]);

            // Modify Notification body
            var newContent = new UNMutableNotificationContent();
            newContent.Body = decryptedBody;

            // Present to user
            contentHandler(newContent);
        }

        public override void TimeWillExpire() {
            // Handle out-of-time fallback event
            ...
        }

    }
}

此程式代碼會從 encrypted-content 金鑰解密加密的內容、建立新的 UNMutableNotificationContent、將屬性設定 Body 為解密的內容,並使用 contentHandler 將通知呈現給使用者。

摘要

本文涵蓋 iOS 10 增強使用者通知的所有方式。 它呈現了新的使用者通知架構,以及如何在 Xamarin.iOS 應用程式或應用程式延伸模組中使用。