Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
An app notification is a UI popup that appears outside of your app's window, delivering timely information or actions to the user. Notifications can be purely informational, can launch your app when clicked, or can trigger a background action without bringing your app to the foreground.
This article walks you through the steps to create and send an app notification from a UWP app, and then handle activation when the user interacts with it.
For an overview of app notifications and guidance for other frameworks, see App notifications overview.
This article covers local notifications. For information about delivering notifications from a cloud service, see Push notifications overview.
Prerequisites
- A UWP app project in Visual Studio
- The Universal Windows Platform development workload installed in Visual Studio
Send an app notification
UWP apps use the Windows.UI.Notifications namespace to construct and send notifications using XML. This section shows how to send notifications using C# and C++.
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
var xml = @"<toast launch=""action=viewConversation&conversationId=9813"">
<visual>
<binding template=""ToastGeneric"">
<text>Andrew sent you a picture</text>
<text>Check this out, The Enchantments in Washington!</text>
</binding>
</visual>
</toast>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var notification = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(notification);
Handle activation
When the user clicks your notification (or a button on the notification with foreground activation), your app's OnActivated method is invoked. OnLaunched is not called for notification activations, even if your app was closed and is launching for the first time. We recommend combining OnLaunched and OnActivated into a shared initialization method.
App.xaml.cs
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
OnLaunchedOrActivated(e.PreviousExecutionState);
var rootFrame = Window.Current.Content as Frame;
if (e.PrelaunchActivated == false)
{
if (rootFrame?.Content == null)
{
rootFrame?.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
}
protected override void OnActivated(IActivatedEventArgs e)
{
OnLaunchedOrActivated(e.PreviousExecutionState);
if (e is ToastNotificationActivatedEventArgs toastArgs)
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame?.Content == null)
{
rootFrame?.Navigate(typeof(MainPage));
}
Window.Current.Activate();
// Parse the notification arguments
string argument = toastArgs.Argument;
// TODO: Navigate to the relevant content based on the arguments
}
}
private void OnLaunchedOrActivated(ApplicationExecutionState previousState)
{
if (Window.Current.Content is not Frame)
{
var rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
}
For information about adding buttons, images, inputs, audio, and other rich content to your notifications, see App notification content.