Open In App

Desktop Notifier in Python

Last Updated : 13 Sep, 2023
Like Article

This article demonstrates how to create a simple Desktop Notifier application using Python.

A desktop notifier is a simple application which produces a notification message in form of a pop-up message on desktop.

Notification content

In the example we use in this article, the content that will appear as notification on desktop is the top news headlines of the day.

So, in order to fetch the top headlines, we will be using this Python script to scrape news headlines:

import requests
import xml.etree.ElementTree as ET
# url of news rss feed
def loadRSS():
    utility function to load RSS feed
    # create HTTP request response object
    resp = requests.get(RSS_FEED_URL)
    # return response content
    return resp.content
def parseXML(rss):
    utility function to parse XML format rss feed
    # create element tree root object
    root = ET.fromstring(rss)
    # create empty list for news items
    newsitems = []
    # iterate news items
    for item in root.findall('./channel/item'):
        news = {}
        # iterate child elements of item
        for child in item:
            # special checking for namespace object content:media
            if child.tag == '{}content':
                news['media'] = child.attrib['url']
                news[child.tag] = child.text.encode('utf8')
    # return news items list
    return newsitems
def topStories():
    main function to generate and return news items
    # load rss feed
    rss = loadRSS()
    # parse XML
    newsitems = parseXML(rss)
    return newsitems

It is a simple Python script which parses the news headlines available in XML format.

Note: To understand how XML parsing works, please refer this article: XML parsing in Python

A sample news item generated by above Python script looks like this:

{'description': 'Months after it was first reported, the feud between Dwayne Johnson and 
                 Vin Diesel continues to rage on, with a new report saying that the two are 
                 being kept apart during the promotions of The Fate of the Furious.',
 'link': '
 'media': '
 'pubDate': b'Sat, 01 Apr 2017 05:22:51 GMT ',
 'title': "Vin Diesel, Dwayne Johnson feud rages on; they're being deliberately kept apart"}

Save this Python script as (as we import it by this name in our desktop notifier app).


Now, in order to create a desktop notifier, you need to install a third party Python module, notify2.

You can install notify2 using a simple pip command:

pip install notify2

Desktop notifier app

Now, we write the Python script for our desktop notifier.

Consider the code below:

import time
import notify2
from topnews import topStories
# path to notification window icon
ICON_PATH = "put full path to icon image here"
# fetch news items
newsitems = topStories()
# initialise the d-bus connection
notify2.init("News Notifier")
# create Notification object
n = notify2.Notification(None, icon = ICON_PATH)
# set urgency level
# set timeout for a notification
for newsitem in newsitems:
    # update notification data for Notification object
    n.update(newsitem['title'], newsitem['description'])
    # show notification on screen
    # short delay between notifications

Let us try to analyze above code step by step:

  • Before we can send any notifications, we need to initialize a D-Bus connection. D-Bus is a message bus system, a simple way for applications to talk to one another. So, D-Bus connection for notify2 in current Python script is initialized using:
    notify2.init("News Notifier")

    Here, the only argument we passed is the app name. You can set any arbitrary app name.

  • Now, we create a notification object, n using:
    n = notify2.Notification(None, icon = ICON_PATH)

    The general syntax for above method is:

    notify2.Notification(summary, message='', icon='')


    • summary: The title text
    • message: The body text
    • icon: Path to an icon image

    Currently, we have set summary as None and passed the ICON_PATH as icon argument.

    Note: You need to pass complete path of the icon image.

  • You can optionally set the urgency level of notifications using set_urgency method:

    The available constants are:

    • notify2.URGENCY_LOW
    • notify2.URGENCY_NORMAL
    • notify2.URGENCY_CRITICAL
  • Another optional utility is set_timeout method using which, you can explicitly set the display duration in milliseconds as shown below:
  • Now, as we iterate through each news item one by one, we need to update notification object with a new summary and message using update method:
    n.update(newsitem['title'], newsitem['description'])
  • In order to display a notification, simply call show() method of Notification object like this:

A sample screen shot of desktop when you run above Python script:


Github repository for this desktop notifier application: Desktop-Notifier-Example

This blog is contributed by Nikhil Kumar.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads