On the Settings page you need to enter the following:
Set extension version
If you are setting it for the first time, Select V2 to start using the token-based version of the Push Notification extension.
If you already have an app using V1 and want to migrate your app to use V2, then Select V1 & V2 option. This ensures that the users viewing the older version of your app also receive Push Notifications.
Eventually, when all your users are on the latest version of your app, you can change this option to V2, thus turning off V1 (Topic-based) Push Notifications completely.
Select the platforms that you want to support
Select from Web, Android, Ionic, React Native, Flutter & iOS.
Notification payload settings
You can control if the notification key should be in the Payload or not. Learn more about the FCM Messages here.
Push payload message options
The maximum payload size supported by FCM and APNs for push notifications is approximately 4 KB. Due to the inclusion of CometChat’s message object, the payload size may exceed this limit, potentially leading to non-delivery of push notifications for certain messages. The options provided allow you to remove the sender’s metadata, receiver’s metadata, message metadata and trim the content of the text field.
The message metadata includes the outputs of the Thumbnail Generation, Image Moderation, and Smart Replies extensions. You may want to retain this metadata if you need to customize the notification displayed to the end user based on these outputs.
Notification Triggers
Select the triggers for sending Push Notifications. These triggers can be classified into 3 main categories:
Message Notifications
Call Notifications
Group Notifications
These are pretty self-explanatory and you can toggle them as per your requirement.
For Cordova & Ionic, there are numerous plugins available via NPM which can be used to set up push notifications for your apps like FCM Plugin and Push Plugin.
To setup Push Notification, you need to follow the steps mentioned in the Plugin’s Documentation.
At this point, you will have:
Separate apps created on the Firebase console. (For Web, Android and iOS).
Plugin setup completed as per the respective documentation.
This step assumes that you already have a React Native app setup with CometChat installed. Make sure that the CometChat object is initialized and user has been logged in.
On the success callback of user login, you can fetch the FCM Token and register it with the extension as shown below:
JavaScript
Report incorrect code
Copy
Ask AI
// Pseudo-code with async-await syntax// Using the FCM Pluginconst APP_ID = 'APP_ID';const REGION = 'REGION';const AUTH_KEY = 'AUTH_KEY';const UID = 'UID';const APP_SETTINGS = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(REGION) .build();try { // First initialize the app await CometChat.init(APP_ID, APP_SETTINGS); // Login the user await CometChat.login(UID, AUTH_KEY); // Login is successful so next step // Get the FCM device token // You should have imported the following in the file: // import { FCM } from '@ionic-native_fcm'; const FCM_TOKEN = await fcm.getToken(); // Register the token for Push Notifications (legacy) await CometChat.registerTokenForPushNotification(FCM_TOKEN);} catch (error) { // Handle errors gracefully}
Registration also needs to happen in case of token refresh as shown below:
JavaScript
Report incorrect code
Copy
Ask AI
// Pseudo-code// You should have imported the following in the file:// import { FCM } from '@ionic-native_fcm';try { // Listen to whether the token changes return fcm.onTokenRefresh(FCM_TOKEN => { await CometChat.registerTokenForPushNotification(FCM_TOKEN); }); // ...} catch(error) { // Handle errors gracefully}
// Pseudo-codeimport messaging from '@react-native-firebase_messaging';import { Alert } from 'react-native';// Implementation can be done in a life-cycle method or hookconst unsubscribe = messaging().onMessage(async (remoteMessage) => { Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));});
Converting push notification payload to message object
CometChat SDK provides a method CometChat.CometChatHelper.processMessage() to convert the message JSON to the corresponding object of TextMessage, MediaMessage,CustomMessage, Action or Call.
JavaScript
Report incorrect code
Copy
Ask AI
var processedMessage = CometChat.CometChatHelper.processMessage(JSON_MESSAGE);
Type of Attachment can be of the following the type: CometChatConstants.MESSAGE_TYPE_IMAGE CometChatConstants.MESSAGE_TYPE_VIDEO CometChatConstants.MESSAGE_TYPE_AUDIO CometChatConstants.MESSAGE_TYPE_FILE
Push Notification: Payload Sample for Text Message and Attachment/Media Message