microsoft / microsoft/WindowsAppSDK

If process is ended right after AppNotificationManager.Show() method call, the toast notification is not drawn

Open
#3,437 0 comments 2 reactions 0 assignees View on GitHub
area-Notifications bug
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

### Describe the bug

If a process is ended right after `AppNotificationManager.Show` method call, the toast notification is not drawn. So I need to wait for some unknown period of time.

### Steps to reproduce the bug

I have the following scenario: a raw push notification is received by my app, then it's saved to a local db and the corresponding toast notification is drawn. Assume I have the following schematical code snippet:
```
void Main()
{
var rawNotification = GetRawNotificationFromAppArgs();
SaveRawNotificationToLocalDb(rawNotification);
var appNotification = CreateAppNotificationFromRaw(rawNotification);
AppNotificationManager.Default.Register();
AppNotificationManager.Default.Show(appNotification);
Thread.Sleep(1); <------- need to wait here for 1ms on my PC, if we want to see toast notification drawn
AppNotificationManager.Default.Unregister(); <--- even if we remove this line, the problem remains
}
```
Here I want to do all things as quickly as possible and exit. The problem is that if `Thread.Sleep` line is commented out, then the app notification is not drawn. So I need to wait for some unknown period of time until I will be able to end the process if I want to see a toast notification drawn. Currently on my PC 1 ms of waiting is enough, but I'm not happy with this solution - I don't know if it would be enough for clients' PCs, and, on the other hand, I don't want this process to live more than it's needed and consume system resources. So it seems, that `AppNotificationManager.Show` method is inherently async, but I have only sync interface and so I don't know when I'm able to exit.

### Expected behavior

I would like to have a mechanism to know when I can end the process. Either the `AppNotificationManager.Show` method should be really sync and blocking and return only when the drawing will definitely occur, or (which is more preferable), I would like to have something like `ShowAsync`, which, again, would set its Task as completed only after the drawing will definitely occur.
At least for now I would like to know what is the guaranteed minimal amount of time, that I need to wait until the notification will be definitely drawn.

### Screenshots

_No response_

### NuGet package version

Windows App SDK 1.2.3: 1.2.230118.102

### Packaging type

Packaged (MSIX)

### Windows version

Windows 10 version 21H2 (19044, November 2021 Update)

### IDE

Visual Studio 2022-preview

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the AppNotificationManager.Register, Show, and Unregister calls in the reported reproduction, then investigate what happens when the process exits immediately afterward. Reproduce the Windows App SDK 1.2.3 scenario on Windows 10 21H2 and verify that the toast is drawn reliably without an arbitrary Thread.Sleep delay; the issue does not name implementation files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.