2012年11月20日火曜日

iOS デバイスへのプッシュ通知と node.js (プロバイダ編)

iOS デバイスへのプッシュ通知は、APNs (Apple Push Notification service) を使って実装できます。
加えて、配信するコンテンツを提供するサーバーも必要になります。
今回は、ちょうど使ってみたかった node.js で動かしてみます。

APNs の概要


全容を把握したい場合は、やっぱり公式のガイドがいいと思います。

 Local NotificationおよびPush Notificationプログラミングガイド

現時点では日本語版の方が古いということもなく、英語版の Local and Push Notification Programming Guide と同じ内容みたいですので、安心して日本語版を参照できますね。

APNs を利用したプッシュ通知を単純化すると以下のようになります。
(上記、公式ドキュメントからの引用です)


今回 node.js で作るのは、上図のプロバイダに相当するものです。


まずは iOS Provisioning Portal


APNs は実機でしか使えないので、Provisioning Portal で Provisioning Profile を用意します。手順は基本的に通常の iOS アプリ開発時と同じなので割愛。
ただし、App ID については2点ほど注意が必要です。

1. ワイルドカードを使っていない ID を作る
通常 AppID にはワイルドカードの指定が許されていますが、APNs を利用するアプリではワイルドカードは使用できません。

2. 1 の ID で APNs を有効にする
ID 作成後に、Configure から設定に入り、
Enable Push Notification service にチェックを入れて APNs を有効にした後に
証明書(Push SSL Certificate)を取得する必要があります。
証明書には、Development / Production の2つがありますが、今回は Development にします。


プロバイダを node.js で


簡単に APNs を利用できるモジュールがありますので、それを使います。

node-apn
 https://github.com/argon/node-apn

README がしっかりと用意されているので、そちらを読めば使い方は基本的な使い方は把握出来そうです。
READMEからポイントをいくつか転載。

インストールとコード内での利用
これはいつも通りです。
$ npm install apn
var apns = require('apn');

apns.Connection へ渡すオプションについて
デフォルトは以下のようになっています。
var options = {
    cert: 'cert.pem',                 /* Certificate file path */
    certData: null,                   /* String or Buffer containing certificate data, if supplied uses this instead of cert file path */
    key:  'key.pem',                  /* Key file path */
    keyData: null,                    /* String or Buffer containing key data, as certData */
    passphrase: null,                 /* A passphrase for the Key file */
    ca: null,                         /* String or Buffer of CA data to use for the TLS connection */
    gateway: 'gateway.push.apple.com',/* gateway address */
    port: 2195,                       /* gateway port */
    enhanced: true,                   /* enable enhanced format */
    errorCallback: undefined,         /* Callback when error occurs function(err,notification) */
    cacheLength: 100                  /* Number of notifications to cache for error purposes */
};

var apnsConnection = new apns.Connection(options);

  • cert, key には前節 App ID のところで取得した証明書を使う。
  • Development の Push SSL Certificate を使用する場合は、gateway の値を gateway.sandbox.push.apple.com に!
  • enhanced が true の場合、errorCallback が使用できます。(後述)

APNs の証明書
cert.pem, key.pem は以下のコマンドで作れます。

$ openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
$ openssl pkcs12 -in key.p12 -out key.pem -nodes
コマンド中の2つのインプットは次のようにして用意します。
cert.cer ... Provisioning Portal の App ID のところで取得したもの(aps_development.cerとか)
key.p12 ... aps_development.cer をダブルクリック等でインストール後、
  Keychain Access.app を起動 -> My Certificates -> Apple Development iOS Push Services を選択 -> File メニューから Export Items を選択
 で取得できます。



errorCallback について
APNs のエラー応答については、前述の公式ドキュメントの「バイナリインターフェイスの形式と通知形式(p.50)」あたりに詳しいです。
node-apn では、オプションの enhanced が true の状態で
errorCallback に function(err, notification) {} を指定することで APNs からのエラーメッセージが拾えます。




feedback について
フィードバックの概要は、公式ドキュメントの「フィードバックサービス(p.55)」で。
var feedback = new apns.Feedback(options);
で簡単に使用できます。

var options = {
    cert: 'cert.pem',                   /* Certificate file */
    certData: null,                     /* Certificate file contents (String|Buffer) */
    key:  'key.pem',                    /* Key file */
    keyData: null,                      /* Key file contents (String|Buffer) */
    passphrase: null,                   /* A passphrase for the Key file */
    ca: null,                           /* Certificate authority data to pass to the TLS connection */
    address: 'feedback.push.apple.com', /* feedback address */
    port: 2196,                         /* feedback port */
    feedback: false,                    /* enable feedback service, set to callback */
    interval: 3600                      /* interval in seconds to connect to feedback service */
};

options も Connection と同じような感じですね。
address は、開発用を使っている場合は Connection と同じように feedback.sandbox.push.apple.com を使用します。
feedback にコールバックをセットするとインターバルの間隔で呼び出されます。この際、コールバックがとる引数は2つ、サーバーが返す time と デバイストークンを含む Buffer です。

debug モジュール
debug モジュール ( https://github.com/visionmedia/debug ) を導入するとデバッグメッセージを有効にできます。
npm install debug.

$ DEBUG=apn node apns.js
のようにすると APNs 接続に関するデバッグメッセージがコンソールに表示されます。
DEBUG=apnfb では、フィードバックに関するメッセージが出力されます。

token
デバイストークン。
長くなってきたので、これについては iOS 側の実装とともにまた次回の記事で。


0 件のコメント:

コメントを投稿