iPhoneのインスタントアラートサービス (APNS)をiPhoneアプリケーション開発に適用する方法を説明します。
やることは4つです。
まずはPushNotificationサービス機能をONにしたProvisioningファイルを取得する必要がある。
- iPhone Developer Connection PortalにアクセスしてApp IDsタブを選択
- App IDはワイルドカード(*)は使用してはいけない。ワイルドカードを使用したらPush Serviceを使用することができない。例(私のApp ID)
AB123346CD.com.serverdensity.iphone - Submitを押して保存後、Enable for Apple Push Notification Serviceを選択してからDevelopment Push SSL CertificateのConfigureをクリックする
- Keychain Access部分は説明通り辿っていけばOK。その後signed authorityをportalにアップロードしてcertificateファイル(*.cer)をダウンロードしてくる
- certificateファイルをダブルクリックしてKeychain Accessにインポートする
- Keychain AccessのCertificatesカテゴリを選択して、"Apple Development Push Services"を見つける
- "Apple Development Push Services" を右クリックして Export "Apple Development Push Services ID123″を選択し、"apns-dev-cert.p12"と名前をつけて保存
- その真下にある"Private Key"も同様で"apns-dev-key.p12"を名前をつけて保存
- これらのファイルを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
- サーバーがAPNSにアクセスするときにPEMファイルが必要なので、keyとcertファイルを合わせないて、apns-dev.pemファイルとして保存する
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
iphoneアプリ側で
UIApplicationでregisterForRemoteNotificationTypes:を呼ぶiphoneアプリ側で
UIApplicationDelegateのapplication:didRegisterForRemoteNotificationsWithDeviceToken:メソッドをインプリメントしてiPhone端末に通知するために必要な device tokenを取得する。- 取得したdevice tokenをサーバー側に送信。device tokenを保管(DBなど)する。サーバー側からiPhone端末に通知する時にdevice tokenを使って通知する。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
|
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
#if !TARGET_IPHONE_SIMULATOR
// Get the users Device Model, Display Name, Unique ID, Token & Version Number
UIDevice *dev = [UIDevice currentDevice];
NSString *deviceUuid = dev.uniqueIdentifier;
NSString *deviceName = dev.name;
NSString *deviceModel = dev.model;
NSString *deviceSystemVersion = dev.systemVersion;
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[devToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"deviceToken: %@", deviceToken);
#endif
}
|
注意書き:
アプリを起動した時に下記のようなエラーメッセージがコンソールに出て、DeviceTokenを取得できない人たちが出てくるでしょう。
"Error Domain=NSCocoaErrorDomain Code=3000 UserInfo=0x113e80 "no valid 'aps-environment' entitlement string found for application"
これはビルドに使用したProvisioning ProfileがPush Notification Serviceの使用が
許可されてないからです。
まずは、プロジェクトのプロパティを開き
Code Signing Identityが先程作成したPush Notification ServiceがONになっている
Profileのを確認。一度Clean All Targetsを選択して、Buildし直してみてください。
これでも同じエラーが出るようでしたら、Provisioning Profile(*.mobileprovision)ファイルを
App IDのApple Push Notification serviceをEnableに設定する前にダウンロードした可能性が高いです。
もう一度、Provisioning Profile(*.mobileprovision)ファイルを
ダウンロードしてXcodeのOrganizerを使って適用したらいいでしょう。
あと、Organizerで古いProvisioning Profileは削除した方がいいです。
時々古いファイルを読み込んでしまう時があります。


コメントする