Skip to main content

Token Management

Register tokens

Token registration can now be done using the callExtension method provided by the CometChat SDK. The token can be FCM token or APNs token and VoIP token.

This can be achieved using the code snippet below:

For FCM token:

// For FCM Token
CometChat.callExtension('push-notification', 'POST', 'v2/tokens', {
fcmToken: "fcm_token"
})
.then(response => {
// Success response
})
.catch(error => {
// Error occured
})

For APNs tokens:

// For APNs tokens
CometChat.callExtension('push-notification', 'POST', 'v2/tokens', {
apnsToken: "apns_token",
voipToken: "voip_token"
})
.then(response => {
// Success response
})
.catch(error => {
// Error occured
})

Get tokens

This provides a list of all the Push Notifications tokens that have been registered for the current user. The tokens are segregated based on the platform.

CometChat.callExtension('push-notification', 'GET', 'v2/tokens', null)
.then(response => {
// Success response
})
.catch(error => {
// Error occured
})

The response will be as follows:

{
"ios": [
"cLztQGbuPA91bG3rkRcgcQYvlKuTlWGmHC1RnCwrTrbyT0VF"
],
"web": [
"cLztQGbuPA91bG3rkRcgcQYvlKuTlWGmHC1RnxyzTrbyT0VF"
],
"android": [
"dLztQGbuPA91bG3rkRckcQYvlKuTlWGmHC1RnxyzTrbyT0VF"
]
}

Delete tokens

Token deletion is handled implicitly by the CometChat.logout() method. That is, once the user is logged out of the current CometChat session, his/her registered Push Notification token automatically gets deleted.

The same can be achieved explicitly by making a call to the extension using callExtension method as shown below. However, the token that is deleted belongs to the current session of the end-user by passing all=false as a parameter.

CometChat.callExtension('push-notification', 'DELETE', 'v2/tokens', {
all: false, // true when ALL the registered tokens for the logged-in user need to be deleted
})
.then((response) => {
// Success response
})
.catch((error) => {
// Error occured
});
Warning

All the tokens for the current user will be deleted if you pass all=true. This needs to be used with care as the other logins of the current user will stop receiving Push Notifications.