Subscribes the messaging instance to push notifications. Returns an FCM registration token
that 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.
The service worker registration for receiving push
messaging. If the registration is not provided explicitly, you need to have a
firebase-messaging-sw.js at your root location. See
Access the registration token
for more details.
Optional vapidKey?: string
The public server key provided to push services. It is used to
authenticate the push subscribers to receive push messages only from sending servers that
hold the corresponding private key. If it is not provided, a default VAPID key is used. Note
that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it
is recommended to generate and import a VAPID key for your project with
Configure Web Credentials with FCM.
See
The Web Push Protocol
for details on web push services.}
Returns Promise<string>
The promise resolves with an FCM registration token.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-09-28 UTC."],[],[],null,["- [firebase](/docs/reference/js/v8/firebase).\n- [messaging](/docs/reference/js/v8/firebase.messaging).\n- Messaging \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\nMethods\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\ndeleteToken\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\ngetToken\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 Optional options: { serviceWorkerRegistration?: ServiceWorkerRegistration; vapidKey?: string }\n -\n\n Optional serviceWorkerRegistration?: ServiceWorkerRegistration \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 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\nonBackgroundMessage\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 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\nonMessage\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 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.