iOS APNS 推送 (Development 環(huán)境)

Developer.apple.com

Certificates

iOS App Development 創(chuàng)建

  • 選擇iOS App Development 選項創(chuàng)建Certificates.
  • Upload CSR file.
  • Generate
  • Download

注:此Certificates用于Xcode Code Signing Identity。

Apple Push Notification service SSL (Sandbox) 創(chuàng)建

  • 選擇Apple Push Notification service SSL (Sandbox)選項創(chuàng)建Certificates.
  • Select an App ID for your Push SSL Certificate (Sandbox).
  • Upload CSR file.
  • Generate
  • Download

注:此Certificates用于推送服務(wù)器端。

Identifiers

Devices

Provisioning Profiles

  • 使用iOS App Development選項創(chuàng)建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導(dǎo)出得到文件apns-dev-cert.p12,apns-dev-key.p12

2、通過終端命令將這些文件轉(zhuǎn)換為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的設(shè)備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、終端設(shè)置環(huán)境變量

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

3、終端module安裝

sudo npm install apn

4、終端node命令執(zhí)行

node node.js
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容