88"""
99
1010# system imports
11- import os
1211import uuid
1312import logging
1413from xml .etree .ElementTree import Element , SubElement , tostring
2625)
2726from winsdk .windows .data .xml import dom
2827from winsdk .windows .applicationmodel .core import CoreApplication
29- from winsdk .windows .applicationmodel .background import (
30- BackgroundTaskRegistration ,
31- BackgroundTaskBuilder ,
32- BackgroundExecutionManager ,
33- BackgroundAccessStatus ,
34- ToastNotificationActionTrigger ,
35- )
3628from winsdk .windows .foundation import IPropertyValue , PropertyType
3729import winsdk ._winrt as _winrt
3830
@@ -63,8 +55,6 @@ class WinRTDesktopNotifier(DesktopNotifierBase):
6355
6456 :param app_name: The name of the app. This has no effect since the app name will be
6557 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.
6858 :param notification_limit: Maximum number of notifications to keep in the system's
6959 notification center.
7060 """
@@ -75,16 +65,13 @@ class WinRTDesktopNotifier(DesktopNotifierBase):
7565 Urgency .Critical : ToastNotificationPriority .HIGH ,
7666 }
7767
78- background_task_name = "DesktopNotifier-ToastBackgroundTask"
79-
8068 def __init__ (
8169 self ,
8270 app_name : str = "Python" ,
8371 notification_limit : Optional [int ] = None ,
8472 ) -> None :
8573 super ().__init__ (app_name , notification_limit )
8674 self .manager = ToastNotificationManager .get_default ()
87- self .notifier : ToastNotification | None = None
8875
8976 # Prefer using the real App ID if detected, fall back to user-provided name
9077 # and icon otherwise.
@@ -101,57 +88,13 @@ async def request_authorisation(self) -> bool:
10188
10289 :returns: Whether authorisation has been granted.
10390 """
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 )
10792
10893 async def has_authorisation (self ) -> bool :
10994 """
11095 Whether we have authorisation to send notifications.
11196 """
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 )
15598
15699 async def _send (
157100 self ,
0 commit comments