@firebase/messaging

  • Version 0.13.0
  • Published
  • 1.14 MB
  • 6 dependencies
  • Apache-2.0 license

Install

npm i @firebase/messaging
yarn add @firebase/messaging
pnpm add @firebase/messaging

Overview

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

Index

Functions

function deleteToken

deleteToken: (messaging: Messaging) => Promise<boolean>;
  • Deletes the registration token associated with this Messaging instance and unsubscribes the Messaging instance from the push subscription.

    If there is no legacy registration token but the client has FID-based registration metadata (from register), this deletes that registration on the server, clears local metadata, and invokes onUnregistered with the removed FID when successful.

    Parameter messaging

    The Messaging instance.

    Returns

    The promise resolves when the token has been successfully deleted.

    Modifiers

    • @public

    Deprecated

    Use onUnregistered to observe when the client is no longer registered and update your backend accordingly, instead of explicitly deleting the registration token with this API.

function getMessaging

getMessaging: (app?: FirebaseApp) => Messaging;
  • Retrieves a Firebase Cloud Messaging instance.

    Returns

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

    Modifiers

    • @public

function getToken

getToken: (messaging: Messaging, options?: GetTokenOptions) => Promise<string>;
  • Subscribes the Messaging instance to push notifications. Returns a Firebase Cloud Messaging registration token that can be used to send push messages to that Messaging 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.

    Parameter messaging

    The Messaging instance.

    Parameter options

    Provides an optional vapid key and an optional service worker registration.

    Returns

    The promise resolves with an FCM registration token.

    Modifiers

    • @public

    Deprecated

    Use register together with onRegistered for Firebase Installation ID-based messaging instead of retrieving an FCM registration token with this API.

function isSupported

isSupported: () => Promise<boolean>;
  • Checks if all required APIs exist in the browser.

    Returns

    a Promise that resolves to a boolean.

    Modifiers

    • @public

function onMessage

onMessage: (
messaging: Messaging,
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
) => Unsubscribe;
  • 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.

    Parameter messaging

    The Messaging instance.

    Parameter nextOrObserver

    This function, or observer object with next defined, is called when a message is received and the user is currently viewing your page.

    Returns

    To stop listening for messages execute this returned function.

    Modifiers

    • @public

function onRegistered

onRegistered: (
messaging: Messaging,
nextOrObserver: NextFn<string> | Observer<string>
) => Unsubscribe;
  • 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, instruct your backend to remove any legacy token for this instance.

    Parameter messaging

    The Messaging instance.

    Parameter nextOrObserver

    A function or observer object called when an FID is registered.

    Returns

    Unsubscribe function to stop listening.

    Modifiers

    • @public

function onUnregistered

onUnregistered: (
messaging: Messaging,
nextOrObserver: NextFn<string> | Observer<string>
) => Unsubscribe;
  • 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.

    Parameter messaging

    The Messaging instance.

    Parameter nextOrObserver

    A function or observer object called with the unregistered FID.

    Returns

    Unsubscribe function to stop listening.

    Modifiers

    • @public

function register

register: (messaging: Messaging, options?: RegisterOptions) => Promise<void>;
  • Registers the app instance with FCM using its Firebase Installation ID (FID). The FID is delivered via the onRegistered callback, not as a return value. Call this to establish an FID-based identity; once onRegistered 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.

    Parameter messaging

    The Messaging instance.

    Parameter options

    Optional. VAPID key and/or service worker registration (same as getToken).

    Returns

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

    Modifiers

    • @public

function unregister

unregister: (messaging: Messaging) => Promise<void>;
  • Unregisters the app instance from FCM by deleting its FID-based registration. On success, triggers onUnregistered (if registered) with the unregistered FID.

    Parameter messaging

    The Messaging instance.

    Modifiers

    • @public

Interfaces

interface FcmOptions

interface FcmOptions {}
  • Options for features provided by the FCM SDK for Web. See WebpushFcmOptions.

    Modifiers

    • @public

property analyticsLabel

analyticsLabel?: string;
  • The label associated with the message's analytics data.

link?: string;
  • The link to open when the user clicks on the notification.

interface GetTokenOptions

interface GetTokenOptions {}

property serviceWorkerRegistration

serviceWorkerRegistration?: ServiceWorkerRegistration;
  • The service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See Access the registration token for more details.

property vapidKey

vapidKey?: string;
  • The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with Configure Web Credentials with FCM. See The Web Push Protocol for details on web push services.

interface MessagePayload

interface MessagePayload {}
  • Message payload that contains the notification payload that is represented with NotificationPayload and the data payload that contains an arbitrary number of key-value pairs sent by developers through the Send API.

    Modifiers

    • @public

property collapseKey

collapseKey: string;

property data

data?: {
[key: string]: string;
};
  • Arbitrary key/value payload.

property fcmOptions

fcmOptions?: FcmOptions;

property from

from: string;
  • The sender of this message.

property messageId

messageId: string;
  • The message ID of a message.

property notification

notification?: NotificationPayload;

interface Messaging

interface Messaging {}
  • Public interface of the Firebase Cloud Messaging SDK.

    Modifiers

    • @public

property app

app: FirebaseApp;

interface NotificationPayload

interface NotificationPayload {}
  • Display notification details. Details are sent through the Send API.

    Modifiers

    • @public

property body

body?: string;
  • The notification's body text.

property icon

icon?: string;
  • The URL to use for the notification's icon. If you don't send this key in the request, FCM displays the launcher icon specified in your app manifest.

property image

image?: string;
  • The URL of an image that is downloaded on the device and displayed in the notification.

property title

title?: string;
  • The notification's title.

interface RegisterOptions

interface RegisterOptions {}
  • Options for register. Same shape as GetTokenOptions for SW and VAPID configuration.

    Modifiers

    • @public

property serviceWorkerRegistration

serviceWorkerRegistration?: ServiceWorkerRegistration;

property vapidKey

vapidKey?: string;

Package Files (1)

Dependencies (6)

Dev Dependencies (6)

Peer Dependencies (1)

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@firebase/messaging.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@firebase/messaging)
  • HTML
    <a href="https://www.jsdocs.io/package/@firebase/messaging"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>