Enable mobile push notifications
If you use TalkJS inside a mobile app, you can send push notifications to notify users when they receive new messages. This guide shows you how to enable push notifications with TalkJS.
On your TalkJS dashboard, under Settings → Mobile push notifications, add your credentials for:
- Firebase Cloud Messaging (FCM)
- Apple Push Notification service (APNs)
To identify the device that should receive push notifications, obtain its device token (for APNs) or registration token (for FCM). How you obtain the user's device token or registration token depends on your app platform:
Once you have the token, you can register it with TalkJS.
After your app obtains a push token, set it on the user using the JavaScript Data API. You can do this once the user’s session is active.
In the JavaScript Data API, you store push tokens on the user's pushTokens property. Each key combines the push provider and the token ID, separated by a colon, in the format provider:token_id
. For example:
fcm:abc123
for Firebase Cloud Messagingapns:def456
for Apple Push Notification service
For example, to register a user's device for push notifications:
1session.currentUser.set({2 pushTokens: {3 'fcm:TOKEN': true,4 },5});
Replace TOKEN
with your user's actual device token or registration token.
Once registered, any message sent to this user in TalkJS triggers a push notification when the user is offline.
You can update or remove a push token at any time. For example, to unregister a token when the user logs out:
1session.currentUser.set({2 pushTokens: {3 'fcm:TOKEN': null,4 },5});
Setting a token value to null
unregisters that specific device. Setting pushTokens: null
removes all registered devices for the user.
TalkJS delivers raw push notifications, so your app can decided how to display or react to them.
You can optionally add code that captures the push notifications that TalkJS sends and performs actions based on them. For example, you could display them in your app's notification center, or open a specific page or chat when the notification is tapped.
After setup, TalkJS automatically sends push notifications whenever users receive new messages while offline.