Skip to content

Commit ffe2182

Browse files
authored
feat: set urgency on linux notifications (electron#20152)
1 parent e510af7 commit ffe2182

File tree

7 files changed

+36
-1
lines changed

7 files changed

+36
-1
lines changed

Diff for: BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ if (is_linux) {
280280
"notify_notification_add_action",
281281
"notify_notification_set_image_from_pixbuf",
282282
"notify_notification_set_timeout",
283+
"notify_notification_set_urgency",
283284
"notify_notification_set_hint_string",
284285
"notify_notification_show",
285286
"notify_notification_close",

Diff for: docs/api/notification.md

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Returns `Boolean` - Whether or not desktop notifications are supported on the cu
3737
* `hasReply` Boolean (optional) _macOS_ - Whether or not to add an inline reply option to the notification.
3838
* `replyPlaceholder` String (optional) _macOS_ - The placeholder to write in the inline reply input field.
3939
* `sound` String (optional) _macOS_ - The name of the sound file to play when the notification is shown.
40+
* `urgency` String (optional) _Linux_ - The urgency level of the notification. Can be 'normal', 'critical', or 'low'.
4041
* `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.
4142
* `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.
4243

@@ -144,6 +145,12 @@ A `Boolean` property representing whether the notification is silent.
144145

145146
A `Boolean` property representing whether the notification has a reply action.
146147

148+
#### `notification.urgency` _Linux_
149+
150+
A `String` property representing the urgency level of the notification. Can be 'normal', 'critical', or 'low'.
151+
152+
Default is 'low' - see [NotifyUrgency](https://developer.gnome.org/notification-spec/#urgency-levels) for more information.
153+
147154
#### `notification.actions`
148155

149156
A [`NotificationAction[]`](structures/notification-action.md) property representing the actions of the notification.

Diff for: shell/browser/api/atom_api_notification.cc

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Notification::Notification(v8::Isolate* isolate,
6868
}
6969
opts.Get("silent", &silent_);
7070
opts.Get("replyPlaceholder", &reply_placeholder_);
71+
opts.Get("urgency", &urgency_);
7172
opts.Get("hasReply", &has_reply_);
7273
opts.Get("actions", &actions_);
7374
opts.Get("sound", &sound_);
@@ -118,6 +119,10 @@ base::string16 Notification::GetSound() const {
118119
return sound_;
119120
}
120121

122+
base::string16 Notification::GetUrgency() const {
123+
return urgency_;
124+
}
125+
121126
std::vector<electron::NotificationAction> Notification::GetActions() const {
122127
return actions_;
123128
}
@@ -155,6 +160,10 @@ void Notification::SetSound(const base::string16& new_sound) {
155160
sound_ = new_sound;
156161
}
157162

163+
void Notification::SetUrgency(const base::string16& new_urgency) {
164+
urgency_ = new_urgency;
165+
}
166+
158167
void Notification::SetActions(
159168
const std::vector<electron::NotificationAction>& actions) {
160169
actions_ = actions;
@@ -211,6 +220,7 @@ void Notification::Show() {
211220
options.actions = actions_;
212221
options.sound = sound_;
213222
options.close_button_text = close_button_text_;
223+
options.urgency = urgency_;
214224
notification_->Show(options);
215225
}
216226
}
@@ -238,6 +248,8 @@ void Notification::BuildPrototype(v8::Isolate* isolate,
238248
&Notification::SetHasReply)
239249
.SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder,
240250
&Notification::SetReplyPlaceholder)
251+
.SetProperty("urgency", &Notification::GetUrgency,
252+
&Notification::SetUrgency)
241253
.SetProperty("sound", &Notification::GetSound, &Notification::SetSound)
242254
.SetProperty("actions", &Notification::GetActions,
243255
&Notification::SetActions)

Diff for: shell/browser/api/atom_api_notification.h

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Notification : public mate::TrackableObject<Notification>,
5454
bool GetSilent() const;
5555
bool GetHasReply() const;
5656
base::string16 GetReplyPlaceholder() const;
57+
base::string16 GetUrgency() const;
5758
base::string16 GetSound() const;
5859
std::vector<electron::NotificationAction> GetActions() const;
5960
base::string16 GetCloseButtonText() const;
@@ -64,6 +65,7 @@ class Notification : public mate::TrackableObject<Notification>,
6465
void SetBody(const base::string16& new_body);
6566
void SetSilent(bool new_silent);
6667
void SetHasReply(bool new_has_reply);
68+
void SetUrgency(const base::string16& new_urgency);
6769
void SetReplyPlaceholder(const base::string16& new_reply_placeholder);
6870
void SetSound(const base::string16& sound);
6971
void SetActions(const std::vector<electron::NotificationAction>& actions);
@@ -80,6 +82,7 @@ class Notification : public mate::TrackableObject<Notification>,
8082
bool has_reply_ = false;
8183
base::string16 reply_placeholder_;
8284
base::string16 sound_;
85+
base::string16 urgency_;
8386
std::vector<electron::NotificationAction> actions_;
8487
base::string16 close_button_text_;
8588

Diff for: shell/browser/notifications/linux/libnotify_notification.cc

+10
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ void LibnotifyNotification::Show(const NotificationOptions& options) {
100100
nullptr);
101101
}
102102

103+
NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL;
104+
if (options.urgency == base::ASCIIToUTF16("critical")) {
105+
urgency = NOTIFY_URGENCY_CRITICAL;
106+
} else if (options.urgency == base::ASCIIToUTF16("low")) {
107+
urgency = NOTIFY_URGENCY_LOW;
108+
}
109+
110+
// Set the urgency level of the notification.
111+
libnotify_loader_.notify_notification_set_urgency(notification_, urgency);
112+
103113
if (!options.icon.drawsNothing()) {
104114
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
105115
libnotify_loader_.notify_notification_set_image_from_pixbuf(notification_,

Diff for: shell/browser/notifications/notification.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct NotificationOptions {
3434
bool has_reply;
3535
base::string16 reply_placeholder;
3636
base::string16 sound;
37+
base::string16 urgency; // Linux
3738
std::vector<NotificationAction> actions;
3839
base::string16 close_button_text;
3940

Diff for: spec-main/api-notification-dbus-spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ ifdescribe(!skip)('Notification module (dbus)', () => {
116116
actions: [],
117117
hints: {
118118
'append': 'true',
119-
'desktop-entry': appName
119+
'desktop-entry': appName,
120+
'urgency': 1
120121
}
121122
})
122123
})

0 commit comments

Comments
 (0)