My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
APNSWrapperOverview  
This page describes basic usage of APNSWrapper
Phase-Implementation, Phase-Design
Updated Jul 16, 2009 by klymys...@gmail.com

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()
Comment by dujun...@gmail.com, Aug 18, 2009

How to create the deviceToken?

Comment by jdand...@gmail.com, Sep 26, 2009

That sample device token looks a bit cryptic. Can't we just use the plain text hex string version?

Comment by jdand...@gmail.com, Sep 26, 2009

Perhaps we use binascii.unhexlify on the regular deviceToken string and that does the trick?

Comment by vip...@gmail.com, Oct 7, 2009

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():

deviceToken = binascii.unhexlify('hex-value-from-device-without-spaces')

Comment by iphonep...@gmail.com, Nov 4, 2009

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()

Comment by romain.c...@gmail.com, Nov 16, 2009

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 !!

Comment by project member klymys...@gmail.com, Nov 20, 2009

Hey romain, try to upgrade APNSWrapper (easy_install --upgrade APNSWrapper). New version should work

Comment by sam.madd...@gmail.com, Dec 25, 2009

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 {

const unsigned char devTokenBytes = bytes?; printf("ID IS: "); for (int i = 0; i < length?; i++)
printf("%02X", devTokenBytesi?);
printf("\n");
}

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.

Comment by ya.nez...@gmail.com, Jan 15, 2010

Hello! I have a problem, the message it has been sent

>>> wrapper.notify()
True

but iphone the message does not receive.

Comment by 2der...@gmail.com, Jan 29, 2010

How i can convert Apple Push certificate (.cer) to "pem" format? Thanks.

Comment by john.gil...@gmail.com, Feb 6, 2010

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.

Comment by henrik.genssen@gmail.com, Mar 17, 2010

can you pleas extend the sample to multiple messages to different devices?

Comment by programm...@gmail.com, Apr 21, 2011

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.

Comment by luis.agu...@gmail.com, Jul 5, 2011

Hi it is god for me, thank you very much

http://www.desarrollodigital.com.mx

Comment by mike.toy...@gmail.com, Jul 23, 2011

Fast and easy for development testing! Thanks for the code :)

Comment by guilla...@haploid.fr, Oct 17, 2011

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?

Comment by rvinothk...@gmail.com, Nov 4, 2011

is there any logger available?

Comment by tra...@stamped.com, Nov 14, 2011

@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.

Comment by 693576...@qq.com, Nov 15, 2011

hi such mistakes:Traceback (most recent call last):

File "apns.py", line 16, in <module>
wrapper.notify()
File "/Library/Python/2.6/site-packages/APNSWrapper-0.6.1-py2.6.egg/APNSWrapper/notifications.py", line 194, in notify File "/Library/Python/2.6/site-packages/APNSWrapper-0.6.1-py2.6.egg/APNSWrapper/connection.py", line 215, in connect File "/Library/Python/2.6/site-packages/APNSWrapper-0.6.1-py2.6.egg/APNSWrapper/connection.py", line 161, in connect File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ssl.py", line 309, in connect
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ssl.py", line 293, in do_handshake
self.sslobj.do_handshake()
ssl.SSLError: 1? ssl.c:480: error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown

my 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()

Comment by keren.me...@gmail.com, Jan 14, 2012

Need help I receive an exception: ssl.SSLError: 336265225? ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey?_file:PEM lib


Sign in to add a comment
Powered by Google Project Hosting