How to implement a Python desktop notifier using the plyer module

This shot explores how to create a desktop notifier application in Python with the plyer module. We will create a Remainder application that will remind us to take a break while doing work.

Implementation

  1. Install the plyer module with pip.


    pip install plyer
    

  2. plyer comes with a class called notification, which helps us create a notification. Import it like this:


    from plyer import notification
    
  1. The notification class provides a method called notify, which accepts the following parameters:

    • title (str): Displays the title of the notification.

    • message (str): Accepts message description of the notification.

    • app_icon (str): Displays the icon for the notification.

    • timeout (int): Provides how much time in seconds the notification is to be displayed.

    • ticker (str): It will display the text provided in the status bar when a notification arrives.

    • toast (bool): This will display a toast message in Android instead of a full notification.

Some of the parameters won’t work on desktop. For example, toast and ticker can only be used on Android devices.

Code

In the following code snippet:

  • Line 2: We import the notification class from the plyer module.

  • Line 4: We import time, to make this program temporarily sleep to create a time gap between notifications.

  • Line 7: Use a while loop to create notification indefinitely until you stop the program manually.

  • Line 9: We call the notify method and pass the necessary parameters as shown below.

  • Line 15: After displaying a notification, we will make it sleep for 1 hour or 60 minutes. You can choose a different interval.

The notification shows for every 60 minutes.

#import notification from plyer module
from plyer import notification
#import time
import time
#Use while loop to create notifications indefinetly
while(True):
#notification
notification.notify(
title = "Reminder to take a break",
message = '''Drink water, take a walk''',
timeout = 60
)
#System pause the execution of this programm for 60 minutes
time.sleep(60*60)

Output

widget