-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwindows.py
51 lines (41 loc) · 2 KB
/
windows.py
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
from winsdk.windows.ui.notifications.management import UserNotificationListener, UserNotificationListenerAccessStatus
from winsdk.windows.ui.notifications import NotificationKinds, UserNotification
from winsdk.windows.foundation.metadata import ApiInformation
import time
class Windows:
def __init__(self):
self.seen_ids = set()
self.listeners = []
async def can_read_notifications(self) -> bool:
if not ApiInformation.is_type_present("Windows.UI.Notifications.Management.UserNotificationListener"):
print("UserNotificationListener is not supported on this device.")
return False
listener = UserNotificationListener.get_current()
accessStatus = await listener.request_access_async()
if accessStatus != UserNotificationListenerAccessStatus.ALLOWED:
print("Access to UserNotificationListener is not allowed.")
return False
return True
async def run(self) -> None:
# Call here before loop in order to fill seen_ids
await self.get_new_notifications()
while True:
notifications = await self.get_new_notifications()
for notification in notifications:
self.dispatch(notification)
time.sleep(.3)
async def get_new_notifications(self) -> list[UserNotification]:
listener = UserNotificationListener.get_current()
notifications = await listener.get_notifications_async(NotificationKinds.TOAST)
new_notifications = []
for notification in notifications:
if hasattr(notification, "id"):
if not notification.id in self.seen_ids:
new_notifications.append(notification)
self.seen_ids.add(notification.id)
return new_notifications
def dispatch(self, notification) -> None:
for listener in self.listeners:
listener(notification)
def add_notification_listener(self, listener) -> None:
self.listeners.append(listener)