firebase.messaging. Messaging
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.
Methods
deleteToken
deleteToken(token) returns firebase.Promise
To forceably stop a registration token from being used, delete it by calling this method.
Parameter |
|
|---|---|
|
token |
string The token to delete. Value must not be null. |
- Returns
-
firebase.PromiseThe promise resolves when the token has been successfully deleted.
getToken
getToken() returns firebase.Promise containing string
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
-
firebase.Promise containing stringThe 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) returns function()
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.
Parameter |
|
|---|---|
|
nextOrObserver |
(non-null function(non-null Object) or non-null Object) This function, or observer object with |
- Returns
-
function()To stop listening for messages execute this returned function.
onTokenRefresh
onTokenRefresh(nextOrObserver) returns function()
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.
Parameter |
|
|---|---|
|
nextOrObserver |
(non-null function(non-null Object) or non-null Object) This function, or observer object with |
- Returns
-
function()To stop listening for token refresh events execute this returned function.
requestPermission
requestPermission() returns firebase.Promise
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
-
firebase.PromiseThe promise resolves if permission is granted. Otherwise, the promise is rejected with an error.
setBackgroundMessageHandler
setBackgroundMessageHandler(callback)
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.
Parameter |
|
|---|---|
|
callback |
function(non-null Object) The function to handle the push message. Value must not be null. |
useServiceWorker
useServiceWorker(registration)
To use your own service worker for receiving push messages, you can pass in your service worker registration in this method.
Parameter |
|
|---|---|
|
registration |
ServiceWorkerRegistration The service worker registration you wish to use for push messaging. Value must not be null. |

