Skip to content

Update toast-notifications.md #5402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void LaunchAndBringToForegroundIfNeeded()
m_window.Activate();

// Additionally we show using our helper, since if activated via a app notification, it doesn't
// activate the window correctly
// activate the window correctly.
WindowHelper.ShowWindow(m_window);
}
else
Expand All @@ -205,37 +205,43 @@ private void NotificationManager_NotificationInvoked(AppNotificationManager send

private void HandleNotification(AppNotificationActivatedEventArgs args)
{
// Use the dispatcher from the window if present, otherwise the app dispatcher
// Use the dispatcher from the window if present, otherwise the app dispatcher.
var dispatcherQueue = m_window?.DispatcherQueue ?? DispatcherQueue.GetForCurrentThread();


dispatcherQueue.TryEnqueue(async delegate
{

switch (args.Arguments["action"])
if (args.Argument.Contains("action"))
{
// Send a background message
case "sendMessage":
string message = args.UserInput["textBox"].ToString();
// TODO: Send it

// If the UI app isn't open
if (m_window == null)
{
// Close since we're done
Process.GetCurrentProcess().Kill();
}

break;

// View a message
case "viewMessage":

// Launch/bring window to foreground
LaunchAndBringToForegroundIfNeeded();

// TODO: Open the message
break;
switch (args.Arguments["action"])
{
// Send a background message.
case "sendMessage":
string message = args.UserInput["textBox"].ToString();
// TODO: Send it.

// If the UI app isn't open.
if (m_window == null)
{
// Close since we're done.
Process.GetCurrentProcess().Kill();
}

break;

// View a message.
case "viewMessage":

// Launch/bring window to foreground.
LaunchAndBringToForegroundIfNeeded();

// TODO: Open the message.
break;
}
}
else
{
Debug.Print("Notification args is null");
}
});
}
Expand Down Expand Up @@ -281,14 +287,14 @@ public static DispatcherQueue DispatcherQueue { get; private set; }

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
// Get the app-level dispatcher
// Get the app-level dispatcher.
DispatcherQueue = global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();

// Register for toast activation. Requires Microsoft.Toolkit.Uwp.Notifications NuGet package version 7.0 or greater
// Register for toast activation. Requires Microsoft.Toolkit.Uwp.Notifications NuGet package version 7.0 or greater.
ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated;

// If we weren't launched by an app, launch our window like normal.
// Otherwise if launched by a toast, our OnActivated callback will be triggered
// Otherwise if launched by a toast, our OnActivated callback will be triggered.
if (!ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
{
LaunchAndBringToForegroundIfNeeded();
Expand All @@ -303,7 +309,7 @@ private void LaunchAndBringToForegroundIfNeeded()
m_window.Activate();

// Additionally we show using our helper, since if activated via a toast, it doesn't
// activate the window correctly
// activate the window correctly.
WindowHelper.ShowWindow(m_window);
}
else
Expand All @@ -314,7 +320,7 @@ private void LaunchAndBringToForegroundIfNeeded()

private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
{
// Use the dispatcher from the window if present, otherwise the app dispatcher
// Use the dispatcher from the window if present, otherwise the app dispatcher.
var dispatcherQueue = m_window?.DispatcherQueue ?? App.DispatcherQueue;

dispatcherQueue.TryEnqueue(delegate
Expand All @@ -323,27 +329,27 @@ private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivat

switch (args["action"])
{
// Send a background message
// Send a background message.
case "sendMessage":
string message = e.UserInput["textBox"].ToString();
// TODO: Send it
// TODO: Send it.

// If the UI app isn't open
// If the UI app isn't open.
if (m_window == null)
{
// Close since we're done
// Close since we're done.
Process.GetCurrentProcess().Kill();
}

break;

// View a message
// View a message.
case "viewMessage":

// Launch/bring window to foreground
// Launch/bring window to foreground.
LaunchAndBringToForegroundIfNeeded();

// TODO: Open the message
// TODO: Open the message.
break;
}
});
Expand Down