■プロビジョニングポータル
App IDs > Configure > Enable for Apple Push Notification serviceにチェックを入れ、「Development Push SSL Certificate」のConfigureをクリックする。
キーチェーンアクセスを起動させ、キーチェーンアクセス > 証明書アシスタント > 認証局に証明書を要求
以下の項目を入力する。
- ユーザーのメールアドレス
- 通称(コモンネーム)
ディスクに保存を選択するとCertificateSigningRequest.certSigningRequestというファイルが保存される。「続ける」を押す。
continueを押し保存したファイルをアップロードすると、「Development Push SSL Certificate」のStatusがEnableになるので、Downloadしてダブルクリックしキーチェーンアクセスに保存する。
キーチェーンアクセス > 自分の証明書
- Apple Development Push Servicesの証明書を選択し、ファイルから書き出しを実行してcert.p12で保存
- Apple Development Push Servicesの秘密鍵を選択し、ファイルから書き出しを実行してkey.p12で保存
保存したディレクトリで以下のコマンドを実行する。
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12 openssl pkcs12 -nocerts -out key.pem -in key.p12 openssl rsa -in key.pem -out key-noenc.pem cat cert.pem key-noenc.pem > dev.pem
ここで生成されたdev.pemはサーバー側で使用する。
■クライアント側の実装
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; return YES; }
以下のメソッドはdevice tokenを受け取った時に実行される。binaryを変換して(文字列を取り出して)サーバー側の仕様にそった形式にして送信する。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *str = [NSString stringWithFormat:@"%@",deviceToken]; NSString *newString = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@"<" withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@">" withString:@""]; NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; [ud setObject:newString forKey:KEY_DEVICE_TOKEN]; [ud synchronize]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"error:%@", error); }
リモート通知はシミュレータではサポートされていない。また、「エンタイトルメント文字列が見つかりません」と表示されたら、有効な開発用のプロビジョニングプロファイルがインストールできていないという事である。
以下のメソッドはpush notificationを受け取った時に実行される。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); UILocalNotification *notification = [[UILocalNotification alloc] init]; [notification setFireDate:[NSDate date]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [notification setAlertBody:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]; [notification setUserInfo:userInfo]; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; }
■サーバー側の実装
Socket通信ができれば言語は特に限定しないが今回はゆとり言語PHPとゆとりライブラリでやる。
cd vendors/ wget http://apns-php.googlecode.com/files/ApnsPHP-r100.zip unzip ApnsPHP-r100.zip
Cakeとの親和性が悪いので以下のようにして読み込む。
App::import('Vendor', 'ApnsPHP/Log/Interface'); App::import('Vendor', 'ApnsPHP/Log/Embedded'); App::import('Vendor', 'ApnsPHP/Abstract'); App::import('Vendor', 'ApnsPHP/Exception'); App::import('Vendor', 'ApnsPHP/Push'); App::import('Vendor', 'ApnsPHP/Message'); $push = new ApnsPHP_Push( ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, '/path-to-cert/dev.pem' ); $push->connect(); $message = new ApnsPHP_Message('123456789db0b2478f123456789f8067e3c61234567891df1d04851234567898');//device token $message->setText('こんにちはこんばんわ'); $message->setExpiry(30); $push->add($message); $push->send(); $push->disconnect();