Infer push notification type from topic

Close #87
This commit is contained in:
Sergey Petrov
2019-10-03 00:45:49 +03:00
parent b3d9227740
commit 56d8bf2a88
3 changed files with 32 additions and 30 deletions
+32 -7
View File
@@ -20,6 +20,15 @@ class NotificationPriority(Enum):
Delayed = '5'
class NotificationType(Enum):
Alert = 'alert'
Background = 'background'
VoIP = 'voip'
Complication = 'complication'
FileProvider = 'fileprovider'
MDM = 'mdm'
RequestStream = collections.namedtuple('RequestStream', ['stream_id', 'token'])
Notification = collections.namedtuple('Notification', ['token', 'payload'])
@@ -92,16 +101,32 @@ class APNsClient(object):
def send_notification_async(self, token_hex: str, notification: Payload, topic: Optional[str] = None,
priority: NotificationPriority = NotificationPriority.Immediate,
expiration: Optional[int] = None, collapse_id: Optional[str] = None) -> int:
expiration: Optional[int] = None, collapse_id: Optional[str] = None,
push_type: Optional[NotificationType] = None) -> int:
json_str = json.dumps(notification.dict(), cls=self.__json_encoder, ensure_ascii=False, separators=(',', ':'))
json_payload = json_str.encode('utf-8')
headers = {
'apns-push-type': notification.push_type
}
headers = {}
inferred_push_type = None # type: Optional[str]
if topic is not None:
headers['apns-topic'] = topic
if topic.endswith('.voip'):
inferred_push_type = NotificationType.VoIP.value
elif topic.endswith('.complication'):
inferred_push_type = NotificationType.Complication.value
elif topic.endswith('.pushkit.fileprovider'):
inferred_push_type = NotificationType.FileProvider.value
elif any([notification.alert, notification.badge, notification.sound]):
inferred_push_type = NotificationType.Alert.value
else:
inferred_push_type = NotificationType.Background.value
if push_type:
inferred_push_type = push_type.value
if inferred_push_type:
headers['apns-push-type'] = inferred_push_type
if priority != DEFAULT_APNS_PRIORITY:
headers['apns-priority'] = priority.value
@@ -138,8 +163,8 @@ class APNsClient(object):
def send_notification_batch(self, notifications: Iterable[Notification], topic: Optional[str] = None,
priority: NotificationPriority = NotificationPriority.Immediate,
expiration: Optional[int] = None, collapse_id: Optional[str] = None
) -> Dict[str, Union[str, Tuple[str, str]]]:
expiration: Optional[int] = None, collapse_id: Optional[str] = None,
push_type: Optional[NotificationType] = None) -> Dict[str, Union[str, Tuple[str, str]]]:
"""
Send a notification to a list of tokens in batch. Instead of sending a synchronous request
for each token, send multiple requests concurrently. This is done on the same connection,
@@ -171,7 +196,7 @@ class APNsClient(object):
if next_notification is not None and len(open_streams) < self.__max_concurrent_streams:
logger.info('Sending to token %s', next_notification.token)
stream_id = self.send_notification_async(next_notification.token, next_notification.payload, topic,
priority, expiration, collapse_id)
priority, expiration, collapse_id, push_type)
open_streams.append(RequestStream(stream_id, next_notification.token))
next_notification = next(notification_iterator, None)