4

Does WinUI 3 have the feature to add desktop notifications?

See reference (see below)

Desktop Notification

Rand Random
  • 7,300
  • 10
  • 40
  • 88

2 Answers2

6

Use build-in AppNotification class:

// using Microsoft.Windows.AppNotifications;

public static bool SendNotificationToast(string title, string message)
{
    var xmlPayload = new string($@"
        <toast>    
            <visual>    
                <binding template=""ToastGeneric"">    
                    <text>{title}</text>
                    <text>{message}</text>    
                </binding>
            </visual>  
        </toast>");

    var toast = new AppNotification(xmlPayload);
    AppNotificationManager.Default.Show(toast);
    return toast.Id != 0;
}

Update 2023-03-12

Since Windows App SDK 1.2 You can use AppNotificationBuilder class.

public static bool SendNotificationToast(string title, string message)
{
    var toast = new AppNotificationBuilder()
        .AddText(title)
        .AddText(message)
        .BuildNotification();

    AppNotificationManager.Default.Show(toast);
    return toast.Id != 0;
}

More advanced examples are available at Microsoft Learn.

Kuba Szostak
  • 356
  • 3
  • 5
1
  1. Install the Nuget package:
    • Microsoft.Toolkit.Uwp.Notifications
  2. Use 'ToastContentBuilder' to build the notification content.
  3. Handling activation
    • After showing a notification, you will likely need to handle the user clicking the notification. Whether that means bringing up specific content after the user clicks it, opening your app in general, or performing an action when the user clicks the notification.

Reference:

Microsoft Docs

JerodG
  • 1,248
  • 1
  • 16
  • 34