参考文档:
APNS tutorial: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
之前写的apns笔记:http://blog.csdn.net/totogogo/article/details/7376214
注意:在xcode里开发带notification功能的app时,完全不需要有APNS push certificate。只有用于send push notification的server side 才需要这个cert!!
前提条件:在keychain tool里,有你的iOS developer key (至少有private key),以及对应的cert (该cert可以来自online dev center,也可能来自admin给developer的p12 file)
1. 如果没有publickey,通过这条link来生成。必须生成,否则步骤2会出错。
2. inkeychain tool > login > keys, right click the iOS developer private key,select “request a certificate from a certificate authority with XXXX key”,input your apple account email in “User email address”, leave “CA email address”empty,select “Saved to disk” option, click Continue. it will generate a .certSigningRequest file。该file用于重新生成该app的pushnotification cert!
Note: itis recommended you use the same email address that you used to sign up for theiOS Developer Program, but it seems to accept any email address just fine
3. logindev member center (https://developer.apple.com/membercenter/), access “Certificates,Identifiers & Profiles” page, select “Identifiers > App IDs”, highlightthe app you want to renew/reset push cert, click “Edit”
4. click“Revoke”button if there is existing SSL Certificate in “push notification”section. 这时你access “Certificates > Development”page,你会看到之前存在的APNs push cert已经被删除!
5.access the “app ID”edit page again, find push notification” section, tick thecheckbox, and then click “Create Certificate”button, 把步骤2生成的.certSigningRequestfile upload,这样就会生成一个APNs cert,该cert会绑定到该app ID上。同时你在“Certificates> Development”page里也可以看到这个cert item,该cert是用于Yourserver only send push notifications to that particular app, not to any otherapps。download this cert to local (aps_development.cerfile)。aps cert for development只有三个月有效期,而apscert for distribution有一年有效期,你需要在到期之前进行renew。
然后double click下载的push SSL cert “aps_development.cer”fileto import it to keychain。如果你用c#来send notification,那么把push SSL cert导入keychain是必要的,因为后面在生成pushSSL p12 file for c#的步骤中要在keychain tool里对其进行操作!如果你是使用php,那么就不必导入“aps_development.cer”file
Note:
* 当你新建了push SSL cert之后,需要在Xcode里updateprovisioning profile并clean and recompile your project!!
6.export private key from keychain as a p12 file. in keychain tool > login> keys, right click the iOS developer private key, select “Export”,导出过程中需要你为生成的文件设置一个密码,假设为“123456”
7. 这时我们生成了三个files
* CSR (.certSigningRequest, 步骤2生成)
* SSL certificate for server to send notification(aps_development.cer, 步骤5生成)
* private key (.p12, 步骤6生成)
把这三个文件放在一起。
注意:CSR文件其实已经没有用了,你可以删除它。但是如果你的cert过期了,你需要相同的CSR来上传到server生成新的cert。当然你也可以通过existingprivate key来重新生成CSR。
8 通过aps_development.cerand .p12 这两个文件执行下列命令来生成PEM file for php server to send notification.
1)Convert the .cer file into a .pem file.
openssl x509 -in aps_development.cer -inform der -out aps_development.pem
2)Convert the private key’s .p12 file into a .pem file。在此过程会要你输入.p12的密码,并为生成的pemfile设置密码
openssl pkcs12 -nocerts -out privatekey.pem -in privatekey.p12
3)把步骤1)和 2)生成的2个pen file合并成一个pen file, 该pen file就是供php server用来send notification的文件!!
cat aps_development.pem privatekey.pem > ck.pem
4)测试push SSL cert ( .pem file) and private key( .pem file)是否能成功连接APNS server.
telnet gateway.sandbox.push.apple.com 2195
上述命令尝试创建一个未加密的connection to dev apnsserver
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert aps_development.pem -key privatekey.pem
上述命令尝试使用push SSL cert and private key的PEMfile来创建一个secure connection。执行该命令时要求输入private key pen file的密码。如果成功连接,你会看到一大串的output。然后你应该能够输入一些字符,当你输入enter之后,servershould disconnect
注意: There are two different APNS servers: the “sandbox”server that you can use for testing, and the live server that you use inproduction mode. Above, we used the sandbox server because our certificate isintended for development, not production use.
9.下面使用步骤8的 3) 生成的ck.pem file来编写server side的php代码。
1)从http://d1xzuxjlafny7l.cloudfront.net/downloads/SimplePush.zip 下载php push代码, 并解压
2)把步骤8的3) 生成的ck.pem file 复制到php push 目录下,overwrite the old one
3)edit “simplepush.php” file following part
$deviceToken ='0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';
$passphrase = 'pushchat';
$message = 'My first push notification!';
stream_context_set_option($ctx,'ssl', 'local_cert', 'ck.pem');
4)执行命令
$php simplepush.php
一开始时,总是出现下面的error,怎么找也找不到原因。后来终于发现原来是由于在TextEdit里进行编辑,上面的“passphrase”变量的值的引号自动转成中文的引号,导致了低级错误!!
Warning: stream_socket_client(): Unable to set private key file`/Users/tomsonxu/Desktop/SimplePush/ck.pem' in/Users/tomsonxu/Desktop/SimplePush/simplepush.php on line 21
Warning: stream_socket_client(): failed to create an SSL handlein /Users/tomsonxu/Desktop/SimplePush/simplepush.php on line 21
Warning: stream_socket_client(): Failed to enable crypto in/Users/tomsonxu/Desktop/SimplePush/simplepush.php on line 21
Warning: stream_socket_client(): unable to connect tossl://gateway.sandbox.push.apple.com:2195 (Unknown error) in/Users/tomsonxu/Desktop/SimplePush/simplepush.php on line 21
Failed to connect: 0
10 生成push SSL cert (.p12) for c#to send push notification
详见http://blog.csdn.net/totogogo/article/details/7376214 的Step 14
有一点要补充:当在visual studio 2010里打开那个c# project for notification后,rebuild solution会报错:"Newtonsoft.Json" lib can't be found。因此你要right click "JdSoft.Apple.Apns.Notifications" node and select "Add Reference...",然后添加 “Newtonsoft.Json.Compact.dll"。(该dll文件在下载的c# project的"Reference"目录)