Java एडमिन SDK v7 पर माइग्रेट करें

Java के लिए Firebase Admin SDK के वर्शन 7.0.0 में, एपीआई में कुछ अहम बदलाव किए गए हैं. इस रिलीज़ में, एपीआई में मुख्य रूप से ये बदलाव किए गए हैं: Authentication और FCM के लिए, गड़बड़ी ठीक करने की सुविधा जोड़ी गई है और इसमें सुधार किया गया है.

गड़बड़ी ठीक करने से जुड़े सामान्य बदलाव

FirebaseException बेस क्लास अब कई नए एट्रिब्यूट दिखाता है:

  • ErrorCode getErrorCode(): यह अपवाद से जुड़ा प्लैटफ़ॉर्म गड़बड़ी कोड दिखाता है. FirebaseException के हर इंस्टेंस में, प्लैटफ़ॉर्म के गड़बड़ी वाले कोड का नॉन-नल होना ज़रूरी है. संभावित प्लैटफ़ॉर्म गड़बड़ी कोड को नए enum टाइप ErrorCode के तौर पर तय किया गया है.
  • IncomingHttpResponse getHttpResponse(): यह अपवाद से जुड़ा एचटीटीपी रिस्पॉन्स दिखाता है. अगर अपवाद, बैकएंड एचटीटीपी रिस्पॉन्स के अलावा किसी और वजह से हुआ है, तो यह शून्य हो सकता है.

पहले की तरह, एसडीके में तय किए गए ज़्यादातर अन्य अपवाद टाइप (उदाहरण के लिए, FirebaseAuthException, FirebaseMessagingException) FirebaseException बेस क्लास से लिए गए हैं.

पुष्टि करने से जुड़ी गड़बड़ी को ठीक करने के तरीके में बदलाव

FirebaseAuth क्लास में मौजूद सभी एपीआई, FirebaseAuthException के इंस्टेंस थ्रो कर सकते हैं. एसिंक्रोनस एपीआई (उदाहरण के लिए, ऐसे तरीके जो ApiFuture दिखाते हैं) ExecutionException के साथ काम नहीं कर सकते. इसमें FirebaseAuthException शामिल होता है. खाते से जुड़ी गड़बड़ी के कोड, सार्वजनिक तौर पर नए enum टाइप 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 क्लास के सभी एपीआई, FirebaseMessagingException के इंस्टेंस थ्रो कर सकते हैं. एसिंक्रोनस एपीआई (उदाहरण के लिए, ऐसे तरीके जो ApiFuture दिखाते हैं) ExecutionException के साथ काम नहीं कर सकते. इसमें FirebaseMessagingException शामिल होता है. 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();