fix: handle picky Win11 ToastNotificationService #487
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
On a "virgin" Windows 11 machine without VS and notifications off, I got this exception:
I probed a little with my bullshitting buddy ChatGPT and found a possible reason and a fix for this.
possible reason:
Yes, `ToastNotificationManagerCompat.CreateToastNotifier()` may try to write to the registry.
Yes, `ToastNotificationManagerCompat.CreateToastNotifier()` may try to write to the registry. The method is used to create a `ToastNotifier` object which can be used to display toast notifications on the Windows 10 desktop. In order to create the `ToastNotifier` object, the method needs to access the Windows registry to determine the current user's settings for notifications.
Specifically,
CreateToastNotifier()
callsToastNotificationManagerCompat.Initialize()
which initializes the Windows Runtime Toast Notification API and attempts to create aToastNotifier
object. During the initialization process, the method attempts to create a registry key underHKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\Notifications
with a subkey namedSettings
, which contains various settings related to notifications. If there is an issue with creating this registry key or accessing its subkeys, it can cause anIOException
or anInvalidOperationException
to be thrown.a lot of BSing later, the solution appears to be to explicitly declare a ToastNotifierCompat variable and call ToastNotificationManagerCompat.CreateToastNotifier - I only wanted to catch the possible exception, but apparently forces this a hard cast to ToastNotifierCompat as the target object, which changes the creation process of the object and doesn't use the registry at all, at least without an exception. 🤷♂️😎
This is the PR description ChatGPT generates:
ChtGPT nicely describing the issue
This pull request updates the ToastNotificationService in Greenshot to explicitly create a ToastNotifierCompat instance, which avoids an issue with the Windows registry that could cause notifications to fail.
Previously, the CreateToastNotifier method was used to create a ToastNotifier instance, which could return an object with properties other than ToastNotifierCompat. This approach also relied on the system registry to determine the correct application ID, which could be problematic if the ID was not correctly registered. The new approach avoids these issues by explicitly creating a ToastNotifierCompat instance with the correct application ID. This change has been tested successfully on both Windows 10 and Windows 11.
Yes I have tested on Windows 10 and 11, and Windows 11 arm64.