Back to Firebase Js Sdk

@firebase/messaging

docs-devsite/messaging_.md

12.12.16.9 KB
Original Source

Project: /docs/reference/js/_project.yaml Book: /docs/reference/_book.yaml page_type: reference

{% comment %} DO NOT EDIT THIS FILE! This is generated by the JS SDK team, and any local changes will be overwritten. Changes should be made in the source code at https://github.com/firebase/firebase-js-sdk {% endcomment %}

@firebase/messaging

Functions

FunctionDescription
<b>function(app, ...)</b>
getMessaging(app)Retrieves a Firebase Cloud Messaging instance.
<b>function(messaging, ...)</b>
deleteToken(messaging)Deletes the registration token associated with this Messaging instance and unsubscribes the Messaging instance from the push subscription.
getToken(messaging, options)Subscribes the Messaging instance to push notifications. Returns a Firebase Cloud Messaging registration token that can be used to send push messages to that Messaging instance.<!-- -->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.
onMessage(messaging, nextOrObserver)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 <code>onMessage()</code> event is dispatched with the payload of the push message.
<b>function()</b>
isSupported()Checks if all required APIs exist in the browser.

Interfaces

InterfaceDescription
FcmOptionsOptions for features provided by the FCM SDK for Web. See WebpushFcmOptions<!-- -->.
GetTokenOptionsOptions for getToken()<!-- -->.
MessagePayloadMessage payload that contains the notification payload that is represented with NotificationPayload and the data payload that contains an arbitrary number of key-value pairs sent by developers through the Send API<!-- -->.
MessagingPublic interface of the Firebase Cloud Messaging SDK.
NotificationPayloadDisplay notification details. Details are sent through the Send API<!-- -->.

function(app, ...)

getMessaging(app) {:#getmessaging_cf608e1}

Retrieves a Firebase Cloud Messaging instance.

<b>Signature:</b>

typescript
export declare function getMessagingInWindow(app?: FirebaseApp): Messaging;

Parameters

ParameterTypeDescription
appFirebaseApp

<b>Returns:</b>

Messaging

The Firebase Cloud Messaging instance associated with the provided firebase app.

function(messaging, ...)

deleteToken(messaging) {:#deletetoken_3fae4b1}

Deletes the registration token associated with this Messaging instance and unsubscribes the Messaging instance from the push subscription.

<b>Signature:</b>

typescript
export declare function deleteToken(messaging: Messaging): Promise<boolean>;

Parameters

ParameterTypeDescription
messagingMessagingThe Messaging instance.

<b>Returns:</b>

Promise<boolean>

The promise resolves when the token has been successfully deleted.

getToken(messaging, options) {:#gettoken_b538f38}

Subscribes the Messaging instance to push notifications. Returns a Firebase Cloud Messaging registration token that can be used to send push messages to that Messaging instance.

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.

<b>Signature:</b>

typescript
export declare function getToken(messaging: Messaging, options?: GetTokenOptions): Promise<string>;

Parameters

ParameterTypeDescription
messagingMessagingThe Messaging instance.
optionsGetTokenOptionsProvides an optional vapid key and an optional service worker registration.

<b>Returns:</b>

Promise<string>

The promise resolves with an FCM registration token.

onMessage(messaging, nextOrObserver) {:#onmessage_b9887da}

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.

<b>Signature:</b>

typescript
export declare function onMessage(messaging: Messaging, nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>): Unsubscribe;

Parameters

ParameterTypeDescription
messagingMessagingThe Messaging instance.
nextOrObserverNextFn<!-- --><MessagePayload<!-- -->> | Observer<!-- --><MessagePayload<!-- -->>This function, or observer object with <code>next</code> defined, is called when a message is received and the user is currently viewing your page.

<b>Returns:</b>

Unsubscribe

To stop listening for messages execute this returned function.

function()

isSupported() {:#issupported}

Checks if all required APIs exist in the browser.

<b>Signature:</b>

typescript
export declare function isWindowSupported(): Promise<boolean>;

<b>Returns:</b>

Promise<boolean>

a Promise that resolves to a boolean.