Desktop Notifier by Python | Wenhan Code life

Desktop Notifier by Python

This article show how to send desktop notice using Python

simpleNotification

Install requirments

we need to install notify2 by pip

1
2
3
4
5
# pip install notify2
Collecting notify2
Downloading notify2-0.3.1-py2.py3-none-any.whl
Installing collected packages: notify2
Successfully installed notify2-0.3.1

Coding

First we need to import notify2

1
import notify2

Then need to initialise the d-bus connection.
D-Bus is a message bus system, a simple way for applications to talk to one another.

1
2
# initialise the d-bus connection
notify2.init("hello")

Next we need to create a Notification object.
The simplest way is

1
n = notify2.Notification(None)

else, you can add a icon to the notification.

1
n = notify2.Notification(None, icon = "/home/wenshi/Pictures/me.jpg")

next, set the urgency level for the notification.

1
n.set_urgency(notify2.URGENCY_NORMAL)

the other available options are

1
2
3
notify2.URGENCY_LOW
notify2.URGENCY_NORMAL
notify2.URGENCY_CRITICAL

next, you can decide how long will the notification will display.
use set_timeout to set the time in milliseconds.

1
n.set_timeout(5000)

next, file the title and message body of the notification

1
n.update("hello title", "hello messages")

notification will be shown on screen by show method.

1
n.show()

Try it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import notify2

# initialise the d-bus connection
notify2.init("hello")

# create Notification object
n = notify2.Notification(None, icon = "/path/to/your/image")

# set urgency level
n.set_urgency(notify2.URGENCY_NORMAL)

# set timeout for a notification
n.set_timeout(5000)

# update notification data for Notification object
n.update("hello title", "hello messages")

# show notification on screen
n.show()

and it will show up in your screen
simpleNotification