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.

Index

Methods

deleteToken

  • deleteToken(token: string): Promise<boolean>
  • To forceably stop a registration token from being used, delete it by calling this method.

    Parameters

    • token: string

      The token to delete.

    Returns Promise<boolean>

    The promise resolves when the token has been successfully deleted.

getToken

  • getToken(): Promise<string | null>
  • After calling requestPermission() you can call this method to get an FCM registration token that can be used to send push messages to this user.

    Returns Promise<string | null>

    The promise resolves if an FCM token can be retrieved. This method returns null if the current origin does not have permission to show notifications.

onMessage

  • onMessage(nextOrObserver: firebase.NextFn<any> | Observer<any>, error?: firebase.ErrorFn, completed?: firebase.CompleteFn): 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.

    NOTE: These events are dispatched when you have called setBackgroundMessageHandler() in your service worker.

    Parameters

    • nextOrObserver: firebase.NextFn<any> | Observer<any>
    • Optional error: firebase.ErrorFn
    • Optional completed: firebase.CompleteFn

    Returns firebase.Unsubscribe

    To stop listening for messages execute this returned function.

onTokenRefresh

  • onTokenRefresh(nextOrObserver: firebase.NextFn<any> | Observer<any>, error?: firebase.ErrorFn, completed?: firebase.CompleteFn): firebase.Unsubscribe
  • You should listen for token refreshes so your web app knows when FCM has invalidated your existing token and you need to call getToken() to get a new token.

    Parameters

    • nextOrObserver: firebase.NextFn<any> | Observer<any>
    • Optional error: firebase.ErrorFn
    • Optional completed: firebase.CompleteFn

    Returns firebase.Unsubscribe

    To stop listening for token refresh events execute this returned function.

requestPermission

  • requestPermission(): Promise<void>
  • Notification permissions are required to send a user push messages. Calling this method displays the permission dialog to the user and resolves if the permission is granted.

    Returns Promise<void>

    The promise resolves if permission is granted. Otherwise, the promise is rejected with an error.

setBackgroundMessageHandler

  • setBackgroundMessageHandler(callback: function): void
  • FCM directs push messages to your web page's onMessage() callback if the user currently has it open. Otherwise, it calls your callback passed into setBackgroundMessageHandler().

    Your callback should return a promise that, once resolved, has shown a notification.

    Parameters

    • callback: function

      The function to handle the push message.

        • (payload: any): Promise<any> | void
        • Parameters

          • payload: any

          Returns Promise<any> | void

    Returns void

usePublicVapidKey

  • usePublicVapidKey(b64PublicKey: string): void
  • Parameters

    • b64PublicKey: string

    Returns void

useServiceWorker

  • useServiceWorker(registration: ServiceWorkerRegistration): void
  • To use your own service worker for receiving push messages, you can pass in your service worker registration in this method.

    Parameters

    • registration: ServiceWorkerRegistration

      The service worker registration you wish to use for push messaging.

    Returns void

Send feedback about...

Need help? Visit our support page.