Stay organized with collections
Save and categorize content based on your preferences.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-09-28 UTC.
[null,null,["Last updated 2023-09-28 UTC."],[],[],null,["# Messaging | JavaScript SDK\n\n- [firebase](/docs/reference/js/v8/firebase).\n- [messaging](/docs/reference/js/v8/firebase.messaging).\n- Messaging\n==================================================================================================================\n\nThe Firebase Messaging service interface.\n\nDo not call this constructor directly. Instead, use\n[`firebase.messaging()`](/docs/reference/js/v8/firebase.messaging).\n\nSee [Set Up a JavaScript Firebase Cloud Messaging Client App](https://firebase.google.com/docs/cloud-messaging/js/client) for a full guide on how to use the\nFirebase Messaging service.\n\nIndex\n-----\n\n### Methods\n\n- [deleteToken](/docs/reference/js/v8/firebase.messaging.Messaging#deletetoken)\n- [getToken](/docs/reference/js/v8/firebase.messaging.Messaging#gettoken)\n- [onBackgroundMessage](/docs/reference/js/v8/firebase.messaging.Messaging#onbackgroundmessage)\n- [onMessage](/docs/reference/js/v8/firebase.messaging.Messaging#onmessage)\n\nMethods\n-------\n\n### deleteToken\n\n- deleteToken ( ) : Promise \\\u003c boolean \\\u003e\n- Deletes the registration token associated with this messaging instance and unsubscribes the\n messaging instance from the push subscription.\n\n #### Returns Promise\\\u003cboolean\\\u003e\n\n The promise resolves when the token has been successfully deleted.\n\n### getToken\n\n- getToken ( options ? : { serviceWorkerRegistration ?: ServiceWorkerRegistration ; vapidKey ?: string } ) : Promise \\\u003c string \\\u003e\n- Subscribes the messaging instance to push notifications. Returns an FCM registration token\n that can be used to send push messages to that messaging instance.\n\n If a notification permission isn't already granted, this method asks the user for permission.\n The returned promise rejects if the user does not allow the app to show notifications.\n\n #### Parameters\n\n -\n\n ##### Optional options: { serviceWorkerRegistration?: ServiceWorkerRegistration; vapidKey?: string }\n\n -\n\n ##### Optional serviceWorkerRegistration?: ServiceWorkerRegistration\n\n The service worker registration for receiving push\n messaging. If the registration is not provided explicitly, you need to have a\n `firebase-messaging-sw.js` at your root location. See\n [Access the registration token](https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token)\n for more details.\n -\n\n ##### Optional vapidKey?: string\n\n The public server key provided to push services. It is used to\n authenticate the push subscribers to receive push messages only from sending servers that\n hold the corresponding private key. If it is not provided, a default VAPID key is used. Note\n that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it\n is recommended to generate and import a VAPID key for your project with\n [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_with_fcm).\n See\n [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol)\n for details on web push services.}\n\n #### Returns Promise\\\u003cstring\\\u003e\n\n The promise resolves with an FCM registration token.\n\n### onBackgroundMessage\n\n- onBackgroundMessage ( nextOrObserver : firebase.NextFn \\\u003c [MessagePayload](/docs/reference/js/v8/firebase.messaging.MessagePayload) \\\u003e \\| Observer \\\u003c [MessagePayload](/docs/reference/js/v8/firebase.messaging.MessagePayload) \\\u003e ) : firebase.Unsubscribe\n- Called when a message is received while the app is in the background. An app is considered to\n be in the background if no active window is displayed.\n\n #### Parameters\n\n -\n\n ##### nextOrObserver: firebase.NextFn\\\u003c[MessagePayload](/docs/reference/js/v8/firebase.messaging.MessagePayload)\\\u003e \\| Observer\\\u003c[MessagePayload](/docs/reference/js/v8/firebase.messaging.MessagePayload)\\\u003e\n\n #### Returns firebase.Unsubscribe\n\n To stop listening for messages execute this returned function\n\n### onMessage\n\n- onMessage ( nextOrObserver : firebase.NextFn \\\u003c any \\\u003e \\| Observer \\\u003c any \\\u003e ) : firebase.Unsubscribe\n- When a push message is received and the user is currently on a page for your origin, the\n message is passed to the page and an `onMessage()` event is dispatched with the payload of\n the push message.\n\n #### Parameters\n\n -\n\n ##### nextOrObserver: firebase.NextFn\\\u003cany\\\u003e \\| Observer\\\u003cany\\\u003e\n\n #### Returns firebase.Unsubscribe\n\nTo stop listening for messages execute this returned function."]]
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.