The Firebase Messaging service interface.

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

SeeSet Up a JavaScript Firebase Cloud Messaging Client App for a full guide on how to use theFirebase Messaging service.

Index

Methods

deleteToken

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

    ReturnsPromise<boolean>

    The promise resolves when the token has been successfully deleted.

getToken

  • getToken(options?: {serviceWorkerRegistration?:ServiceWorkerRegistration;vapidKey?:string }):Promise<string>
  • Subscribes the messaging instance to push notifications. Returns an FCM registration tokenthat 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.

    Parameters

    • Optional options:{serviceWorkerRegistration?:ServiceWorkerRegistration;vapidKey?:string }
      • Optional serviceWorkerRegistration?:ServiceWorkerRegistration

        The service worker registration for receiving pushmessaging. If the registration is not provided explicitly, you need to have afirebase-messaging-sw.js at your root location. See Access the registration tokenfor more details.

      • Optional vapidKey?:string

        The public server key provided to push services. It is used toauthenticate the push subscribers to receive push messages only from sending servers thathold the corresponding private key. If it is not provided, a default VAPID key is used. Notethat some push services (Chrome Push Service) require a non-default VAPID key. Therefore, itis recommended to generate and import a VAPID key for your project withConfigure Web Credentials with FCM.SeeThe Web Push Protocolfor details on web push services.}

    ReturnsPromise<string>

    The promise resolves with an FCM registration token.

onBackgroundMessage

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

    Parameters

    Returnsfirebase.Unsubscribe

    To stop listening for messages execute this returned function

onMessage

  • onMessage(nextOrObserverfirebase.NextFn<any> |Observer<any>):firebase.Unsubscribe
  • When a push message is received and the user is currently on a page for your origin, themessage is passed to the page and anonMessage() event is dispatched with the payload ofthe push message.

    Parameters

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

    Returnsfirebase.Unsubscribe

    To stop listening for messages execute this returned function.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2023-09-28 UTC.