1.证书及乱七八糟的配置
公钥:app id管理那儿的“Development Push SSL Certificate” push证书,我这儿下载下来叫"aps_developer.cer"
私钥:申请证书时候从钥匙串生成的"CertificateSigningRequest.certSigningRequest"文件在"钥匙串->密钥"那儿生成的与之前输入的名字相同的“专用密钥”,可以右键导出为***.p12文件
合成PEM证书
1)转换公钥
openssl x509 -in aps_developer.cer -inform der -out public.pem
2)转换私钥
openssl pkcs12 -nocerts -in MyPushChatKey.p12 -out private.pem
(这时候要输入密码的)
有了这两个pem文件其实就可以测试一下能否联通苹果的服务器了,网上有,就简写了
telnet gateway.sandbox.push.apple.com 2195 (测试是否能连通苹果的推送测试服务器)
Trying 17.172.232.226... Connected to gateway.sandbox.push-apple.com.akadns.net. Escape character is '^]'.
要是出现上面的结果就ok了,然后测试刚才的两个pem:
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert public.pem -key private.pem
输完密码之后,要是输出一堆提示信息就算是ok了
3)把两个证书合成(为了服务器用着方便)
cat public.pem private.pem > push_cert.pem
(这里先输入私钥密码,再输入合成之后的新密码,新密码得记住了,之后server用这个pem发推送的时候要用到)
保存好这些文件,证书这就算ok了。
2.用python写个简单的测试push server
from APNSWrapper import *
import binascii
def badge(wrapper, token):
message = APNSNotification()
message.token(token)
message.badge(3)
print message._build()
wrapper.append(message)
def sound(wrapper, token):
message = APNSNotification()
message.token(token)
message.sound("default")
print message._build()
wrapper.append(message)
def alert(wrapper, token):
message = APNSNotification()
message.token(token)
alert = APNSAlert()
alert.body("Very important alert message")
alert.loc_key("ALERTMSG")
alert.loc_args(["arg1", "arg2"])
alert.action_loc_key("OPEN")
message.badge(3)
message.sound("default")
message.alert(alert)
# properties wrapper
property = APNSProperty("acme", (1, "custom string argument"))
message.appendProperty(property)
print message._build()
wrapper.append(message)
def testAPNSWrapper():
encoded_token = binascii.unhexlify("02b0a0fa821278edb2674f711ca66da255b5d81feceb784ec125897f5e7f")
wrapper = APNSNotificationWrapper("push_cert.pem
",True)
alert(wrapper, encoded_token)
wrapper.notify()
if __name__ == "__main__":
testAPNSWrapper()