---
id: connectors/firebase-messaging
title: Firebase messaging from a CLI or application backend
summary: Wire service-account grants, file storage, explicit message actions and public browser settings without generating an application.
keywords: connectors, integrations, firebase, fcm, service-account, jwt, notifications, push, vapid, cli, files
requires: @jskit-ai/connectors-core, @jskit-ai/connectors-catalog
---

# Firebase messaging from a CLI or application backend

## Use when

Use this pattern for an application's Firebase sender. The provider package
implements the service-account protocol and messaging operations; the core
implements token persistence, renewal and ownership. The application supplies
its existing authenticated identity, authorization, runtime directory and Env
bindings. The CLI uses these same libraries. Do not copy their token manager
or introduce an editor database.

Read the packaged `docs/firebase-cloud-messaging.md` before provisioning. No
OAuth callback exists for this credential mode. The application owner supplies
the Firebase project and service account through its own Env bindings.

## Do not use when

Use another provider contract for Firebase Authentication, Firestore or Cloud
Storage. This fragment does not provide those services or managed provisioning.

## Product decisions

Choose the target project, who may send which notifications, and whether the
application needs browser enrollment or only existing targets. Decide where
recipient/device associations belong in the application's existing storage.

## Invariants

Private service-account JSON remains on the server. Configuration stores its
reference, stable project identity and public settings. Verify before reporting
Connected; authorize every audience before sending. Preserve the existing file
store and token lock instead of copying the grant implementation.

Each collaborator the composition receives (`authorizeConnectionOperation`) is a
required parameter typed in a JSDoc `@param` and called directly, without a
`typeof` probe, a "requires x" guard or a no-op default. It is typed with
`ConnectorAuthorize` from `@jskit-ai/connectors-core/server/types`. A server
feature that uses it follows
[typed features](https://github.com/mobily-enterprises/jskit-ai/blob/main/packages/agent-docs/site/guide/framework/typed-features.md).

## Framework APIs

Use `parseIntegrationConfiguration`, `createConnectionService`,
`createEnvironmentReferenceResolver`, `createFileConnectionStore`,
`createCredentialProtection` and `firebaseCloudMessagingProvider` as shown below.
The public helper is `firebaseCloudMessagingWebConfiguration`; the standard
Feature action is `connectors.verifyServiceAccount`.

## Example files

The example module and inline fragments adapt to the application's existing
source files. They are source guidance, not a generated application or template
installation.

Adapt this source fragment to the application's existing startup owner:

[server/notifications.js](example/server/notifications.js) exports
`openNotificationConnections({ configurationPath, runtimeDirectory, credentialKey, environment, authorizeConnectionOperation })`.
It composes the existing libraries and returns the ordinary connection service.

`credentialKey` is a private 32-byte `Uint8Array`, loaded from the application's
secret owner, not a literal in generated source. `runtimeDirectory` is an
absolute private path outside the repository. `environment` contains the
service-account JSON string referenced by `integrations.json`; a path to that
JSON file is not the credential value. An application with its own secret store
can supply an equivalent `resolveReference` implementation.

`authorizeConnectionOperation(context, request)` must obtain the real owner
from trusted identity and return `{ applicationId, subjectId }` only after
checking access. Shared senders use an authorized shared subject. Every
`messages.send` requires checking `request.input.message` against the allowed
recipient and content. A signed-in user or assistant must not gain arbitrary
topic broadcasts merely by accessing this integration. Keep public and paid
project assignments in host policy.

## Explicit actions

From the existing CLI command or server action, verify the configured sender:

```js
await connections.connectServiceAccount({
  context: trustedContext, integrationId: "push", signal
});
```

This checks a fixed topic with `validate_only: true`. It does not send a test
notification. For an actual application message, build a bounded typed payload
and authorize it before invoking the send operation:

```js
const message = {
  fid: recipientInstallationId,
  notification: { title: "Order ready", body: "Your order is ready." },
  data: { orderId: String(order.id) }
};
await connections.invoke({ context: trustedContext, integrationId: "push",
  operation: "messages.validate", input: { message }, signal });
// Use the application's explicit send action and its authorization policy.
await connections.invoke({ context: trustedContext, integrationId: "push",
  operation: "messages.send", input: { message }, signal });
```

Validation does not authorize the later send and is not a delivery guarantee.
Use exactly one target: `fid`, existing `token`, `topic` or `condition`. Prefer
installation IDs when using the current Firebase SDK. Keep the installed SDK's
enrollment contract consistent; do not mix registration-token and installation
APIs. No automatic retry should wrap an ambiguous timed-out send. If the product
needs retry scheduling, implement that behavior in its existing delivery owner.

The standard JSKIT Feature is also available through
`createConnectorsFeature(options)` and `connectors.verifyServiceAccount`.
Product message operations belong in application actions that call `invoke`;
never expose an arbitrary authenticated URL or unconstrained provider operation.

## Variation points

### Browser composition

For a configuration with `clientMode: "web"`, use the pure library projection:

```js
import { firebaseCloudMessagingWebConfiguration } from "@jskit-ai/connectors-catalog/client/firebase-cloud-messaging";
const { firebaseConfig, vapidKey } = firebaseCloudMessagingWebConfiguration(publicPushSettings);
```

Supply `firebaseConfig` to the application's Firebase initialization and
`vapidKey` to its SDK registration call. Current documentation uses
`register(messaging, { vapidKey })` and `onRegistered(messaging, callback)`;
verify those exports against the version actually installed before wiring them.
The callback passes the installation ID to an authenticated app endpoint that
associates it with the correct user/device. Serve the service worker at the
app's origin and request notification permission through an explicit UI action.
The public configuration contains no private service-account data.

On a custom-domain change, register the browser on the new origin and replace
its installation association through the same authorized endpoint. Keep the
stable application identity and project assignment. The server token exchange
does not acquire a new OAuth callback just because an app uses another domain.

## Verification

Use a generated test key and an injected `fetchImpl` fixture. Verify JWT issuer,
audience, expiry, scope and signature; inspect `validate_only` and payload
targets; cover missing permissions, malformed receipts and cancellation. Use
the actual file store to test restart and rotation. Form tests must check
server/web mode changes, reference rejection, public values and file reload.
These checks do not prove live delivery or browser subscription. Do not create
a real project, key, subscription or notification as an incidental test.

## Avoid

Do not expose service-account JSON to the browser, confuse Firebase API keys
with private service credentials, or choose the paid project from caller input.
Do not treat an accepted message receipt as device delivery or automatically
retry a send whose outcome is unknown.

Do not probe a collaborator, default one to `null`, `{}` or a no-op, fill a slot
with a setter after construction, or keep a module-level registry.
