<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@firebase/messaging](./messaging.md) &gt; [onMessage](./messaging.onmessage.md)

## onMessage() 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.

<b>Signature:</b>

```typescript
export declare function onMessage(messaging: FirebaseMessaging, nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>): Unsubscribe;
```

## Parameters

|  Parameter | Type | Description |
|  --- | --- | --- |
|  messaging | [FirebaseMessaging](./messaging.firebasemessaging.md) | The <code>FirebaseMessaging</code> instance. |
|  nextOrObserver | NextFn&lt;[MessagePayload](./messaging.messagepayload.md)<!-- -->&gt; \| Observer&lt;[MessagePayload](./messaging.messagepayload.md)<!-- -->&gt; | 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.

