NotificationHandler

This object is used to add custom handlers for specific events relating to push notifications.

Example

1import * as TalkRn from '@talkjs/react-native';
2
3const notificationHandler = TalkRn.getNotificationHandler();
4notificationHandler.onTokenRefresh((oldToken, newToken) => {
5 console.log(oldToken);
6 console.log(newToken);
7});
8
9notificationHandler.onNotificationPressed((notification) => {
10 console.log(notification);
11});

Properties

FieldTypeDescription
deviceToken
DeviceTokenContains information about the currently valid push notification device id

Methods

onTokenRefresh

1onTokenRefresh(handler: TokenRefreshHandler): void;

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

You can call this method multiple times with different handlers. They will each be called when the event occurs.

Parameters

NameTypeDescription
handler RequiredTokenRefreshHandlerThe function you want to be called when the push notification token has been refreshed.

Returns

void

onNotificationPressed

1onNotificationPressed(handler: NotificationPressedHandler): void;

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

You can call this method multiple times with different handlers. They will each be called when the event occurs.

Parameters

NameTypeDescription
handler RequiredNotificationPressedHandlerThe function you want to be called when the user clicks a notification.

Returns

void

Used by


Type Definitions

DeviceToken

Example

1{
2 provider: 'apns',
3 pushRegistrationId: '03df25c8345d460bcdad7802d2vf6fc1341e97283bf75cc993eb6dca835ea2e2f'
4}

Properties

FieldTypeDescription
providerfcm | apnsSpecifies whether the token belongs to Firebase (fcm) or Apple Push Notification service (apns).
pushRegistrationIdstringThe push notification device token that has been registered for that device on either Firebase or Apple Push Notification service.

TokenRefreshHandler

1type TokenRefreshHandler = (
2 oldToken: DeviceToken,
3 newToken: DeviceToken
4) => void;

Parameters

NameTypeDescription
oldTokenDeviceTokenThe previous device token object.
newTokenDeviceTokenThe current device token object.

Returns

void

NotificationPressedEvent

Example

1{
2 id: "128859FC-91AF-4BFC-AB65-ACF817F55E9F",
3 title: "Sebastian",
4 body: "rklqjtew;",
5 data: {
6 conversationId: "ff45c61343229e431dca",
7 messageId: "msg_5V20MNImL2Np01SlwSBDP3",
8 senderId: "4321567890",
9 timestamp: 1676040892409,
10 }
11}

Properties

FieldTypeDescription
idstringThe notification ID.
isForegroundEventbooleanIndicates 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 | undefinedThe text body of the notification.
title (optional)string | undefinedThe notification title.
data (optional)NotificationData | undefinedExtra data providing TalkJS specific information associated with the notification.

NotificationData

Example

1{
2 conversationId: "ff45c61343229e431dca",
3 messageId: "msg_5V20MNImL2Np01SlwSBDP3",
4 senderId: "4321567890",
5 timestamp: 1676040892409,
6}

Properties

FieldTypeDescription
senderIdstringThe sender's ID.
conversationIdstringThe ID of the conversation where this message was sent.
messageIdstringThe message ID
timestampnumberUnix timestamp indicating when the message and subsequently the notification was sent

NotificationPressedHandler

1type NotificationPressedHandler = (
2 notification: NotificationPressedEvent
3) => void;

Parameters

NameTypeDescription
notificationNotificationPressedEventAn object containing information about the notification the user just clicked.