Skip to content

Instantly share code, notes, and snippets.

@tudormunteanu
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save tudormunteanu/2360db08ea2605e5ca9d to your computer and use it in GitHub Desktop.

Select an option

Save tudormunteanu/2360db08ea2605e5ca9d to your computer and use it in GitHub Desktop.
PyAPNs usage
from djacobs_apns.apns import APNs, Frame
from djacobs_apns.apns import APNs, Payload
class PayloadSafari(Payload):
"""
Overwrites the .dict() method with a new format,
for Safari Push Notifs
"""
def __init__(self, alert=None, url_args=[]):
super(Payload, self).__init__()
self.alert = alert
self.url_args = url_args
self._check_size()
def dict(self):
d = {"alert": self.alert, "url-args": self.url_args}
d = {'aps': d}
return d
# cert_path and key_path are correct
# push_message is my own object
apns = APNs(use_sandbox = False, cert_file = cert_path, key_file = key_path)
devices = Device.objects.filter(account_key = profile.account_key)
alert_dict = {"title": push_message.title, "body": push_message.body, "action": "View"}
for device in devices:
token = device.token
payload = PayloadSafari(alert = alert_dict, url_args = [str(push_message.id)])
try:
apns.gateway_server.send_notification(token, payload)
except Exception, e:
apns = APNs(use_sandbox=False, cert_file=cert_path, key_file=key_path)
apns.gateway_server.send_notification(token, payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment