The Firebase Messaging service interface.

Do not call this constructor directly. Instead, use firebase.messaging().

See Set Up a JavaScript Firebase Cloud Messaging Client App for a full guide on how to use the Firebase Messaging service.




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

    Returns Promise<boolean>

    The promise resolves when the token has been successfully deleted.


  • getToken ( options ? :  { serviceWorkerRegistration ?: ServiceWorkerRegistration ; vapidKey ?: string } ) : Promise < string >
  • Subscribes the messaging instance to push notifications. Returns an FCM registration token that can be used to send push messages to that messaging instance.

    If a 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.


    • Optional options: { serviceWorkerRegistration?: ServiceWorkerRegistration; vapidKey?: string }
      • Optional 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 Retrieve the current registration token for more details.

      • Optional vapidKey?: string

        The public server key provided to push services. It is used to authenticate the 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.}

    Returns Promise<string>

    The promise resolves with an FCM registration token.


  • onBackgroundMessage ( nextOrObserver firebase.NextFn < MessagePayload > | Observer < MessagePayload > ) : firebase.Unsubscribe
  • Called when a message is received while the app is in the background. An app is considered to be in the background if no active window is displayed.


    Returns firebase.Unsubscribe

    To stop listening for messages execute this returned function


  • onMessage ( nextOrObserver firebase.NextFn < any > | Observer < any > ) : firebase.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.


    • nextOrObserver: firebase.NextFn<any> | Observer<any>

    Returns firebase.Unsubscribe

    To stop listening for messages execute this returned function.