Manage messages
Update time: 2024/03/07 11:13:40
NIM SDK can send and receive messages in groups, receive notification messages for group operations, and set message notification modes.
Messaging in groups
You can send and receive multiple types of messages in group chats.
After creating or joining a group, the interface used to send and receive messages is the same as that of P2P messaging. The difference lies in the parameter of the session type (sessionType
). Select team
for group messages.
Enumeration | Description |
---|---|
p2p | P2P chat |
team | Advanced group |
superTeam | Supergroup |
chatRoom | Chat room |
system | System notification |
For information about how messaging works, see Messaging.
Notification messages
NIM SDK provides built-in message types (NIMMessageType
). Notifications are sent for events of groups, chat rooms and supergroups. Notifications of this type are sent by the server, and clients cannot send notification messages.
The following events trigger group notification messages.
NIMTeamNotificationTypes |
Attachment class | Description |
---|---|---|
acceptInvite | NIMMemberChangeAttachment | A member joins the group after accepting the invitation. |
inviteMember | NIMMemberChangeAttachment | A member are invited to join a group |
addTeamManager | NIMMemberChangeAttachment | An administrator is added |
kickMember | NIMMemberChangeAttachment | A member is removed from a group |
transferOwner | NIMMemberChangeAttachment | The ownership of a group is transferred. |
passTeamApply | NIMMemberChangeAttachment | Join request is accepted |
removeTeamManager | NIMMemberChangeAttachment | An administrator is removed |
dismissTeam | NIMDismissAttachment | A group is dismissed |
leaveTeam | NIMLeaveTeamAttachment | A member leave a group. |
muteTeamMember | NIMMuteMemberAttachment | A member is muted or unmuted. |
updateTeam | NIMUpdateTeamAttachment | The group information is updated. |
Parse notification messages:
- Get the message type by calling the
NIMMessage.messageType
method. Thenotification
type indicates a notification message. - Convert the attachment type obtained by
[
NIMMessage.messageAttachment](https://doc.commsease.com/docs/interface/NIM_SDK/en/Latest/Flutter/nim_core/NIMMessage/messageAttachment.html) to
NIMTeamNotificationAttachment`. - Get the specific notification type (
NotificationType
) usingNotificationAttachment.getType
. - Convert the attachment type obtained by
NIMMessage.messageAttachment
to a corresponding type based onTeamNotificationTypes
. See the previous table.
- For
NIMMemberChangeAttachment
, get the target of an event using thetargets
method. For example, if User B is removed from the group, the account of User B can be obtained using thegetTargets
method. NIMMuteMemberAttachment
is the subclass ofNIMMemberChangeAttachment
. You can get the targets of the event using thetargets
method and whether thetargets
are muted or unmuted usingmute
.- For
NIMUpdateTeamAttachment
, notifications must be parsed for which group profile is updated. Muting groups belongs to this type.
Sample code:
NimCore.instance.messageService.onMessage.listen((event) {
print('Test_Observer onMessage ${event.toString()}');
for (NIMMessage m in event) {
if(message.messageType = NIMMessageType.notification){
if (type >= NIMTeamNotificationTypes.inviteMember &&
type <= NIMTeamNotificationTypes.muteTeamMember) {
/// See the events that trigger group notifications.
}
}
}
});
Do-Not-Disturb
The SDK can set the configuration of the alert mode for message notifications. the alert mode for notifications (NIMTeamMessageNotifyTypeEnum
):
- all: All notifications are alerted (default).
- manager: Only notifications sent by the group owner and administrators are alerted.
- mute: all notifications are not alerted.
The alerts belong to [Push service and alerts] (https://doc.commsease.com/messaging/docs/zQ5NzIwODk?platform=flutter) in the CommsEase system.
The alert mode of messages does not affect receiving messages and the unread count. If you set the alert mode to no alerts, you can still receive group messages, and the unread count will still change.
Set the Do-Not-Disturb status
Set the mute type of a specified group.using muteTeam
.
Parameters:
Parameter | Description |
---|---|
teamId | group ID |
notifyType | Notification type enumerationall : All members are alerted. manager : Only the group owner and administrators are alertedmute : no alerts |
Sample code:
var result = await NimCore.instance.teamService.muteTeam(
teamId,
NIMTeamMessageNotifyTypeEnum.mute);
Do-Not-Disturb
Get the alert mode for notifications of a group using NIMTeam.messageNotifyType
Read receipt for group messages
- For interfaces for read receipt, see Read receipt.
API reference
API |
Description |
---|---|
muteTeam |
Set the mute type for a specified group. |
fetchTeamMessageReceiptDetail |
Query the specified accounts that have read and unread a message in a group. |
queryTeamMessageReceiptDetail |
Get the specified accounts that have read or unread a group message from the local database (synchronous call). |
refreshTeamMessageReceipt |
Refresh the number of read and unread messages in group without an asychronous callback |
sendTeamMessageReceipt |
Mark group message as read. |