I recently found a lot of third-party libraries that help send desktop notifications in Linux. I just don’t get it. Most of these libraries just wrap the call to
notify-send into a function or even an over-engineered class. You don’t really need these third-party libraries to do the job… You can do the same job with merely two or three new lines in your code.
notify-send is a command-line tool. You can call the command-line tool through Python’s
notify-send is a quick solution but requires
libnotify and a call to an external executable. I don’t like it. Therefore, I always send the notification directly with D-Bus’ Python binding. Most desktop notification servers follow a standard called Desktop Notifications Specification. It exposes a D-Bus interface at
org.freedesktop.Notifications and a method
org.freedesktop.Notifications.Notify to send notifications from users’ applications. It’s OK if you don’t know anything about D-Bus. Just remember that you can send a desktop notification through D-Bus’ Python binding:
The D-Bus’ Python binding can be installed from PyPI:
notfy_intf.Notify, the fourth argument is the notification title, and the content body is at the fifth argument. Regarding the urgency,
critical. The last argument indicates how long the notification box will be visible on the screen. The unit is milliseconds. So the
3000 here means 3 seconds. To understand the details of the arguments in the
notfy_intf.Notify, refer to the documentation of org.freedesktop.Notifications.Notify.
You can now see that you don’t really need those third-party libraries to send simple desktop notifications.