Skip to content

Commit 2962293

Browse files
trop[bot]codebytereckerr
authored
feat: support notification priority on Windows (#50382)
* feat: support notification priority on Windows Add Windows notifications support urgency/priority levels. This maps the existing `urgency` option (previously Linux-only) to Windows toast notification priorities: - 'critical' maps to ToastNotificationPriority_High, which sorts the notification above default-priority items in Action Center. - 'normal' and 'low' both map to ToastNotificationPriority_Default. Note that on Windows, 'critical' priority does not prevent the toast from being auto-dismissed. Users should additionally set `timeoutType` to 'never' for that behavior. Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: make linter happy Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
1 parent 8b9e721 commit 2962293

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

docs/api/notification.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ Returns `boolean` - Whether or not desktop notifications are supported on the cu
4242
* `timeoutType` string (optional) _Linux_ _Windows_ - The timeout duration of the notification. Can be 'default' or 'never'.
4343
* `replyPlaceholder` string (optional) _macOS_ - The placeholder to write in the inline reply input field.
4444
* `sound` string (optional) _macOS_ - The name of the sound file to play when the notification is shown.
45-
* `urgency` string (optional) _Linux_ - The urgency level of the notification. Can be 'normal', 'critical', or 'low'.
45+
* `urgency` string (optional) _Linux_ _Windows_ - The urgency level of the notification. Can be 'normal', 'critical', or 'low'.
4646
* `actions` [NotificationAction[]](structures/notification-action.md) (optional) _macOS_ - Actions to add to the notification. Please read the available actions and limitations in the `NotificationAction` documentation.
4747
* `closeButtonText` string (optional) _macOS_ - A custom title for the close button of an alert. An empty string will cause the default localized text to be used.
4848
* `toastXml` string (optional) _Windows_ - A custom description of the Notification on Windows superseding all properties above. Provides full customization of design and behavior of the notification.
4949

50+
> [!NOTE]
51+
> On Windows, `urgency` type 'critical' sorts the notification higher in Action Center (above default priority notifications), but does not prevent auto-dismissal. To prevent auto-dismissal, you should also set
52+
> `timeoutType` to 'never'.
53+
5054
### Instance Events
5155

5256
Objects created with `new Notification` emit the following events:

shell/browser/notifications/notification.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct NotificationOptions {
4545
std::u16string timeout_type;
4646
std::u16string reply_placeholder;
4747
std::u16string sound;
48-
std::u16string urgency; // Linux
48+
std::u16string urgency; // Linux/Windows
4949
std::vector<NotificationAction> actions;
5050
std::u16string close_button_text;
5151
std::u16string toast_xml;

shell/browser/notifications/win/windows_toast_notification.cc

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ void WindowsToastNotification::CreateToastNotificationOnBackgroundThread(
280280
// Continue to create the toast notification
281281
ComPtr<ABI::Windows::UI::Notifications::IToastNotification>
282282
toast_notification;
283-
if (!CreateToastNotification(toast_xml, notification_id, weak_notification,
284-
ui_task_runner, &toast_notification)) {
283+
if (!CreateToastNotification(toast_xml, options, notification_id,
284+
weak_notification, ui_task_runner,
285+
&toast_notification)) {
285286
return; // Error already posted to UI thread
286287
}
287288

@@ -349,6 +350,7 @@ bool WindowsToastNotification::CreateToastXmlDocument(
349350
// returns the created notification via out parameter.
350351
bool WindowsToastNotification::CreateToastNotification(
351352
ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument> toast_xml,
353+
const NotificationOptions& options,
352354
const std::string& notification_id,
353355
base::WeakPtr<Notification> weak_notification,
354356
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
@@ -416,6 +418,27 @@ bool WindowsToastNotification::CreateToastNotification(
416418
return false;
417419
}
418420

421+
ComPtr<winui::Notifications::IToastNotification4> toast4;
422+
hr = (*toast_notification)->QueryInterface(IID_PPV_ARGS(&toast4));
423+
if (SUCCEEDED(hr)) {
424+
winui::Notifications::ToastNotificationPriority priority =
425+
winui::Notifications::ToastNotificationPriority::
426+
ToastNotificationPriority_Default;
427+
if (options.urgency == u"critical") {
428+
priority = winui::Notifications::ToastNotificationPriority::
429+
ToastNotificationPriority_High;
430+
}
431+
432+
hr = toast4->put_Priority(priority);
433+
if (FAILED(hr)) {
434+
std::string err = base::StrCat({"WinAPI: Setting priority failed, ERROR ",
435+
FailureResultToString(hr)});
436+
DebugLog(err);
437+
PostNotificationFailedToUIThread(weak_notification, err, ui_task_runner);
438+
return false;
439+
}
440+
}
441+
419442
return true;
420443
}
421444

shell/browser/notifications/win/windows_toast_notification.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class WindowsToastNotification : public Notification {
9494
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
9595
static bool CreateToastNotification(
9696
ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument> toast_xml,
97+
const NotificationOptions& options,
9798
const std::string& notification_id,
9899
base::WeakPtr<Notification> weak_notification,
99100
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,

0 commit comments

Comments
 (0)