notify2 is a replacement for pynotify which can be used from different GUI toolkits and from programs without a GUI. The API is largely the same as that of pynotify, but some less important parts are left out.
You can alternatively use the GObject Introspection bindings to libnotify (from gi.repository import Notify). I’d recommend that for GTK applications, while notify2 has fewer dependencies for non-GTK applications. It should be easy to switch between the two.
Notifications are sent to a notification daemon over D-Bus, according to the Desktop notifications spec, and the server is responsible for displaying them to the user. So your application has limited control over when and how a notification appears. For example, Ubuntu uses the NotifyOSD daemon.
Initialise the D-Bus connection. Must be called before you send any notifications, or retrieve server info or capabilities.
To get callbacks from notifications, DBus must be integrated with a mainloop. There are three ways to achieve this:
If you only want to display notifications, without receiving information back from them, you can safely omit mainloop.
Get a list of server capabilities.
These are short strings, listed in the spec. Vendors may also list extra capabilities with an ‘x-‘ prefix, e.g. ‘x-canonical-append’.
A notification object.
Ask the server to show the notification.
Call this after you have finished setting any parameters of the notification that you want.
Set the urgency level to one of URGENCY_LOW, URGENCY_NORMAL or URGENCY_CRITICAL.
Set the display duration in milliseconds, or one of the special values EXPIRES_DEFAULT or EXPIRES_NEVER. This is a request, which the server might ignore.
Only exists for compatibility with pynotify; you can simply set:
n.timeout = 5000
n.set_hint(key, value) <–> n.hints[key] = value
See hints in the spec.
Only exists for compatibility with pynotify.
To receive callbacks, you must have set a D-Bus event loop when you called init().
Set the callback for the notification closing; the only valid value for event is ‘closed’ (the parameter is kept for compatibility with pynotify).
The callback will be called with the Notification instance.
Add an action to the notification.
Check for the ‘actions’ server capability before using this.
Urgency levels to pass to Notification.set_urgency().
Special expiration times to pass to Notification.set_timeout().