Desktop Notifier Project (code) in Python

← Back to Projects

Desktop Notifier in Python.

About the project: This is a Desktop Notifier project in Python. This will be a standalone script that sends custom notifications to your desktop.

For this project, we'll use the plyer library, which provides a cross-platform way to access features like notifications. This will work on Windows, macOS, and Linux.

Some operating systems may require additional dependencies. For example, on Windows, win10toast might be used, and on Linux, libnotify is a common dependency.

How to use this program:

To use this Desktop Notifier, you'll first need to install the plyer library. Open your terminal or command prompt and run:


    pip install plyer
    

Once installed, you can run the Python script:

  • Run the Python script.
  • The program will ask you to enter a notification title and a notification message.
  • The program will then send the notification to your desktop.
  • You can keep creating and sending new notifications until you type q to quit at any prompt.

Project Level: Intermediate

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# desktop_notifier.py

from plyer import notification
import time

def show_notification(title, message, timeout=10):
    """
    Displays a desktop notification.

    Args:
        title (str): The title of the notification.
        message (str): The main message content of the notification.
        timeout (int): The duration in seconds that the notification will stay on screen.
                       (Some platforms may ignore this).
    """
    try:
        notification.notify(
            title=title,
            message=message,
            app_name='Python Notifier', # Optional, sets the application name
            timeout=timeout             # Optional, duration on screen
        )
        print(f"Notification sent: '{title}' - '{message}'")
    except Exception as e:
        print(f"Error sending notification: {e}")
        print("Please check your installation and platform compatibility.")

def main():
    """
    Main function to run the Desktop Notifier app.
    It will send a series of notifications based on user input.
    """
    print("--- Python Desktop Notifier ---")
    print("Enter 'q' at any prompt to quit.")

    while True:
        notification_title = input("\nEnter notification title: ").strip()
        if notification_title.lower() == 'q':
            print("Exiting Notifier. Goodbye!")
            break

        notification_message = input("Enter notification message: ").strip()
        if notification_message.lower() == 'q':
            print("Exiting Notifier. Goodbye!")
            break

        if not notification_title or not notification_message:
            print("Title and message cannot be empty.")
            continue

        show_notification(notification_title, notification_message)
        
        # Add a delay to prevent too many notifications from being sent at once
        time.sleep(2) 

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()