Common Preferences
Login to CometChat dashboard and navigate to the Notifications section.
Under Preferences tab, set the event preferences at the CometChat app-level and decide if users have the capability to override these settings. When “Override” toggle is enabled, users will have the capability to modify the default value that has been set.
Group preferences
Dashboard configuration
As the name suggests, these preferences help you to configure Notifications for events generated in group conversations.
| Categories | Events | Available preferences | Can user override? |
|---|
| Conversations | New messages | • Don’t notify • Notify for all messages (Default) • Notify for messages with mentions | • Yes (Default) • No |
| New replies | • Don’t notify • Notify for all replies (Default) • Notify for replies with mentions | • Yes (Default) • No |
| Message actions | Message is edited | • Don’t notify • Notify (Default) | • Yes • No (Default) |
| Message is deleted | • Don’t notify • Notify (Default) | • Yes • No (Default) |
| Message receives a reaction | • Don’t notify • Notify for reactions received on all messages • Notify for reactions received on own messages (Default) | • Yes (Default) • No |
| Group actions | A member leaves | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A new member is added | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A new member joins | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A member is kicked | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A member is banned | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A member is unbanned | • Don’t notify (Default) • Notify | • Yes (Default) • No |
| A member’s scope changes | • Don’t notify (Default) • Notify | • Yes (Default) • No |
Regarding Message edited & Message deleted eventsPush notifications should be triggered for the message edited and message deleted events in order to retract the notification displaying the original message. Turning them off is not recommended.
Client-side implementation
1. Fetch group preferences
CometChatNotifications.fetchPreferences() method retrieves the notification preferences as an instance of NotificationPreferences class. If the user has not configured any preferences, the default preferences defined by the CometChat administrator via the dashboard will be returned.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
const preferences = await CometChatNotifications.fetchPreferences();
// Display Group preferences
const groupPreferences = preferences.getGroupPreferences();
const groupMessagesPreference = groupPreferences.getMessagesPreference();
const groupRepliesPreference = groupPreferences.getRepliesPreference();
const groupReactionsPreference = groupPreferences.getReactionsPreference();
const memberLeftPreference = groupPreferences.getMemberLeftPreference();
const memberAddedPreference = groupPreferences.getMemberAddedPreference();
const memberJoinedPreference = groupPreferences.getMemberJoinedPreference();
const memberKickedPreference = groupPreferences.getMemberKickedPreference();
const memberBannedPreference = groupPreferences.getMemberBannedPreference();
const memberUnbannedPreference = groupPreferences.getMemberUnbannedPreference();
const memberScopeChangedPreference =
groupPreferences.getMemberScopeChangedPreference();
CometChatNotifications.fetchPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Display group preferences
GroupPreferences groupPreferences = notificationPreferences.getGroupPreferences();
MessagesOptions groupMessagesPreference = groupPreferences.getMessagesPreference();
RepliesOptions groupRepliesPreference = groupPreferences.getRepliesPreference();
ReactionsOptions groupReactionsPreference = groupPreferences.getReactionsPreference();
MemberActionsOptions memberAddedPreference = groupPreferences.getMemberAddedPreference();
MemberActionsOptions memberLeftPreference = groupPreferences.getMemberLeftPreference();
MemberActionsOptions memberJoinedPreference = groupPreferences.getMemberJoinedPreference();
MemberActionsOptions memberKickedPreference = groupPreferences.getMemberKickedPreference();
MemberActionsOptions memberBannedPreference = groupPreferences.getMemberBannedPreference();
MemberActionsOptions memberUnbannedPreference = groupPreferences.getMemberUnbannedPreference();
MemberActionsOptions memberScopeChangedPreference = groupPreferences.getMemberScopeChangedPreference();
}
@Override
public void onError(CometChatException e) {
// Something went wrong while fetching notification preferences
}
});
CometChatNotifications.fetchPreferences { notificationPreferences in
// Display group preferences
let groupPreferences = notificationPreferences.groupPreferences;
let groupMessages = groupPreferences?.messagesPreference;
let groupReplies = groupPreferences?.repliesPreference;
let groupReactions = groupPreferences?.reactionsPreference;
let left = groupPreferences?.memberLeftPreference;
let added = groupPreferences?.memberAddedPreference;
let joined = groupPreferences?.memberJoinedPreference;
let kicked = groupPreferences?.memberKickedPreference;
let banned = groupPreferences?.memberBannedPreference;
let unbanned = groupPreferences?.memberUnbannedPreference;
let scope = groupPreferences?.memberScopeChangedPreference;
} onError: { error in
// Something went wrong while fetching notification preferences.
print("fetchPreferences: \(error.errorCode) \(error.errorDescription)");
}
CometChatNotifications.fetchPreferences(
onSuccess: (notificationPreferences) {
// Display group preferences
GroupPreferences? groupPreferences = notificationPreferences.groupPreferences;
MessagesOptions? messagesPreference = groupPreferences?.messages;
RepliesOptions? repliesPreference = groupPreferences?.replies;
ReactionsOptions? reactionsPreference = groupPreferences?.reactions;
MemberActionsOptions? memberAddedPreference = groupPreferences?.memberAdded;
MemberActionsOptions? memberJoinedPreference = groupPreferences?.memberJoined;
MemberActionsOptions? memberLeftPreference = groupPreferences?.memberLeft;
MemberActionsOptions? memberKickedPreference = groupPreferences?.memberKicked;
MemberActionsOptions? memberBannedPreference = groupPreferences?.memberBanned;
MemberActionsOptions? memberUnbannedPreference = groupPreferences?.memberUnbanned;
MemberActionsOptions? memberScopeChangedPreference = groupPreferences?.memberScopeChanged;
},
onError: (e) {
debugPrint("fetchPreferences:error ${e.toString()}");
});
2. Update group preferences
CometChatNotifications.updatePreferences() method is used to update a user’s notification preferences. The “override” toggle defined in the dashboard is crucial when updating preferences. If any preference is non-overridable, the method doesn’t generate an error; it instead returns the NotificationPreferences object with the updated values where overrides are allowed.
This functionality can be beneficial for temporarily superseding certain user preferences to ensure notifications for a specific event are delivered. Nonetheless, it is advisable to use this approach temporarily to avoid confusing users with unexpected changes to their notification settings.
It is unnecessary to specify all values; only set and save the preferences that have been changed.
Since the user is performing this action, enums have values as SUBSCRIBE or DONT_SUBSCRIBE. It is equivalent to “Notify” and “Don’t notify” respectively, from the dashboard preferences.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
const updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
const groupPreferences = new GroupPreferences();
// Change group preferences
groupPreferences.setMessagesPreference(MessagesOptions.DONT_SUBSCRIBE);
groupPreferences.setRepliesPreference(RepliesOptions.DONT_SUBSCRIBE);
groupPreferences.setReactionsPreference(ReactionsOptions.DONT_SUBSCRIBE);
groupPreferences.setMemberAddedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberKickedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberJoinedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberLeftPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberBannedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberUnbannedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberScopeChangedPreference(
MemberActionsOptions.SUBSCRIBE
);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setGroupPreferences(groupPreferences);
// Update the preferences and receive the udpated copy.
const preferences = await CometChatNotifications.updatePreferences(
updatedPreferences
);
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
NotificationPreferences updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
GroupPreferences groupPreferences = new GroupPreferences();
// Change group preferences
groupPreferences.setMessagesPreference(MessagesOptions.DONT_SUBSCRIBE);
groupPreferences.setRepliesPreference(RepliesOptions.DONT_SUBSCRIBE);
groupPreferences.setReactionsPreference(ReactionsOptions.DONT_SUBSCRIBE);
groupPreferences.setMemberAddedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberKickedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberJoinedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberLeftPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberBannedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberUnbannedPreference(MemberActionsOptions.SUBSCRIBE);
groupPreferences.setMemberScopeChangedPreference(MemberActionsOptions.SUBSCRIBE);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setGroupPreferences(groupPreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences, new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Updated notificationPreferences
}
@Override
public void onError(CometChatException e) {
// Something went wrong
}
});
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
let updatedPreferences = CometChatNotifications.NotificationPreferences();
// Instantiate the preferences that you want to update.
let groupPreferences = CometChatNotifications.GroupPreferences();
// Change group preferences
groupPreferences.set(messagesPreference: .DONT_SUBSCRIBE)
groupPreferences.set(repliesPreference: .DONT_SUBSCRIBE)
groupPreferences.set(reactionsPreference: .DONT_SUBSCRIBE)
groupPreferences.set(memberAddedPreference: .SUBSCRIBE)
groupPreferences.set(memberKickedPreference: .SUBSCRIBE)
groupPreferences.set(memberJoinedPreference: .SUBSCRIBE)
groupPreferences.set(memberLeftPreference: .SUBSCRIBE)
groupPreferences.set(memberBannedPreference: .SUBSCRIBE)
groupPreferences.set(memberUnbannedPreference: .SUBSCRIBE)
groupPreferences.set(memberScopeChangedPreference: .SUBSCRIBE)
// Load the updates in the NotificationPreferences instance.
updatedPreferences.set(groupPreferences: groupPreferences)
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences) { updatedPreferences in
print("updatePreferences: \(updatedPreferences)")
} onError: { error in
print("updatePreferences: \(error.errorCode) \(error.errorDescription)")
}
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences
NotificationPreferences updatedPreferences = NotificationPreferences();
GroupPreferences groupPreferences = GroupPreferences(
messages: MessagesOptions.SUBSCRIBE_TO_MENTIONS,
replies: RepliesOptions.SUBSCRIBE_TO_ALL,
reactions: ReactionsOptions.SUBSCRIBE_TO_REACTIONS_ON_ALL_MESSAGES,
memberAdded: MemberActionsOptions.SUBSCRIBE,
memberJoined: MemberActionsOptions.SUBSCRIBE,
memberLeft: MemberActionsOptions.SUBSCRIBE,
memberKicked: MemberActionsOptions.SUBSCRIBE,
memberBanned: MemberActionsOptions.SUBSCRIBE,
memberUnbanned: MemberActionsOptions.SUBSCRIBE,
memberScopeChanged: MemberActionsOptions.SUBSCRIBE,
);
updatedPreferences.groupPreferences = groupPreferences;
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences,
onSuccess: (preferencesAfterUpdate) {
debugPrint("updatePreferences:success");
// Use the preferencesAfterUpdate
}, onError: (e) {
debugPrint("updatePreferences:error: ${e.toString()}");
});
One-on-one preferences
Dashboard configuration
As the name suggests, these preferences help you to configure Notifications for events generated in one-on-one conversations.
| Categories | Events | Available preferences | Can user override? |
|---|
| Conversations | New messages | • Don’t notify • Notify for all messages (Default) • Notify for messages with mentions | • Yes (Default) • No |
| New replies | • Don’t notify • Notify for all replies (Default) • Notify for replies with mentions | • Yes (Default) • No |
| Message actions | Message is edited | • Don’t notify • Notify (Default) | - Yes • No (Default) |
| Message is deleted | • Don’t notify • Notify (Default) | - Yes • No (Default) |
| Message receives a reaction | • Don’t notify • Notify for reactions received on all messages • Notify for reactions received on own messages (Default) | • Yes (Default) • No |
Regarding Message edited & Message deleted eventsPush notifications should be triggered for the message edited and message deleted events in order to retract the notification displaying the original message. Turning them off is not recommended.
Client-side implementation
CometChatNotifications.fetchPreferences() method retrieves the notification preferences saved by the user as an instance of NotificationPreferences class. If the user has not configured any preferences, the default preferences defined by the CometChat administrator via the dashboard will be returned.
1. Fetch one-on-one preferences
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
const preferences = await CometChatNotifications.fetchPreferences();
// Display One-on-One preferences
const oneOnOnePreferences = preferences.getOneOnOnePreferences();
const oneOnOneMessagesPreference = oneOnOnePreferences.getMessagesPreference();
const oneOnOneRepliesPreference = oneOnOnePreferences.getRepliesPreference();
const oneOnOneReactionsPreference =
oneOnOnePreferences.getReactionsPreference();
CometChatNotifications.fetchPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Display one-on-one preferences
OneOnOnePreferences oneOnOnePreferences = notificationPreferences.getOneOnOnePreferences();
MessagesOptions oneOnOneMessagesPreference = oneOnOnePreferences.getMessagesPreference();
RepliesOptions oneOnOneRepliesPreference = oneOnOnePreferences.getRepliesPreference();
ReactionsOptions oneOnOneReactionsPreference = oneOnOnePreferences.getReactionsPreference();
}
@Override
public void onError(CometChatException e) {
// Something went wrong while fetching notification preferences
}
});
CometChatNotifications.fetchPreferences { notificationPreferences in
// Display one-on-one preferences
let oneOnOnePreferences = notificationPreferences.oneOnOnePreferences;
let oneMessages = oneOnOnePreferences?.messagesPreference;
let oneReplies = oneOnOnePreferences?.repliesPreference;
let oneReactions = oneOnOnePreferences?.reactionsPreference;
} onError: { error in
// Something went wrong while fetching notification preferences.
print("fetchPreferences: \(error.errorCode) \(error.errorDescription)");
}
CometChatNotifications.fetchPreferences(
onSuccess: (notificationPreferences) {
// Display one-on-one preferences
OneOnOnePreferences? oneOnOnePreferences = notificationPreferences.oneOnOnePreferences;
MessagesOptions? oneOnOneMessagesPreference = oneOnOnePreferences?.messages;
RepliesOptions? oneOnOneRepliesPreference = oneOnOnePreferences?.replies;
ReactionsOptions? oneOnOneReactionsPreference = oneOnOnePreferences?.reactions;
},
onError: (e) {
debugPrint("fetchPreferences:error ${e.toString()}");
});
2. Update one-on-one preferences
CometChatNotifications.updatePreferences() method is used to update a user’s notification preferences. The “override” toggle defined in the dashboard is crucial when updating preferences. If any preference is non-overridable, the method doesn’t generate an error; it instead returns the NotificationPreferences object with the updated values where overrides are allowed.
This functionality can be beneficial for temporarily superseding certain user preferences to ensure notifications for a specific event are delivered. Nonetheless, it is advisable to use this approach temporarily to avoid confusing users with unexpected changes to their notification settings.
It is unnecessary to specify all values; only set and save the preferences that have been changed.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
const updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
const oneOnOnePreferences = new OneOnOnePreferences();
// Change one-on-one preferences
oneOnOnePreferences.setMessagesPreference(MessagesOptions.DONT_SUBSCRIBE);
oneOnOnePreferences.setRepliesPreference(RepliesOptions.DONT_SUBSCRIBE);
oneOnOnePreferences.setReactionsPreference(ReactionsOptions.DONT_SUBSCRIBE);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setOneOnOnePreferences(oneOnOnePreferences);
// Update the preferences and receive the udpated copy.
const preferences = await CometChatNotifications.updatePreferences(
updatedPreferences
);
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
NotificationPreferences updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
OneOnOnePreferences oneOnOnePreferences = new OneOnOnePreferences();
// Change one-on-one preferences
oneOnOnePreferences.setMessagesPreference(MessagesOptions.DONT_SUBSCRIBE);
oneOnOnePreferences.setRepliesPreference(RepliesOptions.DONT_SUBSCRIBE);
oneOnOnePreferences.setReactionsPreference(ReactionsOptions.DONT_SUBSCRIBE);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setOneOnOnePreferences(oneOnOnePreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences, new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Updated notificationPreferences
}
@Override
public void onError(CometChatException e) {
// Something went wrong
}
});
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
let updatedPreferences = CometChatNotifications.NotificationPreferences();
// Instantiate the preferences that you want to update.
let oneOnOnePreferences = CometChatNotifications.OneOnOnePreferences();
// Change one-on-one preferences
oneOnOnePreferences.set(messagesPreference: .DONT_SUBSCRIBE)
oneOnOnePreferences.set(repliesPreference: .DONT_SUBSCRIBE)
oneOnOnePreferences.set(reactionsPreference: .DONT_SUBSCRIBE)
// Load the updates in the NotificationPreferences instance.
updatedPreferences.set(oneOnOnePreferences: oneOnOnePreferences)
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences) { updatedPreferences in
print("updatePreferences: \(updatedPreferences)")
} onError: { error in
print("updatePreferences: \(error.errorCode) \(error.errorDescription)")
}
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences
NotificationPreferences updatedPreferences = NotificationPreferences();
// Instantiate the preferences that you want to update.
OneOnOnePreferences oneOnOnePreferences = OneOnOnePreferences(
messages: MessagesOptions.SUBSCRIBE_TO_ALL,
replies: RepliesOptions.SUBSCRIBE_TO_MENTIONS,
reactions: ReactionsOptions.SUBSCRIBE_TO_REACTIONS_ON_ALL_MESSAGES);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.oneOnOnePreferences = oneOnOnePreferences;
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences,
onSuccess: (preferencesAfterUpdate) {
debugPrint("updatePreferences:success");
// Use the preferencesAfterUpdate
}, onError: (e) {
debugPrint("updatePreferences:error: ${e.toString()}");
});
Mute preferences
Dashboard configuration
These preferences allow you to control whether the users will be able to modify mute preferences.
| Mute preferences | Can user configure? |
|---|
| Mute all notifications (DND) | • Yes (Default) - Users can activate the Do Not Disturb (DND) feature. • No |
| Mute group conversations | • Yes (Default) - Users can mute notifications for chosen group conversations for a specified duration. • No |
| Mute one-on-one conversations | • Yes (Default) - Users can mute notifications for chosen one-on-one conversations for a specified duration. • No |
Client-side implementation
1. Fetch mute preferences
CometChatNotifications.fetchPreferences() method retrieves the notification preferences saved by the user as an instance of NotificationPreferences class. If the user has not configured any preferences, the default preferences defined by the CometChat administrator via the dashboard will be utilized.
You can use the CometChatNotifications.getMutedConversations() method to display a list of conversations that have been muted by users. The method will return an array of MutedConversations object.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// Fetch mute preferences
const preferences = await CometChatNotifications.fetchPreferences();
// Display Mute preferences
const mutePreferences = preferences.getMutePreferences();
const DNDPreference = mutePreferences.getDNDPreference();
// Fetch muted conversations
const mutedConversations = await CometChatNotifications.getMutedConversations();
// Fetch mute preferences
CometChatNotifications.fetchPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Display mute preferences
MutePreferences mutePreferences = notificationPreferences.getMutePreferences();
DNDOptions dndPreference = mutePreferences.getDNDPreference();
}
@Override
public void onError(CometChatException e) {
// Something went wrong while fetching notification preferences
}
});
// Fetch muted conversations
CometChatNotifications.getMutedConversations(new CometChat.CallbackListener<List<MutedConversation>>() {
@Override
public void onSuccess(List<MutedConversation> mutedConversations) {
// List of muted conversations
}
@Override
public void onError(CometChatException e) {
// Fetching muted conversations failed.
}
});
CometChatNotifications.fetchPreferences { notificationPreferences in
// Display mute preferences
let mutePreferences = notificationPreferences.mutePreferences;
let dndPreference = mutePreferences?.DNDPreference;
} onError: { error in
// Something went wrong while fetching notification preferences.
print("fetchPreferences: \(error.errorCode) \(error.errorDescription)");
}
// Fetch muted conversations
CometChatNotifications.getMutedConversations { mutedConversations in
print("getMutedConversations: \(mutedConversations)")
} onError: { error in
print("getMutedConversations: \(error.errorCode) \(error.errorDescription)")
}
// Fetch mute preferences
CometChatNotifications.fetchPreferences(
onSuccess: (notificationPreferences) {
// Display mute preferences
MutePreferences? mutePreferences = notificationPreferences.mutePreferences;
DNDOptions? dndPreference = mutePreferences?.dnd;
},
onError: (e) {
debugPrint("fetchPreferences:error ${e.toString()}");
});
// Fetch muted conversations
CometChatNotifications.getMutedConversations(
onSuccess: (mutedConversations) {
debugPrint("getMutedConversations:success");
// use mutedConversations
}, onError: (e) {
debugPrint("getMutedConversations:error ${e.toString()}");
});
2. Update mute preferences
CometChatNotifications.updatePreferences() method is used to update a user’s notification preferences. The “override” toggle defined in the dashboard is crucial when updating preferences. If any preference is non-overridable, the method doesn’t generate an error; it instead returns the NotificationPreferences object with the updated values where overrides are allowed.
This functionality can be beneficial for temporarily superseding certain user preferences to ensure notifications for a specific event are delivered. Nonetheless, it is advisable to use this approach temporarily to avoid confusing users with unexpected changes to their notification settings. It is unnecessary to specify all values; only set and save the preferences that have been changed.
To mute one or more group or one-on-one conversations, utilize the CometChatNotifications.muteConversations() method. This method requires an array of MutedConversation objects, each containing the following properties:
| Property | Type | Description |
|---|
id | String | This can either be uid or guid. |
type | String | This can either be oneOnOne or group. |
until | Number | This is a valid timestamp from the future. Eg: 1710696964705. |
To unmute one or more group or one-on-one conversations that were muted by the user, utilize the CometChatNotifications.unmuteConversations() method. This method requires an array of UnmutedConversation objects, each containing the following properties:
| Property | Type | Description |
|---|
id | String | This can either be uid or guid. |
type | String | This can either be oneOnOne or group. |
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
const updatedPreferences = new NotificationPreferences();
const mutePreferences = new MutePreferences();
// Change mute preferences
mutePreferences.setDNDPreference(DNDOptions.ENABLED);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setMutePreferences(mutePreferences);
// Update the preferences and receive the udpated copy.
const notificationPreferences = await CometChatNotifications.updatePreferences(
updatedPreferences
);
// Mute conversations
const until = Date.now() + 86400000; // Mute for 1 day
const mutedUser = new MutedConversation();
mutedUser.setId('cometchat-uid-1');
mutedUser.setType(CometChatNotifications.MutedConversationType.ONE_ON_ONE);
mutedUser.setUntil(until);
const mutedGroup = new MutedConversation();
mutedGroup.setId('cometchat-guid-1');
mutedGroup.setType(CometChatNotifications.MutedConversationType.GROUP);
mutedGroup.setUntil(until);
await CometChatNotifications.muteConversations([mutedUser, mutedGroup]);
// Unmute conversations
const unmutedUser = new UnmutedConversation();
unmutedUser.setId('cometchat-uid-1');
unmutedUser.setType(CometChatNotifications.MutedConversationType.ONE_ON_ONE);
const unmuteList = [unmutedUser];
await CometChatNotifications.unmuteConversations(unmuteList);
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
NotificationPreferences updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
MutePreferences mutePreferences = new MutePreferences();
// Change mute preferences
mutePreferences.setDNDPreference(DNDOptions.ENABLED);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setMutePreferences(mutePreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences, new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Updated notificationPreferences
}
@Override
public void onError(CometChatException e) {
// Something went wrong
}
});
// Mute conversations
long until = System.currentTimeMillis() + 86400000; // Mute for 1 day
MutedConversation mutedUser = new MutedConversation();
mutedUser.setId("cometchat-uid-1");
mutedUser.setType(MutedConversationType.ONE_ON_ONE);
mutedUser.setUntil(until);
MutedConversation mutedGroup = new MutedConversation();
mutedGroup.setId("cometchat-guid-1");
mutedGroup.setType(MutedConversationType.GROUP);
mutedGroup.setUntil(until);
List<MutedConversation> allMuted = new ArrayList<>();
allMuted.add(mutedUser);
allMuted.add(mutedGroup);
CometChatNotifications.muteConversations(allMuted, new CometChat.CallbackListener<String>() {
@Override
public void onSuccess(String s) {
// Mute success
}
@Override
public void onError(CometChatException e) {
// Mute failed
}
});
// Unmute conversations
UnmutedConversation u = new UnmutedConversation();
u.setId("cometchat-uid-1");
u.setType(MutedConversationType.ONE_ON_ONE);
List<UnmutedConversation> unmuteList = new ArrayList<>();
unmuteList.add(u);
CometChatNotifications.unmuteConversations(unmuteList, new CometChat.CallbackListener<String>() {
@Override
public void onSuccess(String s) {
// Unmute success
}
@Override
public void onError(CometChatException e) {
// Unmute failed
}
});
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
let updatedPreferences = CometChatNotifications.NotificationPreferences();
// Instantiate the preferences that you want to update.
let mutePreferences = CometChatNotifications.MutePreferences();
// Change mute preferences
mutePreferences.set(DNDPreference:.ENABLED)
// Load the updates in the NotificationPreferences instance.
updatedPreferences.set(mutePreferences: mutePreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences) { updatedPreferences in
print("updatePreferences: \(updatedPreferences)")
} onError: { error in
print("updatePreferences: \(error.errorCode) \(error.errorDescription)")
}
// Mute conversations
let untilInterval = Date().addingTimeInterval(86400) // Mute for 1 day
let until = Int(untilInterval.timeIntervalSince1970 * 1000) // Convert to milliseconds
var mutedUser = CometChatNotifications.MutedConversation()
mutedUser.id = "cometchat-uid-1"
mutedUser.type = .ONE_ON_ONE
mutedUser.until = until
var mutedGroup = CometChatNotifications.MutedConversation()
mutedGroup.id = "cometchat-guid-1"
mutedGroup.type = .GROUP
mutedGroup.until = until
let allMuted = [mutedUser, mutedGroup]
CometChatNotifications.muteConversations(allMuted) { success in
print("muteConversations: \(success)")
} onError: { error in
print("muteConversations: \(error.errorCode) \(error.errorDescription)")
}
// Unmute conversations
var unmutedUser = CometChatNotifications.UnmutedConversation()
unmutedUser.id = "cometchat-uid-1"
unmutedUser.type = .ONE_ON_ONE
let unmuteList = [unmutedUser]
CometChatNotifications.unmuteConversations(unmuteList) { success in
print("unmuteConversations: \(success)")
} onError: { error in
print("unmuteConversations: \(error.errorCode) \(error.errorDescription)")
}
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences
NotificationPreferences updatedPreferences = NotificationPreferences();
MutePreferences mutePreferences = MutePreferences(dnd: DNDOptions.DISABLED);
updatedPreferences.mutePreferences = mutePreferences;
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences,
onSuccess: (preferencesAfterUpdate) {
debugPrint("updatePreferences:success");
// Use the preferencesAfterUpdate
}, onError: (e) {
debugPrint("updatePreferences:error: ${e.toString()}");
});
// Mute conversations
int current = DateTime.now().millisecondsSinceEpoch;
int oneDayMillis = 24 * 60 * 60 * 1000;
int until = current + oneDayMillis;
MutedConversation mutedUser = MutedConversation(
id: "cometchat-uid-1", type: MutedConversationType.ONE_ON_ONE, until: until);
MutedConversation mutedGroup = MutedConversation(
id: "cometchat-guid-1", type: MutedConversationType.GROUP, until: until);
List<MutedConversation> mutedConversations = [];
mutedConversations.add(mutedUser);
mutedConversations.add(mutedGroup);
CometChatNotifications.muteConversations(
mutedConversations,
onSuccess: (response) {
debugPrint("muteConversations:success ${response.toString()}");
},
onError: (e) {
debugPrint("muteConversations:error ${e.toString()}");
},
);
// Unmute conversations
UnmutedConversation unmutedUser = UnmutedConversation(id: "cometchat-uid-1", type: MutedConversationType.ONE_ON_ONE);
List<UnmutedConversation> unmuteList = [];
unmuteList.add(unmutedUser);
CometChatNotifications.unmuteConversations(unmuteList, onSuccess: (response) {
debugPrint("unmuteConversations:success ${response.toString()}");
},onError: (e) {
debugPrint("unmuteConversations:success ${e.toString()}");
});
Notification schedule
Dashboard configuration
Notifications will be delivered based on the specified daily timetable, adhering to the user’s local time zone. Select “None” to disable Notifications for that day. For instance, this can be applied to weekends, such as Saturday and Sunday.
| Day | From | To | Can user override? |
|---|
| Monday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | • Yes (Default) - Users can configure a personalized notification schedule. • No |
| Tuesday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
| Wednesday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
| Thursday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
| Friday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
| Saturday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
| Sunday | • 0 to 2359 (Default: 0) • None | • Upto 2359 (Default: 2359) | |
Client-side implementation
1. Fetch schedule preferences
CometChatNotifications.fetchPreferences() method retrieves the notification preferences saved by the user as an instance of NotificationPreferences class. If the user has not configured any preferences, the default preferences defined by the CometChat administrator via the dashboard will be utilized.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
const preferences = await CometChatNotifications.fetchPreferences();
// Display schedule preferences
const mutePreferences = preferences.getMutePreferences();
const schedulePreference = mutePreferences.getSchedulePreference();
const mondaySchedule = schedulePreference.get(DayOfWeek.MONDAY);
const tuesdaySchedule = schedulePreference.get(DayOfWeek.TUESDAY);
const wednesdaySchedule = schedulePreference.get(DayOfWeek.WEDNESDAY);
const thursdaySchedule = schedulePreference.get(DayOfWeek.THURSDAY);
const fridaySchedule = schedulePreference.get(DayOfWeek.FRIDAY);
const saturdaySchedule = schedulePreference.get(DayOfWeek.SATURDAY);
const sundaySchedule = schedulePreference.get(DayOfWeek.SUNDAY);
// This action can be performed on other days of the week.
const mondayFrom = mondaySchedule?.getFrom();
const mondayTo = mondaySchedule?.getTo();
const mondayDnd = mondaySchedule?.getDND();
CometChatNotifications.fetchPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Display schedule preferences
MutePreferences mutePreferences = notificationPreferences.getMutePreferences();
Map<DayOfWeek, DaySchedule> scheduleMap = mutePreferences.getSchedulePreference();
DaySchedule monday = scheduleMap.get(DayOfWeek.MONDAY);
DaySchedule tuesday = scheduleMap.get(DayOfWeek.TUESDAY);
DaySchedule wednesday = scheduleMap.get(DayOfWeek.WEDNESDAY);
DaySchedule thrusday = scheduleMap.get(DayOfWeek.THURSDAY);
DaySchedule friday = scheduleMap.get(DayOfWeek.FRIDAY);
DaySchedule saturday = scheduleMap.get(DayOfWeek.SATURDAY);
DaySchedule sunday = scheduleMap.get(DayOfWeek.SUNDAY);
// This action can be performed on other days of the week.
int mondayFrom = monday.getFrom();
int mondayTo = monday.getTo();
boolean mondayDnd = monday.getDnd();
}
@Override
public void onError(CometChatException e) {
// Something went wrong while fetching notification preferences
}
});
CometChatNotifications.fetchPreferences { notificationPreferences in
// Display schedule preferences
let mutePreferences = notificationPreferences.mutePreferences;
let schedulePref = mutePreferences?.schedulePreference;
let mondaySchedule = schedulePref?[.MONDAY];
let tuesdaySchedule = schedulePref?[.TUESDAY];
let wednesdaySchedule = schedulePref?[.WEDNESDAY];
let thursdaySchedule = schedulePref?[.THURSDAY];
let fridaySchedule = schedulePref?[.FRIDAY];
let saturdaySchedule = schedulePref?[.SATURDAY];
let sundaySchedule = schedulePref?[.SUNDAY];
// This action can be performed on other days of the week.
let mondayFrom = mondaySchedule?.from;
let mondayTo = mondaySchedule?.to;
let mondayDND = mondaySchedule?.dnd;
} onError: { error in
// Something went wrong while fetching notification preferences.
print("fetchPreferences: \(error.errorCode) \(error.errorDescription)");
}
CometChatNotifications.fetchPreferences(
// Display schedule preferences
MutePreferences? mutePreferences = notificationPreferences.mutePreferences;
Map<DayOfWeek, DaySchedule>? scheduleMap = mutePreferences?.schedule;
DaySchedule? mondaySchedule = scheduleMap?[DayOfWeek.MONDAY];
DaySchedule? tuesdaySchedule = scheduleMap?[DayOfWeek.TUESDAY];
DaySchedule? wednesdaySchedule = scheduleMap?[DayOfWeek.WEDNESDAY];
DaySchedule? thursdaySchedule = scheduleMap?[DayOfWeek.THURSDAY];
DaySchedule? fridaySchedule = scheduleMap?[DayOfWeek.FRIDAY];
DaySchedule? saturdaySchedule = scheduleMap?[DayOfWeek.SATURDAY];
DaySchedule? sundaySchedule = scheduleMap?[DayOfWeek.SUNDAY];
// This action can be performed on other days of the week.
int? mondayFrom = mondaySchedule?.from;
int? mondayTo = mondaySchedule?.to;
bool? mondayDnd = mondaySchedule?.dnd;
},
onError: (e) {
debugPrint("fetchPreferences:error ${e.toString()}");
});
2. Update schedule preferences
CometChatNotifications.updatePreferences() method is used to update a user’s notification preferences. The “override” toggle defined in the dashboard is crucial when updating preferences. If any preference is non-overridable, the method doesn’t generate an error; it instead returns the NotificationPreferences object with the updated values where overrides are allowed.
This functionality can be beneficial for temporarily superseding certain user preferences to ensure notifications for a specific event are delivered. Nonetheless, it is advisable to use this approach temporarily to avoid confusing users with unexpected changes to their notification settings.
It is unnecessary to specify all values; only set and save the preferences that have been changed.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
const updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
const mutePreferences = new MutePreferences();
// Change schedule preferences
const scheduleMap = new Map<DayOfWeek, DaySchedule>();
const mondaySchedule = new DaySchedule(2015, 2345, false);
scheduleMap.set(DayOfWeek.MONDAY, mondaySchedule);
mutePreferences.setSchedulePreference(scheduleMap);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setOneOnOnePreferences(oneOnOnePreferences);
updatedPreferences.setGroupPreferences(groupPreferences);
updatedPreferences.setMutePreferences(mutePreferences);
// Update the preferences and receive the udpated copy.
const preferences = await CometChatNotifications.updatePreferences(updatedPreferences);
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
NotificationPreferences updatedPreferences = new NotificationPreferences();
// Instantiate the preferences that you want to update.
MutePreferences mutePreferences = new MutePreferences();
// Change schedule preferences
Map scheduleMap = new ArrayMap<DayOfWeek, DaySchedule>();
DaySchedule mondaySchedule = new DaySchedule();
mondaySchedule.setFrom(2015);
mondaySchedule.setTo(2345);
mondaySchedule.setDnd(false);
scheduleMap.put(DayOfWeek.MONDAY, mondaySchedule);
mutePreferences.setSchedulePreference(scheduleMap);
// Load the updates in the NotificationPreferences instance.
updatedPreferences.setMutePreferences(mutePreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences, new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Updated notificationPreferences
}
@Override
public void onError(CometChatException e) {
// Something went wrong
}
});
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
let updatedPreferences = CometChatNotifications.NotificationPreferences();
// Instantiate the preferences that you want to update.
let mutePreferences = CometChatNotifications.MutePreferences();
// Change schedule preferences
var dictionary = [CometChatNotifications.DayOfWeek:CometChatNotifications.DaySchedule]();
dictionary[.MONDAY] = CometChatNotifications.DaySchedule(from: 2015, to: 2345, dnd: false)
mutePreferences.set(schedulePreference: dictionary)
// Load the updates in the NotificationPreferences instance.
updatedPreferences.set(mutePreferences: mutePreferences);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences) { updatedPreferences in
print("updatePreferences: \(updatedPreferences)")
} onError: { error in
print("updatePreferences: \(error.errorCode) \(error.errorDescription)")
}
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences
NotificationPreferences updatedPreferences = NotificationPreferences();
// Instantiate the preferences that you want to update.
MutePreferences mutePreferences = MutePreferences(dnd: DNDOptions.DISABLED);
Map<DayOfWeek, DaySchedule> scheduleMap = {};
DaySchedule mondaySchedule = DaySchedule(from: 2015, to: 2345, dnd: false);
scheduleMap[DayOfWeek.MONDAY] = mondaySchedule;
mutePreferences.schedule = scheduleMap;
updatedPreferences.mutePreferences = mutePreferences;
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences,
onSuccess: (preferencesAfterUpdate) {
debugPrint("updatePreferences:success");
// Use the preferencesAfterUpdate
}, onError: (e) {
debugPrint("updatePreferences:error: ${e.toString()}");
});
Bypass preferences for mentions
If this setting is enabled, end users who have enabled Do Not Disturb (DND), muted a conversation, or set up a schedule will still receive notifications when they are mentioned in a new message or reply.
Bypass preferences for @all
If this setting is enabled, end users who have enabled Do Not Disturb (DND), muted a conversation, or set up a schedule will still receive notifications when @all (a mention that notifies all group members) is used in a group message. This ensures all group members are notified regardless of their individual notification preferences.
Calls preferences
Push notifications are triggered for calling events. These notifications are not delivered via Email or SMS.
| Events | Available preferences | Can user override? |
|---|
| Call initiated | • Don’t notify • Notify (Default) | No |
| Call ongoing | • Don’t notify • Notify (Default) | No |
| Call cancelled | • Don’t notify • Notify (Default) | No |
| Call rejected | • Don’t notify • Notify (Default) | No |
| Call unanswered | • Don’t notify • Notify (Default) | No |
| Call busy | • Don’t notify • Notify (Default) | No |
| Call ended | • Don’t notify • Notify (Default) | No |
Reset preferences
CometChatNotifications.resetPreferences() method is used to reset the preferences for a user to their default state. The default state of preferences is defined by the CometChat administrator via the dashboard.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
const defaultPreferences = await CometChatNotifications.resetPreferences();
CometChatNotifications.resetPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences defaultPreferences) {
// Display the defaultPreferneces.
}
@Override
public void onError(CometChatException e) {
// Something went wrong.
}
});
CometChatNotifications.resetPreferences { defaultPreferences in
print("resetPreferences: defaultPreferences \(defaultPreferences)");
} onError: { error in
print("resetPreferences: \(error.errorCode) \(error.errorDescription)");
}
CometChatNotifications.resetPreferences(onSuccess: (defaultPreferences) {
debugPrint("resetPreferences:success");
// defaultPreferences are available after reset.
},onError: (e) {
debugPrint("resetPreferences:error ${e.toString()}");
});
Push notification preferences
The notification payload sent to FCM, APNs, or custom providers can be customized to include the CometChat message object for new messages and replies. To comply with the 4 KB payload size limit required by FCM and APNs, specific parts of the message object can be excluded to reduce the payload size. Additionally, a custom payload in the form of a JSON object can be included in the push payload.
| Payload setting | Available preferences |
|---|
| Include CometChat message object | • false (Default) • true |
| Include Sender’s metadata | • false • true (Default) |
| Include Receiver’s metadata | • false • true (Default) |
| Include message metadata | • false • true (Default) |
| Trim CometChat text message | • false (Default) • true |
| Custom JSON | No defaults for this value. If not set, this key is not included. |
Email notification preferences
| Preference | Values | Description |
|---|
| Notify for unread messages only | • true (Default) • false | • Email notifications are sent only when there are unread messages in a conversation. • When set to false, the notifications are sent irrespective of whether there are unread messages or not. |
| The interval between two emails (in minutes) | 120 | • By default, the notifications are triggered after 120 minutes. • The minimum allowed value is 1 minute. • The maximum is 1440 minutes (24 hours). |
| Maximum emails per day | 20 | • By default, a maximum of 20 email notifications can be sent to a user on a given day. • The minimum value can be set to 1. • The maximum can be 30. |
| Maximum emails per conversation per day | 2 | • By default, a maximum of 2 email notifications can be sent to a user for a given conversation on a given day. • The minimum value can be set to 1. • The maximum can be 30. |
| Include CometChat message object | • false (Default) • true | If enabled, the message object will be included in the email notification payload. |
| Include Sender’s metadata | • false (Default) • true | If enabled, the sender’s metadata will be included in the message object (applicable only when the message object is included). |
| Include Receiver’s metadata | • false (Default) • true | If enabled, the receiver’s metadata will be included in the message object (applicable only when the message object is included). |
| Include message metadata | • false (Default) • true | If enabled, the message metadata will be included in the message object (applicable only when the message object is included). |
SMS notification preferences
| Preference | Values | Description |
|---|
| Notify for unread messages only | • true (Default) • false | • SMS notifications are sent only when there are unread messages in a conversation. • When set to false, the notifications are sent irrespective of whether there are unread messages or not. |
| The interval between two emails (in minutes) | 120 | • By default, the notifications are triggered after 120 minutes. • The minimum allowed value is 1 minute. • The maximum is 1440 minutes (24 hours). |
| Maximum SMS per day | 20 | • By default, a maximum of 20 SMS notifications can be sent to a user on a given day. • The minimum value can be set to 1. • The maximum can be 30. |
| Maximum SMS per conversation per day | 2 | • By default, a maximum of 2 SMS notifications can be sent to a user for a given conversation on a given day. • The minimum value can be set to 1. • The maximum can be 30. |
| Include CometChat message object | • false (Default) • true | If enabled, the message object will be included in the SMS notification payload. |
| Include Sender’s metadata | • false (Default) • true | If enabled, the sender’s metadata will be included in the message object (applicable only when the message object is included). |
| Include Receiver’s metadata | • false (Default) • true | If enabled, the receiver’s metadata will be included in the message object (applicable only when the message object is included). |
| Include message metadata | • false (Default) • true | If enabled, the message metadata will be included in the message object (applicable only when the message object is included). |
Common templates and sounds
Templates are designed to specify the content displayed in notifications on the user’s device for different events.
Templates incorporate placeholders, which reference specific pieces of information determined by properties from the event.
For example, New message event has the following structure:
{
"data": {
"id": "17",
"conversationId": "group_cometchat-guid-1",
"sender": "cometchat-uid-2",
"receiverType": "group",
"receiver": "cometchat-guid-1",
"category": "message",
"type": "text",
"data": {
"text": "Hello! How are you?",
"entities": {
"sender": {
"entity": {
"uid": "cometchat-uid-2",
"name": "George Alan",
"role": "default",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp",
"status": "available",
"lastActiveAt": 1707901272
},
"entityType": "user"
},
"receiver": {
"entity": {
"guid": "cometchat-guid-1",
"icon": "https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-1.webp",
"name": "Hiking Group",
"type": "public",
"owner": "cometchat-uid-1",
"createdAt": 1706014061,
"conversationId": "group_cometchat-guid-1",
"onlineMembersCount": 3
},
"entityType": "group"
}
},
},
"sentAt": 1707902030,
"updatedAt": 1707902030
}
}
The sender’s name is accessible via data.entities.sender.name, so the placeholder for the sender’s name will be {{message.data.entities.sender.name}}. This placeholder is substituted within the template with the actual name of the sender aka the substitution value.
As an administrator, you can configure:
- Default templates - Use these templates to display previews by leveraging the information contained in the event.
- Privacy templates - Employ these templates to present generic content in the notification.
Privacy setting
Dashboard configuration
Configure which template will be used for displaying the content of the notifications displayed on user’s devices. The available preferences are:
- Use default template - Enforces the use of default templates for all the users.
- Use privacy template - Enforces the use of privacy templates for all the users.
- Use default templates with user privacy override (Default) - Uses default templates by default, but allows the users to enable privacy to hide the previews.
Client-side implementation
1. Fetch privacy setting
The method CometChatNotifications.fetchPreferences() retrieves the notification preferences saved by the user as an instance of NotificationPreferences class. If the user has not configured any preferences, the default preferences defined by the CometChat administrator via the dashboard will be utilized.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
const preferences = await CometChatNotifications.fetchPreferences();
// Display a toggle for use privacy option TODO
const usePrivacyTemplate = preferences.getUsePrivacyTemplate();
CometChatNotifications.fetchPreferences(new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Display a toggle for use privacy option
boolean usePrivacyTemplate = notificationPreferences.getUsePrivacyTemplate();
}
@Override
public void onError(CometChatException e) {
// Something went wrong while fetching notification preferences
}
});
CometChatNotifications.fetchPreferences { notificationPreferences in
// Display a toggle for use privacy option
let usePrivacyTemplate = notificationPreferences.usePrivacyTemplate;
} onError: { error in
// Something went wrong while fetching notification preferences.
print("fetchPreferences: \(error.errorCode) \(error.errorDescription)");
}
CometChatNotifications.fetchPreferences(
onSuccess: (notificationPreferences) {
// Display a toggle for use privacy option
bool? usePrivacyTemplate = notificationPreferences.usePrivacyTemplate;
},
onError: (e) {
debugPrint("fetchPreferences:error ${e.toString()}");
});
2. Update privacy setting
CometChatNotifications.updatePreferences() method is used to update a user’s notification preferences. The “override” toggle defined in the dashboard is crucial when updating preferences. If any preference is non-overridable, the method doesn’t generate an error; it instead returns the NotificationPreferences object with the updated values where overrides are allowed.
This functionality can be beneficial for temporarily superseding certain user preferences to ensure notifications for a specific event are delivered. Nonetheless, it is advisable to use this approach temporarily to avoid confusing users with unexpected changes to their notification settings.
It is unnecessary to specify all values; only set and save the preferences that have been changed.
JavaScript
Android
iOS
Flutter
// This is applicable for web, React native, Ionic cordova
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
const updatedPreferences = new NotificationPreferences();
// To update the preference for privacy template
updatedPreferences.setUsePrivacyTemplate(true);
// Update the preferences and receive the udpated copy.
const notificationPreferences = await CometChatNotifications.updatePreferences(
updatedPreferences
);
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
NotificationPreferences updatedPreferences = new NotificationPreferences();
// To update the preference for privacy template
updatedPreferences.setUsePrivacyTemplate(true);
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences, new CometChat.CallbackListener<NotificationPreferences>() {
@Override
public void onSuccess(NotificationPreferences notificationPreferences) {
// Updated notificationPreferences
}
@Override
public void onError(CometChatException e) {
// Something went wrong
}
});
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences.
let updatedPreferences = CometChatNotifications.NotificationPreferences();
// To update the preference for privacy template
updatedPreferences.set(usePrivacyTemplate: true)
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences) { prefs in
print("updatePreferences: \(prefs)")
} onError: { error in
print("updatePreferences: \(error.errorCode) \(error.errorDescription)")
}
// The example demonstrates modifying all values; however, modifying only the changed values is sufficient.
// Instantiate the NotificationPreferences
NotificationPreferences updatedPreferences = NotificationPreferences();
// To update the preference for privacy template
updatedPreferences.usePrivacyTemplate = true;
// Update the preferences.
CometChatNotifications.updatePreferences(updatedPreferences,
onSuccess: (preferencesAfterUpdate) {
debugPrint("updatePreferences:success");
// Use the preferencesAfterUpdate
}, onError: (e) {
debugPrint("updatePreferences:error: ${e.toString()}");
});
Text message templates
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | {{message.data.text}} | New message |
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body for Image | 📷 Has sent an image | New image message |
| Body for Audio | 🔈 Has sent an audio | New audio message |
| Body for Audio | 🎥 Has sent a video | New video message |
| Body for Audio | 📄 Has sent a file | New file message |
Custom message templates
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | {{message.data.text}} | {{message.data.text}} |
| Body (Fallback) | New message | New message |
Note: The “Body (Fallback)” value is utilized when any placeholders within the “Body” fail to resolve to an appropriate substitution value.
For example, if {{message.data.text}} in the aforementioned scenario evaluates to null or undefined, the “Body (Fallback)” value will be utilized.
Ideally, the “Body (Fallback)” value should not contain any placeholders to prevent additional resolution failures.
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | {{data.interactiveData.title}} | New message |
Interactive card templates
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | {{data.interactiveData.title}} | New message |
Interactive scheduler templates
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | New invite | New invite |
Custom Interactive message templates
| Template for | Default template values | Privacy template values |
|---|
| Title (One-on-one) | {{message.data.entities.sender.entity.name}} | {{message.data.entities.sender.entity.name}} |
| Title (Group) | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} | {{message.data.entities.sender.entity.name}} @ {{message.data.entities.receiver.entity.name}} |
| Body | New message | New message |
Sounds
The sound files must be included within the app’s bundle. These values are set within the notification payload as values of the “sound” field.
Sound for Call Notifications: Specify the name of the sound file you wish to play for call notifications.
Sound for Chat Notifications: Specify the name of the sound file you wish to play for chat notifications.
Email notification templates
You can use a default template or a privacy template in case you consider the information to be displayed as sensitive. The data available for email’s subject template is as follows:
{
"to": {
"uid": "cometchat-uid-1",
"name": "Andrew Joseph",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
},
"messages": [
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "Are we meeting on this weekend?",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
},
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "📷 Has shared an image",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
}
],
"senderDetails": {
"uid": "cometchat-uid-4",
"name": "Susan Marie",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp"
}
}
{
"to": {
"uid": "cometchat-uid-1",
"name": "Andrew Joseph",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
},
"messages": [
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "Hello all! What's up?",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
},
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "This is the place I was thinking about",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
}
],
"groupDetails": {
"guid": "cometchat-guid-1",
"name": "Hiking Group",
"icon": "https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-1.webp"
}
}
Considering the above data, an email’s subject can be formatted as follows:
| Subject for | Template | Final subject |
|---|
| Group notification | Hello {{to.name}}! You have {{messages.length}} message(s) in {{groupDetails.name}}. | Hello Andrew Joseph! You have 2 message(s) in Hiking Group. |
| One-on-one notification | Hello {{to.name}}! You have {{messages.length}} message(s) from {{senderDetails.name}}. | Hello Andrew Joseph! You have 2 message(s) from Susan Marie. |
SMS notification templates
You can use a default template or a privacy template in case you consider the information to be displayed as sensitive. The data available for SMS template is as follows:
{
"to": {
"uid": "cometchat-uid-1",
"name": "Andrew Joseph",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
},
"messages": [
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "Are we meeting on this weekend?",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
},
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "📷 Has shared an image",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
}
],
"senderDetails": {
"uid": "cometchat-uid-4",
"name": "Susan Marie",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp"
}
}
{
"to": {
"uid": "cometchat-uid-1",
"name": "Andrew Joseph",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
},
"messages": [
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "Hello all! What's up?",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
},
{
"sender": {
"uid": "cometchat-uid-4",
"avatar": "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-4.webp",
"name": "Susan Marie"
},
"message": "This is the place I was thinking about",
"messageObject": {CometChat Message Object}, // Present if "Include message object" is enabled. The message object is present for new messages or in case a message was edited
}
],
"groupDetails": {
"guid": "cometchat-guid-1",
"name": "Hiking Group",
"icon": "https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-1.webp"
}
}
Considering the above data, an SMS can be formatted as follows:
| SMS for | Template | Final content |
|---|
| Group notification | You’ve received {{messages.length}} message(s) in {{groupDetails.name}}! Read them at https://your-website.com. | You’ve received 2 message(s) in Hiking Group! Read them at https://your-website.com. |
| One-on-one notification | You’ve received {{messages.length}} message(s) from {{senderDetails.name}}! Read them at https://your-website.com/chat. | You’ve received 2 message(s) from Susan Marie! Read them at https://your-website.com/chat. |