Skip to main content

Integration

Enable Push notifications

Image
  1. Sign in to the CometChat dashboard.
  2. In the right navigation pane, choose "Notifications" from the "Extend" category.
  3. Enable the Push notifications feature.
  4. Continue to configure Push notifications by clicking on "Configure".

Add Providers

Firebase Cloud Messaging (FCM) and Apple Push Notification Service (APNS) are the two primary supported providers for sending push notifications.

Add FCM credentials

Pre-requisite

Generate a service account key for your Firebase application by navigating to the "Service accounts" section within the "Project settings" of the Firebase Cloud Messaging Dashboard. Generate new private key and download the JSON file. This will be required in the next steps.

Image

Add credentials

  1. Select the "+ Add Credentials" button.
  2. In the dialogue that appears, provide a unique, memorable identifier for your provider.
  3. Upload the service account JSON file you previously acquired.
  4. Specify whether the push payload should include the "notification" key. Additional information on this configuration is available in FCM's documentation - About FCM messages.

Similarly, you can add multiple FCM Credentials in case you have multiple FCM projects for your apps.

Image

Add APNS credentials

Pre-requisite

  1. To generate a .p8 key file, go to Apple developer account, then select "Certificates, IDs & Profiles".
  2. Select Keys and click on the "+" button to add a new key.
  3. In the new key page, type in your key name and check the Apple Push Notification service (APNs) box, then click "Continue" and click "Register".
  4. Then proceed to download the key file by clicking Download.
  5. Make note of the Key ID, Team ID and your Bundle ID. These are required in the next steps.

Additional information on this configuration is available in Apple's documentation - Create a private key to access a service

Add credentials

  1. Select the "+ Add Credentials" button.
  2. Enable the toggle if your app is in the Production. For apps under development, the toggle has to be disabled.
  3. In the dialogue that appears, provide a unique, memorable identifier for your provider.
  4. Store the Key ID, Team ID, Bundle ID for your app.
  5. Upload the .p8 file
  6. Enable "Include content-available" if you want to receive background notifications. However, this is not recommended as the background notifications are throttled. Additional information is available in Apple's documentation - Pushing background updates to your App
  7. Enable "Include mutable-content" if you want to modify the notification before it is displayed to the user. Additional information is available in Apple's documentation - Modifying content on newly delivered notifications

Similarly, you can add multiple APNS Credentials in case you have multiple apps with different Bundle IDs.

Image

Tokens management

Pre-requisite

To successfully integrate push notifications in your app for any particular platform, you need to make sure you are using the appropriate version of the CometChat SDK that supports push notifications.

  1. Android SDK version 4.0.9 and above
  2. iOS SDK version 4.0.51 and above
  3. Web SDK version 4.0.8 and above
  4. React Native SDK version 4.0.10 and above
  5. Ionic Cordova SDK version 4.0.8 and above
  6. Flutter SDK version 4.0.15 and above

Register push token after login

Push tokens, obtained from the APIs or SDKs provided by the platforms or frameworks in use, must be registered on behalf of the logged in user with the CometChat backend. For this purpose, CometChat SDKs v4+ offer the following functions:

Push token registration should be completed in two scenarios:

  1. Following the success of CometChat.login() & receiving user's permission to receiver Push notifications.
  2. When a refresh token becomes available.

To register a token, use the CometChatNotifications.registerPushToken() method from the SDK. This method accepts the following parameters.

ParameterTypeDescription
pushTokenString

The pushToken can contain:

  • Firebase (FCM) token
  • Device token (iOS only)
  • VoIP token (iOS only)
platformString

The platform can take the following values:

  • PushPlatforms.FCM_ANDROID
  • PushPlatforms.FCM_FLUTTER_ANDROID
  • PushPlatforms.FCM_FLUTTER_IOS
  • PushPlatforms.APNS_FLUTTER_DEVICE
  • PushPlatforms.APNS_FLUTTER_VOIP
  • PushPlatforms.FCM_IOS
  • PushPlatforms.APNS_IOS_DEVICE
  • PushPlatforms.APNS_IOS_VOIP
  • PushPlatforms.FCM_WEB
  • PushPlatforms.FCM_REACT_NATIVE_ANDROID
  • PushPlatforms.FCM_REACT_NATIVE_IOS
  • PushPlatforms.APNS_REACT_NATIVE_DEVICE
  • PushPlatforms.APNS_REACT_NATIVE_VOIP
  • PushPlatforms.FCM_IONIC_CORDOVA_ANDROID
  • PushPlatforms.FCM_IONIC_CORDOVA_IOS
  • PushPlatforms.APNS_IONIC_CORDOVA_DEVICE
  • PushPlatforms.APNS_IONIC_CORDOVA_VOIP
providerIdString

The providerId should match with:

  • Any one of the FCM provider identifiers in case of an FCM token.
  • Any one of the APNS provider identifiers in case of Device or VoIP tokens.
// This is applicable for web, React native, Ionic cordova

// CometChat.init() success.
// CometChat.login() success.
// User has granted permission to display push notifications.

// For web
CometChatNotifications.registerPushToken(
pushToken,
CometChatNotifications.PushPlatforms.FCM_WEB,
'fcm-provider-2'
)
.then((payload) => {
console.log('Token registration successful');
})
.catch((err) => {
console.log('Token registration failed:', err);
});

// For React Native Android
CometChatNotifications.registerPushToken(
pushToken,
CometChatNotifications.PushPlatforms.FCM_REACT_NATIVE_ANDROID,
'fcm-provider-2'
)
.then((payload) => {
console.log('Token registration successful');
})
.catch((err) => {
console.log('Token registration failed:', err);
});

// For React Native iOS
CometChatNotifications.registerPushToken(
pushToken,
CometChatNotifications.PushPlatforms.FCM_REACT_NATIVE_IOS,
'fcm-provider-2'
)
.then((payload) => {
console.log('Token registration successful');
})
.catch((err) => {
console.log('Token registration failed:', err);
});

// For Ionic cordova Android
CometChatNotifications.registerPushToken(
pushToken,
CometChatNotifications.PushPlatforms.FCM_IONIC_CORDOVA_ANDROID,
'fcm-provider-2'
)
.then((payload) => {
console.log('Token registration successful');
})
.catch((err) => {
console.log('Token registration failed:', err);
});

// For Ionic cordova iOS
CometChatNotifications.registerPushToken(
pushToken,
CometChatNotifications.PushPlatforms.FCM_IONIC_CORDOVA_IOS,
'fcm-provider-2'
)
.then((payload) => {
console.log('Token registration successful');
})
.catch((err) => {
console.log('Token registration failed:', err);
});

// Similary, use this method to register refresh token.

Unregister push token before logout

Typically, push token unregistration should occur prior to user logout, using the CometChat.logout() method.

For token unregistration, use the CometChatNotifications.unregisterPushToken() method provided by the SDKs.

// This is applicable for web, React native, Ionic cordova
await CometChatNotifications.unregisterPushToken();

// Followed by CometChat.logout();

Handle incoming Push notifications

Push notifications should be managed primarily when the app is in the background or in a terminated state.

For web and mobile applications, the Firebase Cloud Messaging (FCM) SDK offers handler functions to receive Push Notifications.

For iOS applications, the FCM SDK can be used as described previously, or alternatively, PushKit can be implemented for enhanced integration and management of chat and call (VoIP) notifications.

The push payload delivered to the user's device includes the following information, which can be customized to suit the desired notification style:

{
// Notification details
"title": "Andrew Joseph", // The title of the notification to be displayed
"body": "Hello!", // The body of the notification to be displayed

// Sender's details
"sender": "cometchat-uid-1", // UID of the user who sent the said message.
"senderName": "Andrew Joseph", // Name of the user who sent the said message.
"senderAvatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp", // Avatar URL of the user.

// Receiver's details
"receiver": "cometchat-uid-2", // UID or GUID of the receiver.
"receiverName": "George Alan", // Name of the user or group.
"receiverAvatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp", // Avatar URL of the receiver.
"receiverType": "user", // Values can be "user" or "group"

// Message details
"tag": "123", // The ID of the said message that can be used as the ID of the notification to be displayed.
"conversationId": "cometchat-uid-1_user_cometchat-uid-2", // The ID of the conversation that the said message belongs to.
"type": "chat", // Values can be "call" or "chat". If this is "call", the below details will be available.

// Call details
"callAction": "initiated", // Values can be "initiated" or "cancelled" or "unanswered" or "ongoing" or "rejected" or "ended" or "busy"
"sessionId": "v1.123.aik2", // The unique sessionId of the said call that can be used as unique identifier in CallKit or ConnectionService.
"callType": "audio", // Values can be "audio" or "video"
}

Sample apps

Check out our sample apps for understanding the implementation.

React - Enhanced Push notifications sample app

Push notifications sample app for the web

View on Github

React Native - Enhanced Push notifications sample app

Push notifications sample app for React Native

View on Github

iOS - Enhanced Push notifications sample app

Push notifications sample app for iOS

View on Github

Android - Enhanced Push notifications sample app

Push notifications sample app for Android

View on Github

Flutter - Enhanced Push notifications sample app

Push notifications sample app for Flutter

View on Github