Pyapns 提供了通用的Apple Push Notification Service (APNS)。該解決方案使用了開源的Twisted server,支持原生的Python和Ruby API。
功能:
XML-RPC Based, works with any client in any language
Native Python API with Django and Pylons support
Native Ruby API with Rails/Rack support
Scalable, fast and easy to distribute behind a proxy
Based on Twisted
Multi-application and dual environment support
Simplified feedback interface
pyapns項目github開源地址:https://github.com/samuraisam/pyapns
安裝(使用pip或easy_install安裝。會安裝
Twisted框架)
pip install pyapns
或者使用
easy_install pyapns
啟動twisted
#linux twistd -r epoll web --class=pyapns.server.APNSServer --port=7077 #mac os twistd -r kqueue web --class=pyapns.server.APNSServer --port=7077
新建push.py文件輸入下面內容
#!/usr/bin/python # -*- coding: utf-8 -* # Filename: push.py from pyapns import configure, provision, notify tokens = ["token1" , "token2"] notifications = [ {'aps' :{'alert': 'Hello token 1', 'badge': 0, 'sound': 'flynn.caf'}}, ] configure({'HOST': 'http://localhost:7077/'}) provision('myapp', open('developent.pem').read(), 'sandbox')
notify('myapp', tokens, notifications)
pyapns推送消息需要一個pem格式的證書文件,跟Parse里要求一個.p12文件不同啊。我看看怎么搞到這文件。從蘋果下載的證書是.cer格式的,在蘋果的KeyChain程序中,選擇導出,可是,只能選擇導出成.cer和.p12文件,.pem的選項被禁用了,好桑心。通過搜索export pem from keychain找到Creating .pem file for APNS?,簡單說,就是在KeyChain里導出.p12格式的,再通過下面的命令轉換成.pem的。搞定。
openssl pkcs12 -in Development.p12 -out developent.pem -nodes -clcerts
python push.py
推送成功
完成