Skip to content

Instantly share code, notes, and snippets.

@mathematicalcoffee
Created December 12, 2012 00:14
Show Gist options
  • Save mathematicalcoffee/4263658 to your computer and use it in GitHub Desktop.
Save mathematicalcoffee/4263658 to your computer and use it in GitHub Desktop.
Code snippets demonstrating how to send a notification to the user in GNOME-shell 3.2, 3.4 and 3.6
/** GNOME 3.2 and 3.4: sending a notification to the user.
* In this example we provide the Source's icon through the notification rather than the source
* (note the 'icon:' property in the object provided as the fourth parameter to the Notification.)
*/
// 1. Make a source
let source = new MessageTray.Source("source title");
// 2. Make a notification
let notification = new MessageTray.Notification(source,
"notification title",
"notification message",
{icon: new St.Icon({
icon_name: 'avatar-default',
icon_size: source.ICON_SIZE
})});
// 3. Add the source to the message tray
Main.messageTray.add(source);
// 4. notify!
source.notify(notification);
/** GNOME 3.2 and 3.4: sending a notification to the user.
* In this example we provide the Source's icon by subclassing Source and implementiong createNotificationIcon.
* Once this is done, sending the ntification is quite simple.
*/
const MySource = new Lang.Class({
Name: 'MySource',
Extends: MessageTray.Source,
_init: function (title) {
// call the parent constructor
this.parent(title);
// set our icon
this._setSummaryIcon(this.createNotificationIcon());
},
// this should return an icon to show in the message tray. You can use
// this.ICON_SIZE for a default icon size if you want.
createNotificationIcon: function () {
return new St.Icon({
icon_name: 'avatar-default',
icon_size: this.ICON_SIZE
});
}
});
/** Send the notification with our new source! */
// 1. Make a source
let source = new MySource("source title");
// 2. Make a notification
let notification = new MessageTray.Notification(source,
"notification title",
"notification message");
// 3. Add the source to the message tray
Main.messageTray.add(source);
// 4. notify!
source.notify(notification);
/** GNOME 3.6: sending a notification to the user.
* In GNOME 3.6 one can provide the Source and Notification icons by passing in
* the icon name to the Source constructor.
* Much simpler than GNOME 3.2 or GNOME 3.4.
*/
// 1. Make a source
let source = new MessageTray.Source("source title", 'avatar-default');
// 2. Make a notification
let notification = new MessageTray.Notification(source,
"notification title",
"notification message");
// 3. Add the source to the message tray
Main.messageTray.add(source);
// 4. notify!
source.notify(notification);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment