8
8
"""
9
9
10
10
# system imports
11
- import os
12
11
import uuid
13
12
import logging
14
13
from xml .etree .ElementTree import Element , SubElement , tostring
26
25
)
27
26
from winsdk .windows .data .xml import dom
28
27
from winsdk .windows .applicationmodel .core import CoreApplication
29
- from winsdk .windows .applicationmodel .background import (
30
- BackgroundTaskRegistration ,
31
- BackgroundTaskBuilder ,
32
- BackgroundExecutionManager ,
33
- BackgroundAccessStatus ,
34
- ToastNotificationActionTrigger ,
35
- )
36
28
from winsdk .windows .foundation import IPropertyValue , PropertyType
37
29
import winsdk ._winrt as _winrt
38
30
@@ -63,8 +55,6 @@ class WinRTDesktopNotifier(DesktopNotifierBase):
63
55
64
56
:param app_name: The name of the app. This has no effect since the app name will be
65
57
automatically determined.
66
- :param app_icon: The default icon to use for notifications. This has no effect since
67
- the app icon will be automatically determined.
68
58
:param notification_limit: Maximum number of notifications to keep in the system's
69
59
notification center.
70
60
"""
@@ -75,16 +65,13 @@ class WinRTDesktopNotifier(DesktopNotifierBase):
75
65
Urgency .Critical : ToastNotificationPriority .HIGH ,
76
66
}
77
67
78
- background_task_name = "DesktopNotifier-ToastBackgroundTask"
79
-
80
68
def __init__ (
81
69
self ,
82
70
app_name : str = "Python" ,
83
71
notification_limit : Optional [int ] = None ,
84
72
) -> None :
85
73
super ().__init__ (app_name , notification_limit )
86
74
self .manager = ToastNotificationManager .get_default ()
87
- self .notifier : ToastNotification | None = None
88
75
89
76
# Prefer using the real App ID if detected, fall back to user-provided name
90
77
# and icon otherwise.
@@ -101,57 +88,13 @@ async def request_authorisation(self) -> bool:
101
88
102
89
:returns: Whether authorisation has been granted.
103
90
"""
104
- if not await self ._request_background_task_access ():
105
- return False
106
- return await self .has_authorisation ()
91
+ return bool (self .notifier .setting == NotificationSetting .ENABLED )
107
92
108
93
async def has_authorisation (self ) -> bool :
109
94
"""
110
95
Whether we have authorisation to send notifications.
111
96
"""
112
- if not self .notifier :
113
- return False
114
- return (
115
- self .notifier .setting == NotificationSetting .ENABLED
116
- and await self ._has_background_task_access ()
117
- )
118
-
119
- async def _has_background_task_access (self ) -> bool :
120
- try :
121
- res = await BackgroundExecutionManager .request_access_async (self .app_id )
122
- except OSError :
123
- return False
124
- else :
125
- return res not in {
126
- BackgroundAccessStatus .DENIED_BY_SYSTEM_POLICY ,
127
- BackgroundAccessStatus .DENIED_BY_USER ,
128
- }
129
-
130
- def _has_registered_background_task (self ) -> bool :
131
- tasks = BackgroundTaskRegistration .get_all_tasks ()
132
- return any (t .name == self .background_task_name for t in tasks .values ())
133
-
134
- async def _request_background_task_access (self ) -> bool :
135
- """Request permission to activate in the background."""
136
- if not self .notifier :
137
- return False
138
-
139
- if not await self ._has_background_task_access ():
140
- logger .warning ("This app is not allowed to run background tasks." )
141
- return False
142
-
143
- # If background task is already registered, do nothing.
144
- if self ._has_registered_background_task ():
145
- return True
146
-
147
- # Create the background tasks.
148
- builder = BackgroundTaskBuilder ()
149
- builder .name = self .background_task_name
150
-
151
- builder .set_trigger (ToastNotificationActionTrigger ())
152
- builder .register ()
153
-
154
- return True
97
+ return bool (self .notifier .setting == NotificationSetting .ENABLED )
155
98
156
99
async def _send (
157
100
self ,
0 commit comments