WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

`AppInstance.GetActivatedEventArgs` crashes if called before `AppNotificationManager.Register`

Open nxtn-staged opened this issue 3 years ago • 4 comments

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

  1. Follow Send a local toast notification from C# apps to set up a packaged app.
  2. In Package.appxmanifest, replace -ToastActivated with ----AppNotificationActivated:.
  3. In the app's startup code, replace ToastNotificationManagerCompat.OnActivated with AppInstance.GetCurrent().GetActivatedEventArgs().
  4. 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

nxtn-staged avatar Sep 10 '22 15:09 nxtn-staged

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.

pmpurifoy avatar Sep 10 '22 16:09 pmpurifoy

We need to update our documentation with this pattern. @robertzhou-wpn

pmpurifoy avatar Sep 10 '22 16:09 pmpurifoy

I've filed an issue to address the throwing behavior to instead return null args. https://github.com/microsoft/WindowsAppSDK/issues/2961

pmpurifoy avatar Sep 10 '22 17:09 pmpurifoy

@drewbatgit, can you please help to update the AppNotification Docs?

robertzhou-wpn avatar Sep 12 '22 17:09 robertzhou-wpn

The doc AppNotification Docs has been updated.

robertzhou-wpn avatar Sep 26 '22 05:09 robertzhou-wpn