Apple Push Notification Python Wrapper
Introduction
Apple Push Notification Wrapper is very simple tool to sending your notification to your apps on iPhone / iPod Touch devices. This document doesn't describe how to generate APNS certificate.
Installation
To install APNSWrapper use setuptools. Type in your terminal:
$ easy_install APNSWrapper
Basic Usage
To send your notification you should make few things:
- create APNSNotificatonWrapper (in sandbox or production mode)
- create an instance or few instances of APNSNotification
- set your notification type (badge, sound or alert)
- send notification
deviceToken = 'Qun\xaa\xd4R\x11zu\x07\x04\x9dG\xe6\x96j&\x95Y\x9d\x91~\xcc`z\n\x88O\xc0\x9c\xf6\xca'
# create wrapper
wrapper = APNSNotificationWrapper('iphone_cert.pem', True)
# create message
message = APNSNotification()
message.token(deviceToken)
message.badge(5)
# add message to tuple and send it to APNS server
wrapper.append(message)
wrapper.notify()
How to create the deviceToken?
That sample device token looks a bit cryptic. Can't we just use the plain text hex string version?
Perhaps we use binascii.unhexlify on the regular deviceToken string and that does the trick?
to get this to work I had to do the following changes:
under the class APNSNotification(object):
command = 0 ---> command = '\x00'
return struct.pack(apnsPackFormat, str(self.command), ---> return struct.pack(apnsPackFormat, self.command,
def testAPNSWrapper():
Greetings. I have a question about how to use the device token in the wrapper. The Wrapper appears to be running - I've made the .PEM file etc., configured my iPhone App to receive the token from APNS. I printed the token via NSLog in XCode. Here is a snippet from the Wrapper where I'm applying the token - I don't see any alert or badge on the iPhone. Any ideas? Your help is appreciated - I'm a newbie. X'd out the token for security.
def testAPNSWrapper(): """ Method to testing apns-wrapper module. """ encoded_token = '660c622exxxxxxcf68d8e89exxxxxxc2596xxxxxxcc87abxxxxxxbdaa7xxxxxx' wrapper = APNSNotificationWrapper('iphone_cert.pem', True) message = APNSNotification() message.tokenBase64(encoded_token) # message.token('660c622exxxxxxcf68d8e89exxxxxxc2596xxxxxxcc87abxxxxxxbdaa7xxxxxx') message.badge(6) message.sound() alert = APNSAlert() alert.body("My specific message.") alert.loc_key("ALERTMSG") alert.loc_args(["arg1", "arg2"]) alert.action_loc_key("OPEN") message.alert(alert) # properties wrapper property = APNSProperty("acme", (1, "custom string argument")) message.appendProperty(property) wrapper.append(message) print message.build() wrapper.notify()
I have installed this APNSWrapper package, but when I run the sample code it will print a message like below "NameError?: name 'APNSNotificationWrapper' is not defined". Anyone can help !!
Hey romain, try to upgrade APNSWrapper (easy_install --upgrade APNSWrapper). New version should work
It's not totally clear from the code here how to get the deviceToken. It's not just your phone's identifier, but rather is a different unique number assigned to your phone. To figure it out, you need to register for notifications in your iPhone Application by following the instructions here:
http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
Then in your didRegisterForRemoteNotificationsWithDeviceToken, do something like:
- (void)application:(UIApplication )app didRegisterForRemoteNotificationsWithDeviceToken:(NSData )devToken {
}Run this on your device in XCode, and copy the printed ID from the debug console. Convert it to a hex string by doing something like:
python >>> import binascii; binascii.unhexlify('ID')
Where 'ID' is the string printed out on the console above. Finally, paste the output of these python commands into the python sample code at the top of this page.
Obviously in a real implementation you would send this string to the server part of your application in didRegisterForRemoteNotificationsWithDeviceToken, which you could then use to know which applications are available to notify.
Hello! I have a problem, the message it has been sent
but iphone the message does not receive.
How i can convert Apple Push certificate (.cer) to "pem" format? Thanks.
Check out this link: http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/
Steps 8, 9 and 10 explain how to convert and it's also a nice tutorial.
can you pleas extend the sample to multiple messages to different devices?
I think that the simplest way is this:
#!/usr/bin/env python from APNSWrapper import * import binascii deviceToken = binascii.unhexlify('12f53d1bf554d0a01bd7c4f233a668e5878d99f229a76338fd7477f7f381c371'); wrapper = APNSNotificationWrapper('ck.pem', True) message = APNSNotification() message.token(deviceToken) message.alert("Very simple alert") message.badge(5) message.sound() wrapper.append(message) wrapper.notify()Thanks for your work.
Hi it is god for me, thank you very much
http://www.desarrollodigital.com.mx
Fast and easy for development testing! Thanks for the code :)
Hi,
everything works for me BUT : i don't receive anything on my device.
How can we read the result code of the APNS server ? notify() only return True or False if the writing on the socket has been successful. Is there a way to get the server response?
is there any logger available?
@guilla and others, sadly there is no way of knowing the response of an APNS push notification. Look at it as a black box. You submit a notification to APNS, and most of the time, it'll just work. If it doesn't, good luck debugging the problem.
hi such mistakes:Traceback (most recent call last):
ssl.SSLError: 1? ssl.c:480: error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknownmy python is 2.6 and warpper.notify always go wrong.
God help me...
following is sources #!/usr/bin/env python from APNSWrapper import import binascii import socket,ssl
deviceToken = binascii.unhexlify('6f8xx994d09f17ffa1xx638e5b1fc8a7cfxxe189f44851f2xx534f138xx93fcb'); wrapper = APNSNotificationWrapper('ck.pem', True)
message = APNSNotification() message.token(deviceToken) message.badge(5)
# add message to tuple and send it to APNS server wrapper.append(message) wrapper.notify()
Need help I receive an exception: ssl.SSLError: 336265225? ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey?_file:PEM lib