Desktop Notifications using Plyer

Jyotika Bhatti
GDSC, IIIT Allahabad
6 min readJan 3, 2021

I think, that everyone reading this article is already well acquainted with the concept of Notifications. Still, allow me to throw some light on them! 😄Notifications are tiny text pop-ups that appear on the desktop 💻 or mobile screens to inform the user about the updates or any other vital pieces of information, allowing the user to focus 💡on essential things and ignore the unimportant ones.

Photo by Sigmund on Unsplash

In this article, we will learn to develop an application that will create some cool notification pop-ups 🔔, reminding you of the daily tasks 🎯 or that make reminders 😍 for something you are usually prone to forget ✅

‘plyer’ is a really cool module available in Python which is essential for Desktop Notifications. Although there are some other modules essentially built for notifications like notify2, wintoast, etc., But personally ‘plyer’ is my favorite 😻

Photo by Shahadat Rahman on Unsplash

Let us know first all about ‘Plyer’ 🐼

It is basically a module 💡 that is used to mainly access the hardware features. Plyer is a cross-platform library that actually works on mobile too when used with Kivy. Plyer has way more than just notification tools. It can do screenshots, 🔉 text-to-speech, 💻 vibration, GPS, Bluetooth, audio, accelerometer, and more!

According to the Documentation at PyPI: 🐼

The Python Package Index (PyPI) is a repository of software for the Python 👻 programming language.

Plyer is a platform-independent API to use features commonly found on various platforms, notably mobile ☎️ ones, in Python.

For using Plyer, we need to first install it using pip command, which is a superman command 🏁 🏁 for installing various kinds of modules using command prompt.

Now, enough of Knowledge, Let us try to implement it practically, 😏

Photo by Swarnavo Chakrabarti on Unsplash

So, let us try to experiment ❤️ with something new & build up a small Water Drinking Notification Reminder Application 💟

Let us first dive into how does the plyer notifications works !! 😃

  • Python has a module named time to handle time-related tasks. We need to import this module to use its functions like timeout and sleep. 😴😴😴
  • Import the ‘plyer’ module to access the hardware💡 parts of your desktop and create desktop 💻notifications for the same.
  • The class ‘notification’ provides access to public methods to create notifications.
  • It has requirements of title and message that needs to get notified.

Also, when we need to display the notification on the screen for a particular time interval only, we need to set the timeout, that is the time for which you want the pop-up to be there on the screen. Similarly, you can make that sleep for a specified interval that comes back after the given amount of time.

This is now a basic implementation 💡💡of the module, in which we import the required modules ‘time’ and ‘plyer’.

Then according to the syntax of the class ‘notification,’ we will specify message, title and app-icon if any, along with time out and sleep time.

Here, if I would have kept the screen time(timeout) to be X seconds and sleep time as Y seconds, it means that after every Y seconds, the notification will appear on the screen for exactly X seconds and then reappear again.

This piece of code when run will remind you of Drinking water at regular intervals

Hold on 😌! I have a much more exciting thing 😄. By the end of this blog, you will also be able to create a notifier for yourself and show it off 😜

Now, let us take one step further ▶️, by creating a News Notifier which will make the use of the Concept of Web Scraping using ‘Beautiful Soup’ and Notifier using ‘Plyer’. We will learn how to create a ‘Desktop News Notifier’

This a really awesome 💙💙application for keeping yourself updated with daily news and headlines.

Photo by Taras Shypka on Unsplash

Here, while building a news notifier, we need to make use of additional modules in addition to ‘plyer’, which are beautiful soup and urllib to scrap and fetch the latest headlines data out of the given google news URL, Pretty Cool 💙❤️💙Right?

Let us know more about beautiful soup and urllib:

  • Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work😜.
  • urllib. request is a Python module for fetching URLs (Uniform Resource Locators), offering a very simple interface via a simple urlopen function. This is capable of fetching URLs using a variety of different protocols 😃.

Let us divide the whole application into three sections :

  • Opening the requested URL (using urllib)
  • Scraping the latest news and fetching the XML data (using beautiful soup)
  • Creating notifications (using plyer)

Before we go further, we need to first import the above-mentioned modules into our code section to do the tasks 💡with these libraries.

Now, further, we will make use of the urlopen function.

Declare the variable ‘url’, then call the urlopen function on the URL . Further XML Data gets read and stored in the temporary variable ‘xml_data’.

Running the XML document that was stored in ‘xml_data’ through Beautiful Soup gives a BeautifulSoup object, which represents the document as a nested data structure. We use the variable ‘news_list’💡 to store the parsed items with the tag ‘item’ and then we choose the first ten indices to get stored in that variable.

For the items that are stored in the ‘news_list’ variable, we will traverse through the list and call the notification.notify 💡function which is a part of the ‘plyer’ module. Assign the title and the timeout of the pop-up. Within the message, we can write the title of the news headline along with the publish date item that is present in the given URL storing XML format.

😛 Now, get ready for running it, using the process I mentioned below in the next portion of the blog, and see the notifications on your desktop.😜😜

This is the overall compiled code of the ‘News Notifier’:

How to run the notifiers in the background?

To run and check the above programs ❤️:

Just execute: main.py

But, To make this program always run in the background :

Use the following command given besides, and to remove the application reminding you, we can manually cancel the task at Task Manager in Windows.

That’s it for now guys. Will soon be coming back with some more amazing content. I would love ❤️to know your feedback. And if you have any queries, feel free to drop them in the comments section.

Hope you enjoyed reading it !! ❤️

--

--