iOS APNS 推送 (Development 环境)

Developer.apple.com

Certificates

iOS App Development 创建

  • 选择iOS App Development 选项创建Certificates.
  • Upload CSR file.
  • Generate
  • Download

注:此Certificates用于Xcode Code Signing Identity

Apple Push Notification service SSL (Sandbox) 创建

  • 选择Apple Push Notification service SSL (Sandbox)选项创建Certificates.
  • Select an App ID for your Push SSL Certificate (Sandbox).
  • Upload CSR file.
  • Generate
  • Download

注:此Certificates用于推送服务器端。

Identifiers

Devices

Provisioning Profiles

  • 使用iOS App Development选项创建Provisioning Profiles.
  • Select App ID.
  • Select certificates.
  • Select devices.
  • Name this profile and generate.
  • Generate
  • Download

注:Select certificates阶段是无法选择Apple Push Notification service SSL (Sandbox) Certificates的。

PushMeBaby工具使用

PushMeBaby GitHub地址

apns.cer 文件添加

self.certificate = [[NSBundle mainBundle] pathForResource:@"apns" ofType:@"cer"];

文件为Apple Push Notification service SSL (Sandbox) Certificates
重命名为apns.cer置于工程目录并添加到工程。
注:钥匙串中要有Certificates文件的私钥。

deviceToken 添加

获取deviceToken,并代码中添加,格式为

self.deviceToken = @"bf2a347d b9ce9568 6456952f dc0198e5 c4e257be c7c307a9 00c462e3 3df2671d";

Node.js 脚本测试

pem文件生成

1、打开Keychain Access,分别将Apple Push Notification service SSL (Sandbox) Certificatescertificateprivate key导出得到文件apns-dev-cert.p12apns-dev-key.p12

2、通过终端命令将这些文件转换为PEM格式:

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

第二行命令要求输入一个密码,输入123456。此密码测试脚本将使用。

3、合成为apns-dev.pem文件,此文件在连接到APNS时需要使用:

cat apns-dev-cert.pem apns-dev-key.pem > apns-dev.pem

测试脚本生成 (node.js)

var apn=require('apn');
    var token ='e5cfc91053e2d2929c987182dd212e4b25d5cab74fab7da639bc1d8dca77ea80'; //长度为64的设备Token,去除空格
    var options = { 
        "cert": "/Users/yujianfeng/Downloads/apns-dev-cert.pem", //cert.pem文件的路径
        "key": "/Users/yujianfeng/Downloads/apns-dev-key.pem",   //key.pem文件的路径
        "gateway": "gateway.sandbox.push.apple.com",
        "passphrase": "123456",
        "port": 2195},
            apnConnection = new apn.Connection(options),
            device = new apn.Device(token),
            note = new apn.Notification();
    note.expiry = Math.floor(Date.now() / 1000) + 60;
    note.badge = 3;
    note.alert = 'test APNS ';
    note.sound = 'default';
    note.payload = {'messageFrom': 'Caroline'};
    note.device = device;
    apnConnection.pushNotification(note, device);

脚本推送:

1、Node.js安装
Node.js

2、终端设置环境变量

export NODE_PATH=/usr/local/lib/node_modules/

3、终端module安装

sudo npm install apn

4、终端node命令执行

node node.js

你可能感兴趣的:(iOS APNS 推送 (Development 环境))