3

I have been looking everywhere for example code on how to do this from a python written server over GAE - but with no luck.

Can someone please help me with the function to do this? (It should be pretty straight forward I believe).

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Lior Z
  • 668
  • 1
  • 9
  • 21
  • Have you looked at any of the python APNS libraries and tried using them? – Paulw11 Apr 12 '14 at 01:07
  • Well, I know GAE has a problem with sockets, and they require opening it. I was wondering if someone has any experience and can simply direct me to what is best to use. – Lior Z Apr 12 '14 at 09:09
  • There used to a restriction on opening outbound sockets from the GAE sandbox which meant that you could only send push notifications through services like urban airship or parse. Outbound sockets are now a "preview" feature - https://developers.google.com/appengine/features/ so it may not stay forever, but it is available – Paulw11 Apr 12 '14 at 09:42

2 Answers2

1

I use this library, and it works well in my app.
https://github.com/simonwhitaker/PyAPNs

enable ssl in app.yaml

libraries:
- name: ssl
  version: latest

code is like below, token_hex == a push notification token sent from a device. And you have to some variables.

from apns import APNs, Payload
apns = APNs(use_sandbox=use_sandbox, 
        cert_file=path/to/cert.pem',
        key_file=path/to/key-noenc.pem')
payload = Payload(alert='hello', sound="default", badge=1,custom={})
apns.gateway_server.send_notification(token_hex, payload)
for (token_hex, fail_time) in apns.feedback_server.items():
    logging.info(token_hex) 
    logging.info(fail_time)
yosuke
  • 157
  • 6
  • I get this error when using this lib: apns.py", line 155, in _connect self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 387, in wrap_socket ciphers=ciphers) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 141, in __init__ ciphers) TypeError: must be _socket.socket, not socket – Lior Z Apr 16 '14 at 18:34
  • you have to enable ssl library in app.yaml – yosuke Apr 16 '14 at 22:54
  • @LiorZ you also have to apply this: http://stackoverflow.com/questions/16192916/importerror-no-module-named-ssl-with-dev-appserver-py-from-google-app-engine/16937668#16937668 – Kaan Soral Jun 10 '14 at 22:09
0

Maybe you may consider this forked version of PyAPNS that has enhanced message support.
https://github.com/jimhorng/PyAPNs
which means it will catch error response for failure messages and resent the message which are discarded by APNS while sending between failure messages and receiving error response.

Solution:

  • Non-blocking ssl socket connection to send notification without waiting for response.
  • A separate thread for constantly checking error-response from read connection.
  • A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns. (Reference to non-blocking apns pull request by minorblend, enhanced message by hagino3000)

Result:

  • Send notification at throughput of 1000/secs
  • In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one.
Jim Horng
  • 1,587
  • 1
  • 13
  • 23