Firebase Admin SDK for Java のバージョン 7.0.0 では、API にいくつかの重要な変更が導入されています。このリリースの API の変更は主に、Authentication と FCM のエラー処理の追加と改善です。
一般的なエラー処理の変更点
FirebaseException 基本クラスでいくつか新しい属性が利用できるようになりました。
ErrorCode getErrorCode(): 例外に関連するプラットフォームのエラーコードを返します。FirebaseExceptionのすべてのインスタンスは、null のプラットフォームのエラーコードが保証されます。プラットフォーム エラーコードは、新しい列挙型ErrorCodeとして定義されています。IncomingHttpResponse getHttpResponse(): 例外に関連付けられた HTTP レスポンスを返します。バックエンド HTTP レスポンス以外の理由によって例外が発生した場合は、null の可能性もあります。
以前と同様、SDK で定義された他の例外タイプ(FirebaseAuthException や FirebaseMessagingException など)は、FirebaseException 基本クラスから派生します。
Auth エラー処理の変更点
FirebaseAuth クラスのすべての API で FirebaseAuthException のインスタンスがスローされることがあります。非同期 API(ApiFuture を返すメソッドなど)は、FirebaseAuthException をラップする ExecutionException で失敗する場合があります。Auth 固有のエラーコードの定義は、新しい列挙型の AuthErrorCode で公開されます。
以前(v6.15.0 以前)
try {
FirebaseAuth.getInstance().verifyIdToken(idToken, true);
} catch (FirebaseAuthException ex) {
if (ex.getErrorCode().equals("id-token-revoked")) {
System.err.println("ID token has been revoked");
} else {
System.err.println("ID token is invalid");
}
}
現在(v7.0.0 以降)
try {
FirebaseAuth.getInstance().verifyIdToken(idToken, true);
} catch (FirebaseAuthException ex) {
if (ex.getAuthErrorCode() == AuthErrorCode.REVOKED_ID_TOKEN) {
System.err.println("ID token has been revoked");
} else {
System.err.println("ID token is invalid");
}
}
AuthErrorCode は、基本の FirebaseException タイプから継承された ErrorCode に追加されます。必要に応じて、両方のエラーコードを調べるエラー処理ロジックを実装できます。
FCM エラー処理の変更点
FirebaseMessaging クラスのすべての API で FirebaseMessagingException のインスタンスがスローされることがあります。非同期 API(ApiFuture を返すメソッドなど)は、FirebaseMessagingException をラップする ExecutionException で失敗する場合があります。Authentication 固有のエラーコードの定義は、新しい列挙型の MessagingErrorCode で公開されます。
以前(v6.15.0 以前)
try {
FirebaseMessaging.getInstance().send(message);
} catch (FirebaseMessagingException ex) {
if (ex.getErrorCode().equals("registration-token-not-registered")) {
System.err.println("Device token has been unregistered");
} else {
System.err.println("Failed to send the notification");
}
}
現在(v7.0.0 以降)
try {
FirebaseMessaging.getInstance().send(message);
} catch (FirebaseMessagingException ex) {
if (ex.getMessagingErrorCode() == MessagingErrorCode.UNREGISTERED) {
System.err.println("Device token has been unregistered");
} else {
System.err.println("Failed to send the notification");
}
}
MessagingErrorCode は、基本の FirebaseException タイプから継承された ErrorCode に追加されます。必要に応じて、両方のエラーコードを調べるエラー処理ロジックを実装できます。
Authentication カスタム クレーム
非推奨になった FirebaseAuth.setCustomClaims() メソッドが削除されました。代わりに FirebaseAuth.setCustomUserClaims() を使用してください。
以前(v6.15.0 以前)
FirebaseAuth.getInstance().setCustomClaims(uid, claims);
現在(v7.0.0 以降)
FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
FCM 通知コンストラクタ
非推奨になった Notification クラスのコンストラクタが削除されました。Notification.Builder クラスを使用して、新しいインスタンスを作成してください。
以前(v6.15.0 以前)
Notification notification = new Notification(title, body, url);
現在(v7.0.0 以降)
Notification notification = Notification.builder()
.setTitle(title)
.setBody(body)
.setImage(url)
.build();