アプリ通知は、アプリのウィンドウの外側に表示される UI ポップアップで、タイムリーな情報やアクションをユーザーに提供します。 通知は、純粋に情報に関する情報である場合や、クリックされたときにアプリを起動したり、アプリをフォアグラウンドに移動せずにバックグラウンド アクションをトリガーしたりできます。
この記事では、.NETコンソール アプリからアプリ通知を作成して送信し、ユーザーが操作するときにアクティブ化を処理する手順について説明します。 この記事では、Windows アプリ SDKMicrosoft.Windows.AppNotifications API を使用します。
アプリ通知の概要と他のフレームワークのガイダンスについては、「 アプリ通知の概要」を参照してください。
この記事では、ローカル通知について説明します。 クラウド サービスからの通知の配信については、「 プッシュ通知」を参照してください。
Important
管理者権限アプリの通知は現在サポートされていません。
前提条件
- .NET 6 以降を対象とする.NETコンソール アプリ
-
Windows アプリ SDK NuGet パッケージ (
Microsoft.WindowsAppSDK)
プロジェクトを設定する
プロジェクト ファイル (.csproj) で、TargetFrameworkにWindowsターゲット フレームワークが含まれていることを確認します。
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>
Windows アプリ SDK NuGet パッケージを追加します。
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250310001" />
パッケージ化されていないアプリの場合は、次を追加します。
<WindowsPackageType>None</WindowsPackageType>
アプリ通知に登録する
Main メソッドで、Register を呼び出す前に NotificationInvoked ハンドラーを登録します。 通知がクリックされたときにアクティブ化コールバックを受信するには、コンソール アプリを実行したままにする必要があります。
Program.cs
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
// Register the notification handler before calling Register
AppNotificationManager.Default.NotificationInvoked += (sender, args) =>
{
// Handle notification activation.
// args.Argument contains the arguments from the notification
// or button that was clicked, as key=value pairs separated
// by '&', for example "action=acknowledge".
Console.WriteLine($"Notification activated! Arguments: {args.Argument}");
};
AppNotificationManager.Default.Register();
注
パッケージ化されていないアプリの場合、Register() は COM サーバーの登録を自動的に設定し、通知がクリックされたときにWindowsがアプリを起動できるようにします。 COM のアクティブ化または AUMID を手動で構成する必要はありません。
アプリ通知を送信する
AppNotificationBuilder を使用して通知コンテンツを作成し、AppNotificationManager.Show を使用して通知を送信します。
var notification = new AppNotificationBuilder()
.AddArgument("action", "viewItem")
.AddText("Console Notification")
.AddText("This was sent from a console app using Windows App SDK.")
.AddButton(new AppNotificationButton("Acknowledge")
.AddArgument("action", "acknowledge"))
.BuildNotification();
AppNotificationManager.Default.Show(notification);
アプリを実行したままにする
NotificationInvoked ハンドラーを呼び出すには、ユーザーが通知をクリックしたときにコンソール アプリが引き続き実行されている必要があります。 ユーザーが通知を操作する前にアプリが終了すると、次のクリックで新しいプロセスがコールド 起動されます。
Console.WriteLine("Notification sent! Waiting for activation...");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
// Unregister when the app exits
AppNotificationManager.Default.Unregister();
完全なコード例
通知を送信し、アクティブ化を処理する完全な Program.cs を次に示します。
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
Console.WriteLine("Console App Notification Test");
// Step 1: Register for notification activation
AppNotificationManager.Default.NotificationInvoked += (sender, args) =>
{
Console.WriteLine($"Notification activated! Arguments: {args.Argument}");
};
AppNotificationManager.Default.Register();
// Step 2: Send a notification
var notification = new AppNotificationBuilder()
.AddArgument("action", "viewItem")
.AddText("Console Notification")
.AddText("This was sent from a console app using Windows App SDK.")
.AddButton(new AppNotificationButton("Acknowledge")
.AddArgument("action", "acknowledge"))
.BuildNotification();
AppNotificationManager.Default.Show(notification);
// Step 3: Wait for user interaction
Console.WriteLine("Notification sent! Click it to test activation.");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
AppNotificationManager.Default.Unregister();
関連するコンテンツ
Windows developer