Skip to content

Commit d7608b5

Browse files
committed
remove background task code from winrt backend
This is only required if we want to run a task without activating the app
1 parent 7cfd9c7 commit d7608b5

File tree

1 file changed

+2
-59
lines changed

1 file changed

+2
-59
lines changed

src/desktop_notifier/winrt.py

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
# system imports
11-
import os
1211
import uuid
1312
import logging
1413
from xml.etree.ElementTree import Element, SubElement, tostring
@@ -26,13 +25,6 @@
2625
)
2726
from winsdk.windows.data.xml import dom
2827
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-
)
3628
from winsdk.windows.foundation import IPropertyValue, PropertyType
3729
import 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

Comments
 (0)