0

I am trying to attach images in iOS push notifications using PyAPNS. How can I add this attachment into the apns payload structure?

We can send the notification with title, subtitle, and body.

class ApplePushNotification(object):
    """
    class to handle apple push notifications
    """


    def __init__(self, account, user_id_token_mappings, project='testproject'):
        '''
        constructor to initialize account and device info
        :param account: account id
        :param user_id_token_mappings: user id - device token mapping (dict)
        '''
        self.account = account
        self.user_id_token_mappings = user_id_token_mappings
        self.cert_file = ''
        self.key_file = self.cert_file

    def send_apple_push_notification(self, message, alert=None, extra_payload={}):
        '''
        sends push notification to iOS device
        :param message: push notification content
        :param alert: alert text
        :param extra_payload: extra notification content
        :return: None
        '''
        if not self.user_id_token_mappings:
            return False
        apns_server = Apns(use_sandbox=USE_SANDBOX, cert_file=self.cert_file, key_file=self.key_file,
                                  enhanced=True)
        apns_server.gateway_server.register_response_listener(response_listener, self.account)
        payload_content = {'sound': "default", 'content_available': 1, 'custom': message}
        if alert:
            payload_content['alert'] = {
                'title': title,
                'subtitle': '',
                'body': alert
            }
        if extra_payload:
            payload_content.update(extra_payload)
        payload = Payload(**payload_content)
        expiry = int(time.time() + 3600)
        invalid_tokens = []
        for i, userid_apns_mapping in enumerate(self.user_id_token_mappings):
            identifier = get_identifier()
            try:
                apns_server.gateway_server.send_notification(userid_apns_mapping[1], payload, identifier=identifier,
                                                             expiry=expiry)
                apns_server.gateway_server.set_identifier_token_mapping(identifier, userid_apns_mapping)
            except TypeError:
                # s = traceback.format_exc()
                # write_error_log(s)
                invalid_tokens.append(userid_apns_mapping)
                print('type error')
                continue

So the APNS class is overridden here and we are sending the payload as it is needed. But can't send attachment in this payload structure and it is throwing error.

Sreehari S
  • 106
  • 1
  • 7

0 Answers0