---
url: https://talkjs.com/docs/UI_Components/React_Native/Object_Types/NotificationHandler
---

# NotificationHandler

Handle push notification events and device token updates.

Ask a question Copy for LLM [View as Markdown](/docs/UI_Components/React_Native/Object_Types/NotificationHandler.md)
This object is used to add custom handlers for specific events relating to push notifications.

## Example

```typescript
import * as TalkRn from '@talkjs/react-native';

const notificationHandler = TalkRn.getNotificationHandler();
notificationHandler.onTokenRefresh((oldToken, newToken) => {
  console.log(oldToken);
  console.log(newToken);
});

notificationHandler.onNotificationPressed((notification) => {
  console.log(notification);
});
```

## Properties

| Field | Type | Description |
| --- | --- | --- |
| ---

**device Token**

--- | [`DeviceToken`](#devicetoken) | Contains information about the currently valid push notification device id |

## Methods

### onTokenRefresh

```typescript
onTokenRefresh(handler: TokenRefreshHandler): void;
```

Specifies a handler that will be called when a new device push notification token has been generated.

#### Parameters

| Name | Type | Description |
| --- | --- | --- |
| **handler** Required | [`TokenRefreshHandler`](#tokenrefreshhandler) | The function you want to be called when the push notification token has been refreshed. |

#### Returns

`void`

### onNotificationPressed

```typescript
onNotificationPressed(handler: NotificationPressedHandler): void;
```

Specifies a handler that will be called when the user clicks on a push notification that is currently on display.

#### Parameters

| Name | Type | Description |
| --- | --- | --- |
| **handler** Required | [`NotificationPressedHandler`](#notificationpressedhandler) | The function you want to be called when the user clicks a notification. |

#### Returns

`void`

## Used by

- [`getNotificationHandler`](/docs/UI_Components/React_Native/API/getNotificationHandler/)
- [`registerPushNotificationHandlers`](/docs/UI_Components/React_Native/API/registerPushNotificationHandlers/)

## Type Definitions

### DeviceToken

#### Example

```typescript
{
    provider: 'apns',
    pushRegistrationId: '03df25c8345d460bcdad7802d2vf6fc1341e97283bf75cc993eb6dca835ea2e2f'
}
```

#### Properties

| Field | Type | Description |
| --- | --- | --- |
| **provider** | `fcm` \| `apns` | Specifies whether the token belongs to Firebase (`fcm`) or Apple Push Notification service (`apns`). |
| **pushRegistrationId** | `string` | The push notification device token that has been registered for that device on either Firebase or Apple Push Notification service. |

### TokenRefreshHandler

```typescript
type TokenRefreshHandler = (
  oldToken: DeviceToken,
  newToken: DeviceToken
) => void;
```

#### Parameters

| Name | Type | Description |
| --- | --- | --- |
| **oldToken** | [`DeviceToken`](#devicetoken) | The previous device token object. |
| **newToken** | [`DeviceToken`](#devicetoken) | The current device token object. |

#### Returns

`void`

### NotificationPressedEvent

#### Example

```typescript
{
    id: "128859FC-91AF-4BFC-AB65-ACF817F55E9F",
    title: "Sebastian",
    body: "rklqjtew;",
    data: {
        conversationId: "ff45c61343229e431dca",
        messageId: "msg_5V20MNImL2Np01SlwSBDP3",
        senderId: "4321567890",
        timestamp: 1676040892409,
    }
}
```

#### Properties

| Field | Type | Description |
| --- | --- | --- |
| **id** | `string` | The notification ID. |
| **isForegroundEvent** | `boolean` | Indicates whether the event occured while the app was in the foreground or background. In iOS, the background event handler is never called. If the app is in the background, it is opened and then the foreground event handler is called. Hence on iOS this is always `true` |
| **body** *(optional)* | `string` \| `undefined` | The text body of the notification. |
| **title** *(optional)* | `string` \| `undefined` | The notification title. |
| **data** *(optional)* | [`NotificationData`](#notificationdata) \| `undefined` | Extra data providing TalkJS specific information associated with the notification. |

### NotificationData

#### Example

```typescript
{
    conversationId: "ff45c61343229e431dca",
    messageId: "msg_5V20MNImL2Np01SlwSBDP3",
    senderId: "4321567890",
    timestamp: 1676040892409,
}
```

#### Properties

| Field | Type | Description |
| --- | --- | --- |
| **senderId** | `string` | The sender's ID. |
| **conversationId** | `string` | The ID of the conversation where this message was sent. |
| **messageId** | `string` | The message ID |
| **timestamp** | `number` | Unix timestamp indicating when the message and subsequently the notification was sent |

### NotificationPressedHandler

```typescript
type NotificationPressedHandler = (
  notification: NotificationPressedEvent
) => void;
```

#### Parameters

| Name | Type | Description |
| --- | --- | --- |
| **notification** | [`NotificationPressedEvent`](#notificationpressedevent) | An object containing information about the notification the user just clicked. |