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 forcibly 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>
  • Subscribes the user to push notifications and returns an FCM registration token that can be used to send push messages to the user.

    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.

    Returns Promise<string>

    The promise resolves with the FCM token string.

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>
  • deprecated

    Use Notification.requestPermission() instead. https://developer.mozilla.org/en-US/docs/Web/API/Notification/requestPermission

    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. It is not necessary to call this method, as getToken() will do this automatically if required.

    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