python配置苹果推送服务器pyapns

这里总结下配置苹果推送服务器的过程:

1. 安装apn

 这里千万不要用pip安装 

pip install apns

错误如下:pypi 自动安装的包有问题,import的时候就会报错。

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from apns import APNs, Frame, Payload
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\projects\virtualenv\zhaota\lib\site-packages\apns.py", line 218
    except ssl.SSLError, err:

解决方法:

从github上下载源码: PyAPNs https://github.com/djacobs/PyAPNs

然后将解压文件夹中的apns.py放入你的项目中.

image.png

 

 

2. 接下来就可以使用了.

from utils.apns import APNs, Frame, Payload
from project.settings import apn_dev_cert_path,apn_dev_key_path
apn=APNs(use_sandbox=True, cert_file=apn_dev_cert_path, key_file=apn_dev_key_path)

3. 从上面使用可以看出,这里需要两个文件,一个是cert_file, 一个是key_file

这两个是从一个叫**.p12的文件产生,如何获取P12文件,可以参考这里

主要用到下面3个命令:

openssl pkcs12 -clcerts -nokeys -out dev-cert.pem -in dev.p12
openssl pkcs12 -nocerts -out dev-key.pem -in dev.p12
openssl rsa -in dev-key.pem -out dev-key-noec.pem

setting.py中:

1

2

apn_dev_cert_path = os.path.join(BASE_DIR, 'conf/apn/dev-cert.pem')

apn_dev_key_path = os.path.join(BASE_DIR, 'conf/apn/dev-key-noec.pem')

 

4. Test.py

apn=APNs(use_sandbox=True, cert_file=apn_dev_cert_path, key_file=apn_dev_key_path)
class APNpushManager():
    def __init__(self):
        self.apn=apn
 
    def sendWakeup(self,datas):
        token_hex=datas.get('token_hex')
        noti_msg=datas.get('noti_msg')
        payload = Payload(alert=noti_msg, sound=None, badge=1, mutable_content=True,content_available=True)
        self.apn.gateway_server.send_notification(token_hex, payload)
         
datas = {
    'token_hex': "****",
    'noti_msg':""
}
pusher=APNpushManager()
pusher.sendWakeup(datas)

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值