🔥 Firebase push notification hay messaging - khi thay đổi Firebase project account những mã lỗi invalid token cũ có thể có

Mở đầu

Từ token trong bài viết là chuỗi ký tự dài, unique dùng để định danh thiết bị cần notify/messaging.

Môi trường mình đã sử dụng để push notify/messaging là Node.js + Firebase Admin Node.js SDK hay firebase-admin trên npm.

Việc thay đổi Firebase project account hay thay đổi private key sang 1 project khác của Firebase là điều không ai mong muốn. Tuy nhiên nếu như phải làm và tự quản lý token trong DB, bạn sẽ gặp những lỗi invalid token, do token được sử dụng đó là của project cũ. Vậy thì việc cần làm là xử lý xóa hoặc thay thế các token lỗi. Việc này dựa vào việc xác định các mã lỗi hay error.code cụ thể.

Những mã lỗi mình đã gặp có thể có là:

messaging/mismatched-credential

{
  code: "messaging/mismatched-credential",
  message: "SenderId mismatch"
}

Nó được đặc tả trong firebase-admin-node/src/utils/error (thời điểm 2022/03):

  public static MISMATCHED_CREDENTIAL = {
    code: 'mismatched-credential',
    message: 'The credential used to authenticate this SDK does not have permission to send ' +
      'messages to the device corresponding to the provided registration token. Make sure the ' +
      'credential and registration token both belong to the same Firebase project.',
  };

Hoặc ta có thể xem code trên github 1s cho tiện.

Make sure the credential and registration token both belong to the same Firebase project.

Câu này cũng đã khẳng định mã lỗi này có thể rơi vào trường hợp thay đổi Firebase project.

messaging/registration-token-not-registered

{
  code: "messaging/registration-token-not-registered",
  message: "Requested entity was not found."
}

Nó được đặc tả trong firebase-admin-node/src/utils/error:

  public static REGISTRATION_TOKEN_NOT_REGISTERED = {
    code: 'registration-token-not-registered',
    message: 'The provided registration token is not registered. A previously valid registration ' +
      'token can be unregistered for a variety of reasons. See the error documentation for more ' +
      'details. Remove this registration token and stop using it to send messages.',
  };

The provided registration token is not registered.

Nó chưa được đăng ký tức là có thể thuộc project cũ.

Remove this registration token and stop using it to send messages.

Nhờ câu này ta cũng chắc chắn hơn về cách xử lý những token thế này.

delete it!

messaging/internal-error

Hiếm khi gặp lỗi nội bộ như thế này, tuy nhiên nếu có thì ta nên xử lý retry nhiều lần. Nếu vẫn gặp lỗi tương tự đối với token nhât định thì cũng nên xóa vì không dùng được token. Tham khảo thêm tại đây - firebase-messaging-fails-sporadically-with-internal-error.

Delete it

Ví dụ về cách xử lý err.code:

const notRegisteredTokenErrCode = 'messaging/registration-token-not-registered';
const mismatchedCredentialErrCode = 'messaging/mismatched-credential';

try {
  const res = await firebaseAdmin.messaging().send(message);
  console.log(`Push notification success, result: ${res}, token: ${token}!`);
} catch (err: any) {
  console.log(`Token: ${token}, err.code: ${err.code}, err.message: ${err.message}.`);

  if (err.code === notRegisteredTokenErrCode || err.code === mismatchedCredentialErrCode) {
    console.log(`Push notification failed, delete token!`);

    await deleteToken(token);
  } else {
    console.log(`Push notification failed! Do nothing!`);
  }
}