Post a custom notification in Linux using Python

Ramit
2 min readMar 24, 2020

Disclaimer :
This article is tailored for Fedora running GNOME.

Hello,
The Earth is pretty much locked down due to Coronavirus and I thought, let’s take up this opportunity to build something cool.

So, for today, let's start with Python + Linux (GNOME) Notification.

Target:
Write a python snippet to post a notification in Linux.

Use case:
Let’s say you are running a python script and want to get notified once it's done. This snippet will help you do just that. Try to imagine other potential use cases for this to yourself. Am doing this just for fun!

Let's get started!
We are going to be using the dbus library for this. You will need to get it installed, https://pypi.org/project/dbus-python/.

What is dbus you ask?
D-Bus is a mechanism for interprocess communication for Linux systems. D-Bus has a layered architecture. At the lowest level is the D-Bus specification, which specifies the D-Bus wire protocol for communication between two processes. The libdbus library is the low-level C API library based on the D-Bus specification.

In short, this will helps you interact with the Linux notification tray and do cool stuff.
** dbus actually serves much more purpose than what I mentioned above. I am trying to keep things slim here and focus only on the fun part. If you want to read more, try https://www.linux.com/topic/networking/d-bus-tutorial/

So, we are going to import dbus and then post a notification,

import dbus
bus = dbus.SessionBus()
notifications = bus.get_object(‘org.freedesktop.Notifications’, ‘/org/freedesktop/Notifications’)
interface = dbus.Interface(notifications, ‘org.freedesktop.Notifications’)
id = 4856
timeout = 2500
interface.Notify(‘name’,id,’’,’summary’,’body’,’’,’’,timeout)

Run this, and you will see a pop-up in the notification shade.

Done!
Feel free to modify the above code and have fun. Figure out use cases this might have for you. Go and be the coolest programmer!

Thank you

--

--

Ramit

Data Science & deep learning entusiast. Polyglot developer. Introvert. Minimalist.