`AppInstance.GetActivatedEventArgs` crashes if called before `AppNotificationManager.Register`
Describe the bug
When toast activated from the background, AppInstance.GetCurrent().GetActivatedEventArgs() crashes if AppNotificationManager.Default.Register() is not called before, because m_waitHandleForArgs is not initialized.
https://github.com/microsoft/WindowsAppSDK/blob/f2234048b7beb7eb7216658e6f801fdb70720dfe/dev/AppNotifications/AppNotificationManager.cpp#L96
Steps to reproduce the bug
- Follow Send a local toast notification from C# apps to set up a packaged app.
- In
Package.appxmanifest, replace-ToastActivatedwith----AppNotificationActivated:. - In the app's startup code, replace
ToastNotificationManagerCompat.OnActivatedwithAppInstance.GetCurrent().GetActivatedEventArgs(). - Click the notification when the app is closed.
Expected behavior
An exception thrown or anything else better.
Screenshots
No response
NuGet package version
1.2.220727.1-experimental1
Packaging type
Packaged (MSIX)
Windows version
Windows 11 version 21H2 (22000)
IDE
Visual Studio 2022-preview
Additional context
No response
This behavior is by design and is the same for both Push/AppNotifications. The activated app must call AppNotificationManager.Default().Register() before calling GetActivatedEventArgs or else the app will crash.
This should be documented in the AppNotification Docs but it is not mentioned at all.
Specifically, we should add this in the documentation at Step 3.
Regardless of all these things, GetActivatedEventArgs shouldn't throw if the app was activated by App/PushNotifications and the app failed to register. It should just return nullptr.
We need to update our documentation with this pattern. @robertzhou-wpn
I've filed an issue to address the throwing behavior to instead return null args. https://github.com/microsoft/WindowsAppSDK/issues/2961
@drewbatgit, can you please help to update the AppNotification Docs?
The doc AppNotification Docs has been updated.