How can I add desktop notifications with WinUI 3?

Viewed 121

Does WinUI 3 have the feature to add desktop notifications?

See reference (see below)

Desktop Notification

2 Answers
  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

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;
}
Related