notify2 API documentation¶
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:
- Set a default mainloop (dbus.set_default_main_loop) before calling init()
- Pass the mainloop parameter as a string ‘glib’ or ‘qt’ to integrate with those mainloops. (N.B. passing ‘qt’ currently makes that the default dbus mainloop, because that’s the only way it seems to work.)
- Pass the mainloop parameter a DBus compatible mainloop instance, such as dbus.mainloop.glib.DBusGMainLoop().
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’.
Get basic information about the server.
Creating and showing notifications¶
Notification(summary, message='', icon='')¶
A notification object.
- summary : str
- The title text
- message : str
- The body text, if the server has the ‘body’ capability.
- icon : str
- Path to an icon image, or the name of a stock icon. Stock icons available
in Ubuntu are listed here.
You can also set an icon from data in your application - see
Ask the server to show the notification.
Call this after you have finished setting any parameters of the notification that you want.
update(summary, message='', icon=None)¶
Replace the summary and body of the notification, and optionally its icon. You should call
show()again after this to display the updated notification.
Ask the server to close this notification.
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
Set the notification location as (x, y), if the server supports it.
Set a custom icon from a GdkPixbuf.
n.set_hint(key, value) <–> n.hints[key] = value
See hints in the spec.
Only exists for compatibility with pynotify.
Set a hint with a dbus byte value. The input value can be an integer or a bytes string of length 1.
To receive callbacks, you must have set a D-Bus event loop when you called
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
add_action(action, label, callback, user_data=None)¶
Add an action to the notification.
Check for the ‘actions’ server capability before using this.
- action : str
- A brief key.
- label : str
- The text displayed on the action button
- callback : callable
- A function taking at 2-3 parameters: the Notification object, the action key and (if specified) the user_data.
- user_data :
- An extra argument to pass to the callback.