可追蹤式磚通知
注意
生活磚是較新版本 Windows 不支援的 Windows 10 功能。 針對新的應用程式,建議您遵循應用程式圖示的目前指引。
可追蹤式磚通知可讓您判斷,當使用者按一下應用程式的動態磚時,動態磚會顯示何種磚通知。 例如,新聞應用程式可以使用此功能判斷,當使用者啟動此功能時,其動態磚會顯示哪一則新聞報導;它可確保報導醒目顯示,讓使用者能夠找到報導。
重要
需要年度更新版:若要搭配 C#、C++ 或 VB 型 UWP 應用程式使用可追蹤式磚通知,您必須以 SDK 14393 為目標,並且執行組建 14393 或更新版本。
重要 API:LaunchActivatedEventArgs.TileActivatedInfo 屬性、TileActivatedInfo 類別
運作方式
若要啟用可追蹤式磚通知,請使用磚通知承載上的引數屬性,類似快顯通知承載上的啟動屬性,以便將有關內容的資訊內嵌於磚通知中。
當您的應用程式透過動態磚啟動時,系統會從目前/最近顯示的磚通知傳回一份引數清單。
使用可追蹤式磚通知的時機
可追蹤式磚通知通常會在您於動態磚上使用通知佇列時使用 (這表示您循環顯示最多 5 個不同的通知)。 當動態磚上的內容可能與應用程式中的最新內容不同步時,這些磚通知也會很實用。 例如,新聞應用程式每隔 30 分鐘重新整理其動態磚,但當應用程式啟動時,它會載入最新的新聞 (其中可能未包含上次輪詢間隔時磚上顯示的內容)。 發生這種情況時,使用者可能會因為找不到他們在動態磚上看到的報導而感到挫折。 此時可追蹤式磚通知會很有用處,可讓您確保使用者能夠輕鬆找到他們在其磚上看見的內容。
如何使用可追蹤磚通知
最重要的一點是,在大多數情況下,當使用者按一下磚時,您「不應該」直接瀏覽至該磚上的特定通知。 動態磚的用途是作為應用程式的進入點。 有兩種情況使用者會按一下您的動態磚:(1) 使用者想要正常啟動您的應用程式,或 (2) 使用者想要查看動態磚上特定通知的詳細資訊。 由於使用者無法明確表達他們想要的行為,因此理想的體驗是正常啟動您的應用程式,同時確保使用者能夠輕鬆找到他們看到的通知。
例如,按一下 MSN 新聞應用程式的動態磚會正常啟動應用程式:它會顯示首頁,或是使用者先前閱讀的文章。 不過,在首頁上,應用程式會確保能夠輕鬆找到動態磚上的報導。 如此一來就能支援這兩種情況:一種是您只想要啟動/繼續使用應用程式,另一種情況是您想要檢視特定報導。
如何在磚通知承載中包含引數屬性
在通知承載中,引數屬性可讓您的應用程式提供稍後可用來識別通知的資料。 例如,您的引數可能包含報導的 ID,如此就能在啟動時擷取和顯示該報導。 屬性可接受字串,您可以視需要序列化該字串 (查詢字串、JSON 等),但我們通常建議使用查詢字串格式,因為它很輕量且經過良好的 XML 編碼。
您可以同時在 TileVisual 和 TileBinding 元素上設定屬性,屬性將會向下串聯。 如果您想要讓每個磚大小都有相同的引數,只要在 TileVisual 上設定引數即可。 如果您需要針對特定磚大小使用特定引數,可以在個別 TileBinding 元素上設定引數。
此範例會建立使用引數屬性的通知承載,以便稍後能夠識別通知。
// Uses the following NuGet packages
// - Microsoft.Toolkit.Uwp.Notifications
// - QueryString.NET
TileContent content = new TileContent()
{
Visual = new TileVisual()
{
// These arguments cascade down to Medium and Wide
Arguments = new QueryString()
{
{ "action", "storyClicked" },
{ "story", "201c9b1" }
}.ToString(),
// Medium tile
TileMedium = new TileBinding()
{
Content = new TileBindingContentAdaptive()
{
// Omitted
}
},
// Wide tile is same as Medium
TileWide = new TileBinding() { /* Omitted */ },
// Large tile is an aggregate of multiple stories
// and therefore needs different arguments
TileLarge = new TileBinding()
{
Arguments = new QueryString()
{
{ "action", "storiesClicked" },
{ "story", "43f939ag" },
{ "story", "201c9b1" },
{ "story", "d9481ca" }
}.ToString(),
Content = new TileBindingContentAdaptive() { /* Omitted */ }
}
}
};
如何在您的應用程式啟動時檢查引數屬性
大多數應用程式都有 App.xaml.cs 檔案,該檔案包含 OnLaunched 方法的覆寫。 正如其名稱所示,您的應用程式會在啟動時呼叫此方法。 它會採用單一引數,也就是 LaunchActivatedEventArgs 物件。
LaunchActivatedEventArgs 物件是啟用可追蹤式通知的屬性:TileActivatedInfo 屬性 提供 TileActivatedInfo 物件的存取權。 當使用者從磚啟動您的應用程式時 (而不是從應用程式清單、搜尋或任何其他進入點),您的應用程式會初始化此屬性。
TileActivatedInfo 物件包含稱為 RecentlyShownNotifications 的屬性,該屬性包含過去 15 分鐘內已在磚上顯示的通知清單。 清單中的第一個項目代表目前在磚上的通知,後續項目則代表使用者在目前項目之前看到的通知。 如果您的磚已清除,則此清單會是空的。
每個 ShownTileNotification 都有引數屬性。 引數屬性將會隨您磚通知承載中的引數字串初始化,如果承載未包含引數字串則為 null。
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
// If the API is present (doesn't exist on 10240 and 10586)
if (ApiInformation.IsPropertyPresent(typeof(LaunchActivatedEventArgs).FullName, nameof(LaunchActivatedEventArgs.TileActivatedInfo)))
{
// If clicked on from tile
if (args.TileActivatedInfo != null)
{
// If tile notification(s) were present
if (args.TileActivatedInfo.RecentlyShownNotifications.Count > 0)
{
// Get arguments from the notifications that were recently displayed
string[] allArgs = args.TileActivatedInfo.RecentlyShownNotifications
.Select(i => i.Arguments)
.ToArray();
// TODO: Highlight each story in the app
}
}
}
// TODO: Initialize app
}
從桌面應用程式存取 OnLaunched
使用傳統型橋接器的桌面應用程式 (如 WPF 等),也可以使用可追蹤式磚! 唯一的差異在於存取 OnLaunched 引數。 請注意,您必須先使用傳統型橋接器封裝您的應用程式。
重要
需要 2018 年 10 月更新:若要使用 AppInstance.GetActivatedEventArgs()
API,您必須以 SDK 17763 為目標,並執行組建 17763 或更新版本。
若要讓桌面應用程式存取啟動引數,請執行下列操作...
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// API only available on build 17763 or later
var args = AppInstance.GetActivatedEventArgs();
switch (args.Kind)
{
case ActivationKind.Launch:
var launchArgs = args as LaunchActivatedEventArgs;
// If clicked on from tile
if (launchArgs.TileActivatedInfo != null)
{
// If tile notification(s) were present
if (launchArgs.TileActivatedInfo.RecentlyShownNotifications.Count > 0)
{
// Get arguments from the notifications that were recently displayed
string[] allTileArgs = launchArgs.TileActivatedInfo.RecentlyShownNotifications
.Select(i => i.Arguments)
.ToArray();
// TODO: Highlight each story in the app
}
}
break;
原始 XML 範例
如果您使用原始 XML 而非通知程式庫,則 XML 如下。
<tile>
<visual arguments="action=storyClicked&story=201c9b1">
<binding template="TileMedium">
<text>Kitten learns how to drive a car...</text>
... (omitted)
</binding>
<binding template="TileWide">
... (same as Medium)
</binding>
<!--Large tile is an aggregate of multiple stories-->
<binding
template="TileLarge"
arguments="action=storiesClicked&story=43f939ag&story=201c9b1&story=d9481ca">
<text>Can your dog understand what you're saying?</text>
... (another story)
... (one more story)
</binding>
</visual>
</tile>