Skip to content

Commit 4c96a79

Browse files
Max-Mengstevewhims
andauthored
Update toast-notifications.md (#5402)
* Update toast-notifications.md we should check whether args.Argument.Contains("action"), otherwise, when user click on an empty area of the notification, no such argument is passed, and the application will crash. * Update toast-notifications.md --------- Co-authored-by: Steven White <[email protected]>
1 parent 76f4953 commit 4c96a79

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

hub/apps/windows-app-sdk/migrate-to-windows-app-sdk/guides/toast-notifications.md

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void LaunchAndBringToForegroundIfNeeded()
189189
m_window.Activate();
190190

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

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

211211

212212
dispatcherQueue.TryEnqueue(async delegate
213213
{
214-
215-
switch (args.Arguments["action"])
214+
if (args.Argument.Contains("action"))
216215
{
217-
// Send a background message
218-
case "sendMessage":
219-
string message = args.UserInput["textBox"].ToString();
220-
// TODO: Send it
221-
222-
// If the UI app isn't open
223-
if (m_window == null)
224-
{
225-
// Close since we're done
226-
Process.GetCurrentProcess().Kill();
227-
}
228-
229-
break;
230-
231-
// View a message
232-
case "viewMessage":
233-
234-
// Launch/bring window to foreground
235-
LaunchAndBringToForegroundIfNeeded();
236-
237-
// TODO: Open the message
238-
break;
216+
switch (args.Arguments["action"])
217+
{
218+
// Send a background message.
219+
case "sendMessage":
220+
string message = args.UserInput["textBox"].ToString();
221+
// TODO: Send it.
222+
223+
// If the UI app isn't open.
224+
if (m_window == null)
225+
{
226+
// Close since we're done.
227+
Process.GetCurrentProcess().Kill();
228+
}
229+
230+
break;
231+
232+
// View a message.
233+
case "viewMessage":
234+
235+
// Launch/bring window to foreground.
236+
LaunchAndBringToForegroundIfNeeded();
237+
238+
// TODO: Open the message.
239+
break;
240+
}
241+
}
242+
else
243+
{
244+
Debug.Print("Notification args is null");
239245
}
240246
});
241247
}
@@ -281,14 +287,14 @@ public static DispatcherQueue DispatcherQueue { get; private set; }
281287

282288
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
283289
{
284-
// Get the app-level dispatcher
290+
// Get the app-level dispatcher.
285291
DispatcherQueue = global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
286292

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

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

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

315321
private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
316322
{
317-
// Use the dispatcher from the window if present, otherwise the app dispatcher
323+
// Use the dispatcher from the window if present, otherwise the app dispatcher.
318324
var dispatcherQueue = m_window?.DispatcherQueue ?? App.DispatcherQueue;
319325

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

324330
switch (args["action"])
325331
{
326-
// Send a background message
332+
// Send a background message.
327333
case "sendMessage":
328334
string message = e.UserInput["textBox"].ToString();
329-
// TODO: Send it
335+
// TODO: Send it.
330336
331-
// If the UI app isn't open
337+
// If the UI app isn't open.
332338
if (m_window == null)
333339
{
334-
// Close since we're done
340+
// Close since we're done.
335341
Process.GetCurrentProcess().Kill();
336342
}
337343

338344
break;
339345

340-
// View a message
346+
// View a message.
341347
case "viewMessage":
342348

343-
// Launch/bring window to foreground
349+
// Launch/bring window to foreground.
344350
LaunchAndBringToForegroundIfNeeded();
345351

346-
// TODO: Open the message
352+
// TODO: Open the message.
347353
break;
348354
}
349355
});

0 commit comments

Comments
 (0)