djacobs / PyAPNs

Python library for interacting with the Apple Push Notification service (APNs)
http://pypi.python.org/pypi/apns/
MIT License
1.22k stars 374 forks source link

Send multiple notifications stopped working on AppEngine #134

Closed dexity closed 8 years ago

dexity commented 9 years ago

About 3 month ago I created development keys for APNs and they worked fine. The notifications were being sent as expected. Recently I created new development and production keys and they stopped working on AppEngine but it still sends messages fine from local script.

Here is how I created the keys:

$ openssl x509 -in aps_production.cer -inform der -out Cert.pem
$ openssl pkcs12 -nocerts -out Key2.pem -in Certificates.p12
$ openssl rsa -in Key2.pem -out Key.pem  # Remove password

It properly connects to production APNs server:

$ openssl s_client -connect gateway.push.apple.com:2195 -cert Cert.pem -key Key.pem

Local script also properly sends messages:

# push.py
import apns

TOKEN_HEX = '24b3703df600427c9444a26e785a4f6642780d0f444ea6e6c279531a748a5511'

app = apns.APNs(use_sandbox=False, cert_file='Cert.pem', key_file='Key.pem', enhanced=True)
# Send a notification
payload = apns.Payload(alert='Hello', sound='default', badge=1)
payload2 = apns.Payload(alert='Hello 2', sound='default', badge=1)
frame = apns.Frame()
frame.add_item(TOKEN_HEX, payload, 1, 3600, 10)
frame.add_item(TOKEN_HEX, payload2, 1, 3600, 10)
app.gateway_server.send_notification_multiple(frame)

I ended up using a single send_notification which works fine on AppEngine but is not efficient as it creates a new connection for every device notification.

import apns
# devices - list of device objects
# kwargs - payload keyword arguments

payload = apns.Payload(**kwargs)
for device in devices:
    apns_socket = apns.APNs(use_sandbox=False, cert_file='Cert.pem', key_file='Key.pem', enhanced=True)
    apns_socket.gateway_server.send_notification(device.device_key, payload)

I can't think of any AppEngine restrictions except may be asynchronous io. I checked the code for apns module and it looks ok. Has anyone experienced the problem?

jayd3e commented 8 years ago

I'm also having this issue. I'm unable to send the same payload to multiple device tokens.

kaansoral commented 8 years ago

I just discovered Frame myself, it turns out I was sending each notification one by one all this time

Anyway, #149 fixed the Frame issue for me, haven't tested it on production yet tho