The Firebase Cloud Messaging Web SDK. This SDK does not work in a Node.js environment.

## Functions

| Function | Description |
|---|---|
| **function(app, ...)** |   |
| [getMessaging(app)](https://firebase.google.com/docs/reference/js/messaging#getmessaging_cf608e1) | Retrieves a Firebase Cloud Messaging instance. |
| **function(messaging, ...)** |   |
| [deleteToken(messaging)](https://firebase.google.com/docs/reference/js/messaging#deletetoken_3fae4b1) | Deletes the registration token associated with this [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance and unsubscribes the [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance from the push subscription.If there is no legacy registration token but the client has FID-based registration metadata (from [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a)), this deletes that registration on the server, clears local metadata, and invokes [onUnregistered()](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) with the removed FID when successful. |
| [getToken(messaging, options)](https://firebase.google.com/docs/reference/js/messaging#gettoken_b538f38) | Subscribes the [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance to push notifications. Returns a Firebase Cloud Messaging registration token that can be used to send push messages to that [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance.If notification permission isn't already granted, this method asks the user for permission. The returned promise rejects if the user does not allow the app to show notifications. |
| [onMessage(messaging, nextOrObserver)](https://firebase.google.com/docs/reference/js/messaging#onmessage_b9887da) | When a push message is received and the user is currently on a page for your origin, the message is passed to the page and an `onMessage()` event is dispatched with the payload of the push message. |
| [onRegistered(messaging, nextOrObserver)](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) | Subscribes to an event that the app instance is registered with FCM via Firebase Installation ID (FID). Use the FID passed to the callback to upload it to your application server. When you receive an FID after calling [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a), instruct your backend to remove any legacy token for this instance. |
| [onUnregistered(messaging, nextOrObserver)](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) | Subscribes to an event that the app instance is unregistered from FCM (FID no longer active). Use this to notify your backend to remove this FID to prevent 404 errors on send. |
| [register(messaging, options)](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a) | Registers the app instance with FCM using its Firebase Installation ID (FID). The FID is delivered via the [onRegistered()](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) callback, not as a return value. Call this to establish an FID-based identity; once [onRegistered()](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) provides an FID, instruct your backend to remove any legacy token previously associated with this instance. The backend send API supports FID as a target. |
| [unregister(messaging)](https://firebase.google.com/docs/reference/js/messaging#unregister_3fae4b1) | Unregisters the app instance from FCM by deleting its FID-based registration. On success, triggers [onUnregistered()](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) (if registered) with the unregistered FID. |
| **function()** |   |
| [isSupported()](https://firebase.google.com/docs/reference/js/messaging#issupported) | Checks if all required APIs exist in the browser. |

## Interfaces

| Interface | Description |
|---|---|
| [FcmOptions](https://firebase.google.com/docs/reference/js/messaging.fcmoptions#fcmoptions_interface) | Options for features provided by the FCM SDK for Web. See [WebpushFcmOptions](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions). |
| [GetTokenOptions](https://firebase.google.com/docs/reference/js/messaging.gettokenoptions#gettokenoptions_interface) | Options for [getToken()](https://firebase.google.com/docs/reference/js/messaging#gettoken_b538f38). |
| [MessagePayload](https://firebase.google.com/docs/reference/js/messaging.messagepayload#messagepayload_interface) | Message payload that contains the notification payload that is represented with [NotificationPayload](https://firebase.google.com/docs/reference/js/messaging.notificationpayload#notificationpayload_interface) and the data payload that contains an arbitrary number of key-value pairs sent by developers through the [Send API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification). |
| [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | Public interface of the Firebase Cloud Messaging SDK. |
| [NotificationPayload](https://firebase.google.com/docs/reference/js/messaging.notificationpayload#notificationpayload_interface) | Display notification details. Details are sent through the [Send API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification). |
| [RegisterOptions](https://firebase.google.com/docs/reference/js/messaging.registeroptions#registeroptions_interface) | Options for [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a). Same shape as GetTokenOptions for SW and VAPID configuration. |

## function(app, ...)

### getMessaging(app)

Retrieves a Firebase Cloud Messaging instance.

**Signature:**

    export declare function getMessagingInWindow(app?: FirebaseApp): Messaging;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| app | [FirebaseApp](https://firebase.google.com/docs/reference/js/app.firebaseapp#firebaseapp_interface) |   |

**Returns:**

[Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface)

The Firebase Cloud Messaging instance associated with the provided firebase app.

## function(messaging, ...)

### deleteToken(messaging)

> > [!WARNING]
> > **Warning:** This API is now obsolete.
>
> Use [onUnregistered()](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) to observe when the client is no longer registered and update your backend accordingly, instead of explicitly deleting the registration token with this API.

Deletes the registration token associated with this [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance and unsubscribes the [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance from the push subscription.

If there is no legacy registration token but the client has FID-based registration metadata (from [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a)), this deletes that registration on the server, clears local metadata, and invokes [onUnregistered()](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) with the removed FID when successful.

**Signature:**

    export declare function deleteToken(messaging: Messaging): Promise<boolean>;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |

**Returns:**

Promise\<boolean\>

The promise resolves when the token has been successfully deleted.

### getToken(messaging, options)

> > [!WARNING]
> > **Warning:** This API is now obsolete.
>
> Use [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a) together with [onRegistered()](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) for Firebase Installation ID-based messaging instead of retrieving an FCM registration token with this API.

Subscribes the [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance to push notifications. Returns a Firebase Cloud Messaging registration token that can be used to send push messages to that [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance.

If notification permission isn't already granted, this method asks the user for permission. The returned promise rejects if the user does not allow the app to show notifications.

**Signature:**

    export declare function getToken(messaging: Messaging, options?: GetTokenOptions): Promise<string>;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |
| options | [GetTokenOptions](https://firebase.google.com/docs/reference/js/messaging.gettokenoptions#gettokenoptions_interface) | Provides an optional vapid key and an optional service worker registration. |

**Returns:**

Promise\<string\>

The promise resolves with an FCM registration token.

### onMessage(messaging, nextOrObserver)

When a push message is received and the user is currently on a page for your origin, the message is passed to the page and an `onMessage()` event is dispatched with the payload of the push message.

**Signature:**

    export declare function onMessage(messaging: Messaging, nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>): Unsubscribe;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |
| nextOrObserver | [NextFn](https://firebase.google.com/docs/reference/js/util#nextfn)\<[MessagePayload](https://firebase.google.com/docs/reference/js/messaging.messagepayload#messagepayload_interface)\> \| [Observer](https://firebase.google.com/docs/reference/js/util.observer#observer_interface)\<[MessagePayload](https://firebase.google.com/docs/reference/js/messaging.messagepayload#messagepayload_interface)\> | This function, or observer object with `next` defined, is called when a message is received and the user is currently viewing your page. |

**Returns:**

[Unsubscribe](https://firebase.google.com/docs/reference/js/util#unsubscribe)

To stop listening for messages execute this returned function.

### onRegistered(messaging, nextOrObserver)

Subscribes to an event that the app instance is registered with FCM via Firebase Installation ID (FID). Use the FID passed to the callback to upload it to your application server. When you receive an FID after calling [register()](https://firebase.google.com/docs/reference/js/messaging#register_795bb8a), instruct your backend to remove any legacy token for this instance.

**Signature:**

    export declare function onRegistered(messaging: Messaging, nextOrObserver: NextFn<string> | Observer<string>): Unsubscribe;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |
| nextOrObserver | [NextFn](https://firebase.google.com/docs/reference/js/util#nextfn)\<string\> \| [Observer](https://firebase.google.com/docs/reference/js/util.observer#observer_interface)\<string\> | A function or observer object called when an FID is registered. |

**Returns:**

[Unsubscribe](https://firebase.google.com/docs/reference/js/util#unsubscribe)

Unsubscribe function to stop listening.

### onUnregistered(messaging, nextOrObserver)

Subscribes to an event that the app instance is unregistered from FCM (FID no longer active). Use this to notify your backend to remove this FID to prevent 404 errors on send.

**Signature:**

    export declare function onUnregistered(messaging: Messaging, nextOrObserver: NextFn<string> | Observer<string>): Unsubscribe;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |
| nextOrObserver | [NextFn](https://firebase.google.com/docs/reference/js/util#nextfn)\<string\> \| [Observer](https://firebase.google.com/docs/reference/js/util.observer#observer_interface)\<string\> | A function or observer object called with the unregistered FID. |

**Returns:**

[Unsubscribe](https://firebase.google.com/docs/reference/js/util#unsubscribe)

Unsubscribe function to stop listening.

### register(messaging, options)

Registers the app instance with FCM using its Firebase Installation ID (FID). The FID is delivered via the [onRegistered()](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) callback, not as a return value. Call this to establish an FID-based identity; once [onRegistered()](https://firebase.google.com/docs/reference/js/messaging#onregistered_f8a466e) provides an FID, instruct your backend to remove any legacy token previously associated with this instance. The backend send API supports FID as a target.

**Signature:**

    export declare function register(messaging: Messaging, options?: RegisterOptions): Promise<void>;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |
| options | [RegisterOptions](https://firebase.google.com/docs/reference/js/messaging.registeroptions#registeroptions_interface) | Optional. VAPID key and/or service worker registration (same as getToken). |

**Returns:**

Promise\<void\>

Promise that resolves when registration has been initiated; FID is delivered via onRegistered.

### unregister(messaging)

Unregisters the app instance from FCM by deleting its FID-based registration. On success, triggers [onUnregistered()](https://firebase.google.com/docs/reference/js/messaging#onunregistered_f8a466e) (if registered) with the unregistered FID.

**Signature:**

    export declare function unregister(messaging: Messaging): Promise<void>;

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| messaging | [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) | The [Messaging](https://firebase.google.com/docs/reference/js/messaging.messaging#messaging_interface) instance. |

**Returns:**

Promise\<void\>

## function()

### isSupported()

Checks if all required APIs exist in the browser.

**Signature:**

    export declare function isWindowSupported(): Promise<boolean>;

**Returns:**

Promise\<boolean\>

a Promise that resolves to a boolean.