Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add desktop notifications with WinUI 3?

Does WinUI 3 have the feature to add desktop notifications?

See reference (see below)

Desktop Notification

like image 927
briannarich Avatar asked Oct 21 '25 10:10

briannarich


2 Answers

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.

like image 159
Kuba Szostak Avatar answered Oct 23 '25 23:10

Kuba Szostak


  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

like image 33
Ambadi_M_Nair Avatar answered Oct 24 '25 00:10

Ambadi_M_Nair