};
function errorHappened(err, notification){
console.log(“err ” + err);
}
var apnsConnection = new apns.Connection(options);
一开始就使用 apn 这个 library,然后设定一些 options,比如 cert 和 key 文件的路径文件
名,再来就是测试用的
Apple 主机 - gateway.sandbox.push.apple.com 和 port。要注意的是现
在 这 个 主 机 是 测 试 用 的 , 正 式 上 线 要 改 用
gateway.push.apple.com , 再 来 准 备 一 个
errorCallBack 的 function,当这个联机有问题时,我可以从 errorHappend 得知错误情况。最
后是新建一个
connection 然后指定到 apnsConnection。
解说到这先停一下,看要如何来产生 cert.pem 和 key.pem 这两个 file.
进入
iOS Developer Portal 的时候先选到某一个 App IDs 的 Configure.
接着要把 Enable Apple Push Notification Service 的底下有两个一个是 Development 另一个
是
Production 。把 Development 点选 Configure 上传自己的 Certification Request 然后得到
Developer Portal 的 Provisioning Profile 了。就会有一个下载的按钮,上传的步骤就和一开始
要得到
Developer Provisioning Profile 一样。
一个一个产生,在 Certificates 上面(也就是蓝色外框图示)右键单击,选 export。
File Format 选 Certificate,然后命名为 cert.cer,存到 apnServer.js file 的路径。
同样方法,右键单击 export <key>
这次的 format 选 .p12 命名为 key.p12,和刚刚的 cert.cer 同一个文件夹
用 Terminal 在这两个档案的目录下,依序输入这两行指令。
$ openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
$ openssl pkcs12 -in key.p12 -out key.pem -nodes
这样一个 key.pem 和 cert.pem 都准备好了。回到 apnServer.js 看看
var token = “xxxx;oiqwjf;oiwje;foiajw;f”;
var myDevice = new apns.Device(token);