-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_notification.py
More file actions
99 lines (80 loc) · 2.98 KB
/
desktop_notification.py
File metadata and controls
99 lines (80 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# This file is part of the Python Projects repository, which is licensed under the
# Apache License, Version 2.0. You may obtain a copy of the license at
#
# http://www.apache.org/licenses/LICENSE-2.0
"""
Desktop Notification Program.
Input:
- No specific input required from the user.
Output:
- Displays desktop notifications with a title and message.
Features:
- Shows desktop notifications with customizable title and message.
- Allows customization of notification duration.
- Supports custom icons for notifications.
- Plays a notification sound when displayed.
- Provides options for persistent notifications and clickable notifications with specified URLs.
"""
from plyer import notification
import time
import os
import pygame
from pathlib import Path
def show_notification(title: str, message: str, icon_path: str, sound_path: str, duration: int = 3, persistent: bool = False, clickable: bool = False, url: str = '') -> None:
"""
Display a desktop notification with customizable options.
Args:
- title (str): Title for the notification.
- message (str): Message content for the notification.
- duration (int): Duration of the notification in seconds.
- icon_path (str): Path to a custom icon for the notification.
- sound_path (str): Path to a notification sound file.
- persistent (bool): Whether the notification should be persistent.
- clickable (bool): Whether the notification should be clickable.
- url (str): URL to open when the notification is clicked.
Returns:
- None
"""
notification_settings = {
'title': title,
'message': message,
'timeout': duration,
'app_name': 'YourAppName', # Customize with your app name
}
if icon_path:
notification_settings['app_icon'] = icon_path
try:
notification.notify(**notification_settings) # type:ignore
if sound_path:
try:
pygame.mixer.init()
pygame.mixer.music.load(sound_path)
pygame.mixer.music.play()
time.sleep(duration)
pygame.mixer.music.stop()
except Exception as e:
print(f"Error playing sound: {e}")
if persistent:
# Wait for the duration if not persistent
time.sleep(duration)
if clickable:
# Make the notification clickable (Windows-specific)
os.system(f'start {url}')
except Exception as e:
print(f"Error displaying notification: {e}")
if __name__ == '__main__':
title = "Hello from Python!"
message = "This is a desktop notification created using Python."
# Path to the icon and sound files
icon_path = str('../../assets/images/image/icon.ico')
sound_path = str('../../assets/audio/notifiaction_sound.mp3')
show_notification(
title,
message,
icon_path=icon_path,
sound_path=sound_path,
duration=5,
persistent=True,
clickable=True,
url=''
)