ユーザフォーラムが開始しました!そこでこれまでユーザの方から寄せられている質問の中からよくある質問を取り上げたいと思います。
プッシュ通知が届かない
ニフクラ mobile backendを使って開発している中で一番多く寄せられているのがこのプッシュ通知が届かないという質問です。この時チェックして欲しいのが以下の3つのポイントです。
1. 開発用とリリース用アプリは分けましょう
開発用とリリース用のプッシュ配信用証明書は異なります。開発用でトークンを登録してしまうと、同じmBaaSアプリをリリース用として使った場合にエンドポイントが異なるために受信されない場合があります。
一旦全てのトークンを消すか、開発用とリリース用のアプリを分けて登録し、それぞれ証明書を登録して使うようにしてください。
2. デバイストークンが追加されない
プッシュ通知の確認ダイアログで許可しているにも関わらずトークンが管理画面に反映されない場合、何らかのエラーが発生している可能性があります。iOSであれば
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { if ([error code] == 3010) { NSLog(@"Push notifications don't work in the simulator!"); } else { NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error); } }
のようなコードを記述して発生しているエラーを確認してください。
3. アプリを再インストールした場合
アプリを一旦削除して再インストールした場合、トークンの重複エラーが発生します。そこでアプリの再インストールを考慮した端末情報の登録を参考の上、エラー処理に対応してください。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NCMBInstallation *currentInstallation = [NCMBInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; [currentInstallation setObject:[[UIDevice currentDevice] systemVersion] forKey:@"osVersion"]; [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if(succeeded){ //端末情報の登録が成功した場合の処理 NSLog(@"didRegisterForRemoteNotification/installation save successed."); NSLog(@"currentInstallation:%@", [NCMBInstallation currentInstallation]); } else { //端末情報の登録が失敗した場合の処理 if (error.code == 409){ //失敗理由がdeviceTokenの重複だった場合は、登録された端末情報を取得する NCMBQuery *installationQuery = [NCMBInstallation query]; [installationQuery whereKey:@"deviceToken" equalTo:currentInstallation.deviceToken]; NCMBInstallation *install = (NCMBInstallation*)[installationQuery getFirstObject:&error]; currentInstallation.objectId = install.objectId; if (!error){ //上書き保存する [currentInstallation save:&error]; if (error){ NSLog(@"CurrentInstallation can't save data:%@", error); } else { NSLog(@"currentInstallation:%@, insatll:%@", [NCMBInstallation currentInstallation], install); } } else { NSLog(@"Can't get first object from Installation Class."); } } else { NSLog(@"installation can't save:%@", error); } } }]; }
プッシュ通知周りはハマりやすいので、不明点があればユーザフォーラムで質問してみるといいでしょう(コミュニティベースなので回答は確実という訳ではありませんのでご注意ください)。