@sadikkhan02/adapter-fcm

中文文档
1.0.0发布于 2026/11/02MITTypeScriptESM + CJS零依赖

Firebase Cloud Messaging (FCM) push notification provider adapter for NotifyKit

原始使用文档
文档目录8 节
  1. Installation
  2. Configuration & Credentials
  3. Environment Variables
  4. Precedence Order
  5. Usage
  6. Error Classification & DLQ Routing
  7. Automatic Private Key Sanitization
  8. License

@sadikkhan02/adapter-fcm

Mobile & Web Push notification adapter for NotifyKit using Firebase Cloud Messaging (FCM).

This package delivers push notifications across Android, iOS (APNs through FCM), and Web browsers via Google Firebase Cloud Messaging v1 API. It conforms to the PushProvider contract, stringifies non-string data payloads, and handles token invalidation gracefully.


Installation

npm install @sadikkhan02/notifykit @sadikkhan02/adapter-fcm firebase-admin

Note: firebase-admin is declared as an external peer dependency to allow consumers control over SDK version pinning, Google Cloud environment authentication, and Firebase App pooling.


Configuration & Credentials

FCM requires Google Cloud service account credentials with the Firebase Cloud Messaging API Admin role.

Environment Variables

VariableDescription
FCM_SERVICE_ACCOUNT_JSONRaw JSON string or file path to your Firebase service account key
GOOGLE_APPLICATION_CREDENTIALSStandard Google Cloud SDK credential path fallback
FCM_PROJECT_IDOptional explicit Google Cloud Project ID

Precedence Order

The adapter resolves service account credentials in the following order:

  1. Direct serviceAccount parameter in new FcmPushProvider({ serviceAccount: ... }) (object, JSON string, or file path)
  2. process.env.FCM_SERVICE_ACCOUNT_JSON
  3. process.env.GOOGLE_APPLICATION_CREDENTIALS
  4. Pre-initialized Firebase app or messaging client injected in options

Usage

import { createNotifier } from '@sadikkhan02/notifykit';
import { FcmPushProvider } from '@sadikkhan02/adapter-fcm';

const pushProvider = new FcmPushProvider({
  // Can be an object, JSON string, or file path:
  serviceAccount: process.env.FCM_SERVICE_ACCOUNT_JSON,
});

const notifier = createNotifier({
  providers: {
    push: {
      type: 'fcm',
      adapter: pushProvider,
    },
  },
});

// Register push notification template
notifier.registerTemplate('new-message', '{{sender}} sent you a new message: {{preview}}');

// Enqueue push notification (returns immediately with jobId)
const { jobId } = await notifier.notify({
  to: { pushToken: 'fcm_device_registration_token_xyz...' },
  channels: ['push'],
  template: 'new-message',
  data: {
    sender: 'Sarah',
    preview: 'Hey, are you free for a call?',
    // Data values are automatically stringified for FCM compliance
    conversationId: 'conv_123',
  },
});

// Start processing push queue
notifier.startWorker({ channels: ['push'], concurrency: 20 });

Error Classification & DLQ Routing

FCM error codes are categorized cleanly between transient network/server glitches and permanent token invalidations:

FCM / Google Error CodeDescriptionretryableBullMQ Action
messaging/server-unavailableGoogle FCM backend overloadedtrueRetried with exponential backoff
messaging/internal-errorTransient internal Firebase errortrueRetried with exponential backoff
messaging/quota-exceededProject sending rate throttledtrueRetried with exponential backoff
messaging/unknown-errorUnknown network/gateway glitchtrueRetried with exponential backoff
messaging/registration-token-not-registeredDevice uninstalled app / token expiredfalseRouted directly to DLQ (no retry)
messaging/invalid-registration-tokenMalformed or corrupted token stringfalseRouted directly to DLQ (no retry)
messaging/mismatched-credentialToken belongs to different sender IDfalseRouted directly to DLQ (no retry)
messaging/authentication-errorInvalid service account or expired keyfalseRouted directly to DLQ (no retry)

Automatic Private Key Sanitization

All error logs and provider results automatically strip private keys (BEGIN PRIVATE KEY), bearer tokens, and client secrets.


License

MIT

版本与发布记录

@sadikkhan02/adapter-fcm 共发布 2 个版本,首次发布于 2026/11/02;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.0latest2026/11/021 天前
0.0.0-stage2026/11/021 天前

关于 @sadikkhan02/adapter-fcm 的常见问题

@sadikkhan02/adapter-fcm 是什么?

用于NotifyKit的Firebase Cloud Messaging (FCM)推送通知提供程序适配器

如何安装 @sadikkhan02/adapter-fcm?

在项目目录运行 npm install @sadikkhan02/adapter-fcm,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:firebase-admin(>=11.0.0)、@sadikkhan02/notifykit(^1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@sadikkhan02/adapter-fcm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@sadikkhan02/adapter-fcm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@sadikkhan02/adapter-fcm 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@sadikkhan02/adapter-fcm 可以商用吗?

@sadikkhan02/adapter-fcm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@sadikkhan02/adapter-fcm 还在维护吗?

最新版本 1.0.0 发布于 2026/11/02(1 天前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/11/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。