Pr0Ger / PyAPNs2

Python library for interacting with the Apple Push Notification service (APNs) via HTTP/2 protocol
MIT License
349 stars 179 forks source link

custom key #45

Closed LarkinBTCC closed 7 years ago

LarkinBTCC commented 7 years ago

how can i add custom keys to payload?

Pr0Ger commented 7 years ago

You can pass dict with your custom keys as a custom parameter for Payload class https://github.com/Pr0Ger/PyAPNs2/blob/master/apns2/payload.py#L48-L50

Dubrzr commented 7 years ago

What if we want to put custom keys in result['aps'] sub-dictionary? You could replace all parameters (except custom) by **kwargs, and then for each named argument of kwargs you add it in result['aps'], this would allow anyone to add its customs needed parameters.

def __init__(self, alert=None, badge=None, sound=None, content_available=False, mutable_content=False, category=None, url_args=None, custom=None, thread_id=None):

becomes:

def __init__(self, custom=None, **kwargs):
  self.custom = custom
  for k, v in kwargs.items():
    setattr(self, k, v)

def dict(self):
  result = {
    'aps': {k, v in self.__dict__.items() if k != 'custom'}
  }
  result.update(self.custom)
  return result

Easy ;)

Pr0Ger commented 7 years ago

You shouldn't add custom keys to aps sub-dictionary since it's reserved for Apple usage. About replacing parameters with **kwargs I dislike this approach because autocompletion doesn't play well with it, also explicit args can replace documentation in some way

Dubrzr commented 7 years ago

So in the case I'm forced to add custom keys to aps sub-dictionary (let's say I work at Apple), I cannot with your library unless I create my own CustomPayload class... smart...