From 1e0daf038f08ba9442174be7b6902d23eea2519f Mon Sep 17 00:00:00 2001 From: 50-Course Date: Sun, 14 Dec 2025 23:06:15 +0100 Subject: [PATCH] Fix non-configurable timeout issue in apns_async --- push_notifications/apns_async.py | 12 ++++++++++-- push_notifications/conf/app.py | 4 ++++ push_notifications/conf/base.py | 3 +++ push_notifications/settings.py | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/push_notifications/apns_async.py b/push_notifications/apns_async.py index 811b0f90..5ca82980 100644 --- a/push_notifications/apns_async.py +++ b/push_notifications/apns_async.py @@ -305,6 +305,7 @@ def apns_send_bulk_message( mutable_content: Optional[bool] = False, category: Optional[str] = None, err_func: Optional[ErrFunc] = None, + timeout: Optional[int] = None, ) -> Dict[str, str]: """ Sends an APNS notification to one or more registration_ids. @@ -326,7 +327,11 @@ def apns_send_bulk_message( Notification Content Extension or UNNotificationCategory configuration. It allows the app to display custom actions with the notification. :param content_available: If True the `content-available` flag will be set to 1, allowing the app to be woken up in the background + :param timeout: Timeout in seconds for each notification send operation """ + if not timeout: + timeout = get_manager().get_apns_error_timeout(application_id) + try: topic = get_manager().get_apns_topic(application_id) results: Dict[str, str] = {} @@ -351,6 +356,7 @@ def apns_send_bulk_message( mutable_content=mutable_content, category=category, err_func=err_func, + timeout=timeout, ) ) @@ -386,6 +392,7 @@ def apns_send_bulk_message( async def _send_bulk_request( registration_ids: list[str], + timeout: int, alert: Union[str, Alert], application_id: Optional[str] = None, creds: Optional[Credentials] = None, @@ -432,16 +439,17 @@ async def _send_bulk_request( for registration_id in registration_ids ] - send_requests = [_send_request(client, request) for request in requests] + send_requests = [_send_request(client, request, timeout) for request in requests] return await asyncio.gather(*send_requests) async def _send_request( apns: APNs, request: NotificationRequest, + timeout: int, ) -> Tuple[str, NotificationResult]: try: - res = await asyncio.wait_for(apns.send_notification(request), timeout=1) + res = await asyncio.wait_for(apns.send_notification(request), timeout=timeout) return request.device_token, res except asyncio.TimeoutError: diff --git a/push_notifications/conf/app.py b/push_notifications/conf/app.py index 38bde083..444cc2b8 100644 --- a/push_notifications/conf/app.py +++ b/push_notifications/conf/app.py @@ -380,6 +380,10 @@ def get_apns_use_alternative_port( def get_apns_topic(self, application_id: Optional[str] = None) -> Optional[str]: return self._get_application_settings(application_id, "APNS", "TOPIC") + + def get_apns_error_timeout(self, application_id: Optional[str] = None) -> int: + return self._get_application_settings(application_id, "APNS", "ERROR_TIMEOUT") + def get_wns_package_security_id(self, application_id: Optional[str] = None) -> str: return self._get_application_settings( application_id, "WNS", "PACKAGE_SECURITY_ID" diff --git a/push_notifications/conf/base.py b/push_notifications/conf/base.py index 3e1c3b66..027943a0 100644 --- a/push_notifications/conf/base.py +++ b/push_notifications/conf/base.py @@ -22,6 +22,9 @@ def get_apns_use_sandbox(self, application_id: Optional[str] = None) -> bool: def get_apns_use_alternative_port(self, application_id: Optional[str] = None) -> bool: raise NotImplementedError + def get_apns_error_timeout(self, application_id: Optional[str] = None) -> int: + raise NotImplementedError + def get_wns_package_security_id(self, application_id: Optional[str] = None) -> str: raise NotImplementedError diff --git a/push_notifications/settings.py b/push_notifications/settings.py index 01cc30f0..fb2fbb22 100644 --- a/push_notifications/settings.py +++ b/push_notifications/settings.py @@ -18,6 +18,7 @@ PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_USE_SANDBOX", False) PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_USE_ALTERNATIVE_PORT", False) PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_TOPIC", None) +PUSH_NOTIFICATIONS_SETTINGS.setdefault("APNS_ERROR_TIMEOUT", 5) # WNS PUSH_NOTIFICATIONS_SETTINGS.setdefault("WNS_PACKAGE_SECURITY_ID", None)