Commit 66be202
msftbot[bot]
New Win32 toast notification helpers (no more shortcut needed!) (#3457)
## Fixes #
**Dramatic Win32 improvements!** It's finally effortless for Win32 apps now!
* **No longer need a start menu shortcut!** - This was the biggest complaint
* **COM activator is automatically registered** - Simply subscribe to the OnActivated callback
**Before for Win32 apps**
1. Generate a new GUID and AUMID
1. Update your shortcut to use your GUID and AUMID
1. Create a NotificationActivator class using your GUID
1. Register your AUMID and COM server
1. Register your activator
1. You can finally now send notifications
```csharp
// (Omitted - setting up your shortcut using your installer)
// Register AUMID and COM server
DesktopNotificationManagerCompat.RegisterAumidAndComServer<MyNotificationActivator>("YourCompany.YourApp");
// Register COM server and activator type
DesktopNotificationManagerCompat.RegisterActivator<MyNotificationActivator>();
// The GUID CLSID must be unique to your app. Create a new GUID if copying this code.
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(INotificationActivationCallback))]
[Guid("replaced-with-your-guid-C173E6ADF0C3"), ComVisible(true)]
public class MyNotificationActivator : NotificationActivator
{
public override void OnActivated(string invokedArgs, NotificationUserInput userInput, string appUserModelId)
{
// TODO: Handle activation
}
}
// Finally can send...
```
**After for Win32 apps**
1. Send a notification! Literally just install the package and send away. To receive activation, subscribe to OnActivated. No GUIDs, no AUMIDs, all super straightforward.
```csharp
new ToastContentBuilder()
.AddText("Hello world")
.Show();
// Listen to toast notification activations
ToastNotificationManagerCompat.OnActivated += e =>
{
// TODO: Handle activation
}
```
**Also simplifies the APIs for showing toasts!** New Show() and Schedule() methods on ToastContentBuilder to simplify showing toasts.
**Before**
```csharp
var content = new ToastContentBuilder()
.AddText("Hello world")
.GetToastContent();
var notif = new ToastNotification(content.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(notif);
```
**After**
(Plus now both Win32 and UWP can use the exact same API)
```csharp
new ToastContentBuilder()
.AddText("Hello world")
.Show();
```
Customization of the toast notification can still be done using an overload
```csharp
new ToastContentBuilder()
.AddText("Hello world")
.Show(notif =>
{
notif.Tag = "myTag";
});
```
Also on ToastContentBuilder *added*... `.AddToastActivationInfo("myArgs")` defaults to Foreground so don't have to specify that 95% of the time, also added option for adding a ToastAudio object `AddAudio(new ToastAudio())` (useful for if you want to specify a silent toast, otherwise that's impossible today).
## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->
<!-- - Bugfix -->
Feature
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->
## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
See above
## What is the new behavior?
<!-- Describe how was this issue resolved or changed? -->
See above. Can now send toasts without having to set up a start menu shortcut... in fact, can send notifications without setting up anything!
### High level changes
* New `Show()` method added to `ToastContentBuilder` for easily showing a toast (works with Win32 apps too)
* New `Schedule()` method added to `ToastContentBuilder` for easily scheduling a toast (works with Win32 apps too)
* New `ToastNotificationManagerCompat` class added for converging Win32 and UWP API surface, and drastically simplifying sending toasts from Win32 (no longer need start menu shortcut, don't need to create the COM activator)
* New `GetXml()` method added to `ToastContentBuilder` for obtaining XML without having to call `GetToastContent().GetXml()`
* Made the activationType optional in `AddToastActivationInfo` so can set foreground activation more concisely (95% of time it's foreground activation)
* Added a `ToastAudio` overload for `AddAudio` so can set silent audio
* C++ UWP support for ToastContentBuilder class
* `ToastArguments` is now part of the toolkit (don't need to use an external query string library)
The `DesktopNotificationManagerCompat` classes have been left identical for back-compat purposes. When developers update their package, they do NOT have to make any changes if they don't want to. There are [migration steps described here](https://review.docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/migrating-to-toastcompat?branch=aleader%2Fconverged-uwp-win32&tabs=uwp).
## PR Checklist
Please check if your PR fulfills the following requirements:
- [x] Tested code with current [supported SDKs](../readme.md#supported)
- [x] Tested with sparse-signed package
- [x] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). [Preview docs](https://docstaging.z5.web.core.windows.net/aleader/toolkit-7/design/shell/tiles-and-notifications/send-local-toast.html?tabs=uwp)
- [x] Sample in sample app has been added / updated (for bug fixes / features)
- [x] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)
- [x] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [x] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [x] Contains **NO** breaking changes
<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below.
Please note that breaking changes are likely to be rejected. -->
## How verified
* Tested NuGet package from CI server on all test apps within here: https://github.com/andrewleader/NotifTestApps
* Includes UWP, .NET Core, and .NET Framework apps, and legacy versions of .NET Core and .NET Framework apps for testing back-compat and zero breaking changes
* Tested on commit 04a7e71
* Also tested with sparse signed package sampleFile tree
56 files changed
+4585
-188
lines changed- Microsoft.Toolkit.Uwp.Notifications
- Adaptive
- Tiles
- Toasts
- Builder
- Compat
- Desktop
- Native
- Microsoft.Toolkit.Uwp.SampleApp/SamplePages
- BackgroundTaskHelper
- Toast
- WeatherLiveTileAndToast
- Microsoft.Toolkit.Uwp.Samples.BackgroundTasks
- Microsoft.Toolkit.Win32.WpfCore.SampleApp.PackagingProject
- Images
- Microsoft.Toolkit.Win32.WpfCore.SampleApp
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
56 files changed
+4585
-188
lines changedLines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
| |||
35 | 40 | | |
36 | 41 | | |
37 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
38 | 48 | | |
39 | 49 | | |
40 | 50 | | |
| |||
69 | 79 | | |
70 | 80 | | |
71 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
72 | 95 | | |
73 | 96 | | |
Lines changed: 4 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 32 | | |
41 | 33 | | |
42 | | - | |
| 34 | + | |
43 | 35 | | |
44 | 36 | | |
| 37 | + | |
| 38 | + | |
45 | 39 | | |
46 | 40 | | |
47 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
Lines changed: 39 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
Lines changed: 111 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| |||
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
48 | 81 | | |
49 | 82 | | |
50 | 83 | | |
| |||
53 | 86 | | |
54 | 87 | | |
55 | 88 | | |
56 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
57 | 93 | | |
58 | 94 | | |
59 | 95 | | |
60 | 96 | | |
61 | 97 | | |
62 | 98 | | |
63 | 99 | | |
64 | | - | |
| 100 | + | |
65 | 101 | | |
66 | 102 | | |
67 | 103 | | |
| |||
76 | 112 | | |
77 | 113 | | |
78 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
79 | 120 | | |
80 | 121 | | |
81 | 122 | | |
82 | 123 | | |
83 | 124 | | |
84 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
85 | 137 | | |
86 | 138 | | |
87 | 139 | | |
88 | 140 | | |
89 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
90 | 155 | | |
91 | 156 | | |
92 | 157 | | |
| |||
96 | 161 | | |
97 | 162 | | |
98 | 163 | | |
99 | | - | |
| 164 | + | |
100 | 165 | | |
101 | 166 | | |
102 | 167 | | |
| |||
105 | 170 | | |
106 | 171 | | |
107 | 172 | | |
108 | | - | |
| 173 | + | |
109 | 174 | | |
110 | 175 | | |
111 | 176 | | |
112 | 177 | | |
113 | 178 | | |
114 | 179 | | |
115 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
116 | 204 | | |
117 | 205 | | |
118 | 206 | | |
119 | 207 | | |
120 | 208 | | |
121 | 209 | | |
122 | 210 | | |
123 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
124 | 220 | | |
125 | 221 | | |
126 | 222 | | |
127 | | - | |
| 223 | + | |
128 | 224 | | |
129 | 225 | | |
130 | 226 | | |
131 | 227 | | |
132 | | - | |
| 228 | + | |
133 | 229 | | |
134 | 230 | | |
135 | 231 | | |
136 | 232 | | |
137 | 233 | | |
138 | 234 | | |
139 | 235 | | |
| 236 | + | |
140 | 237 | | |
141 | 238 | | |
142 | 239 | | |
| |||
145 | 242 | | |
146 | 243 | | |
147 | 244 | | |
148 | | - | |
| 245 | + | |
149 | 246 | | |
150 | 247 | | |
151 | 248 | | |
| |||
157 | 254 | | |
158 | 255 | | |
159 | 256 | | |
160 | | - | |
| 257 | + | |
161 | 258 | | |
162 | 259 | | |
163 | 260 | | |
| |||
185 | 282 | | |
186 | 283 | | |
187 | 284 | | |
188 | | - | |
| 285 | + | |
189 | 286 | | |
190 | 287 | | |
191 | 288 | | |
192 | 289 | | |
193 | | - | |
| 290 | + | |
194 | 291 | | |
195 | 292 | | |
196 | 293 | | |
| |||
203 | 300 | | |
204 | 301 | | |
205 | 302 | | |
| 303 | + | |
206 | 304 | | |
207 | 305 | | |
208 | 306 | | |
| |||
218 | 316 | | |
219 | 317 | | |
220 | 318 | | |
221 | | - | |
222 | 319 | | |
0 commit comments