Manage messages

Update time: 2023/02/09 06:27:11

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 of the session type (sessionType). Select NIMSessionTypeTeam for group messages.

See NIMSessionType

Enumeration Description
NIMSessionTypeP2P P2P chat
NIMSessionTypeSuperTeam Supergroup
NIMSessionTypeTeam Advanced group

For information about how messaging works, see Messaging.

Notification messages

NIM SDK provides built-in message types (NIMMessageType). Notifications (NIMMessageTypeNotification) 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.

Group notification messages are the same as other types of messages, and can be retrieved using the interface in [NIMConversationManager](https://doc.commsease.com/docs/interface/IMS for iOS/NIMSDK-iOS/Protocols/NIMConversationManager.html).

The following events trigger group notification messages.

NIMTeamOperationType Type Event
NIMTeamOperationTypeInvite Invite users into a group (the consent of the invited user is not required)
NIMTeamOperationTypeAcceptInvitation Join the group after accepting the invitation (the consent of the invited user is required)
NIMTeamOperationTypeKick A member is removed from a group.
NIMTeamOperationTypeLeave A member leaves a group.
NIMTeamOperationTypeUpdate The group information is updated
NIMTeamOperationTypeDismiss A group is dismissed.
NIMTeamOperationTypeApplyPass A request to join a group is approved.
NIMTeamOperationTypeTransferOwner The ownership of a group is transferred.
NIMTeamOperationTypeAddManager A new administrator is added
NIMTeamOperationTypeRemoveManager An administrator is removed.
NIMTeamOperationTypeMute A member is muted or unmuted

Parse notification messages:

  1. Use the messageType of NIMMessage to determine whether a message is a notification.
  2. Convert the value type of the messageObject field in NIMMessage to NIMNotificationObject.
  3. Parse the content field in NIMNotificationObject to get the parent class NIMNotificationContent.
  4. Check the notificationType field in NIMNotificationContent to determine whether it is NIMNotificationTypeTeam.
  5. Convert the type of the parent class NIMNotificationContent to NIMTeamNotificationContent.
  6. The specific type of group notifications can be parsed using operationType of NIMTeamNotificationContent, see the previous table for the specific type.
  7. After receiving group notifications, the SDK will modify the locally cached group information, and then trigger the callback for the delegate event related to the edit event.

Parse notification messages:

  //Receive NIMMessage returned by the callback and the variable name is message    
    NIMNotificationObject *object = message.messageObject;
    if (message.messageType == NIMMessageTypeNotification && object.notificationType == NIMNotificationTypeTeam)
    {
        //Parse the group notification
        NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;

        //Perform the business logic for the received NIMTeamNotificationContent.
        ...
    }

NIMTeamNotificationContent representation:

  • operationType: the event type, indicating which event happens in a group. The notification type is defined by the NIMTeamOperationType enumeration.
  • sourceID: The operator ID of the event, indicating who performs the operation.
  • targetIDs: A list of target IDs, indicating the targets of the operation.
  • notifyExt : The extension field of the notification, defined by each operation interface that triggers the notification. This field can only be defined by the server API. For example: to remove a user out of a group, you can call the Server API with the notification extension parameters. After calling the API, the event for the operation is triggered and the notification that the user is removed out of the group is sent. At this time, other group members who receive the notification can read the notifyExt field of NIMTeamNotificationContent to get the extension field defined for the removing a member.
  • attachment: notification attachment used to assist the upper layer for display. The types of group notifications with additional information are:
    • NIMTeamOperationTypeUpdate: the additional information is wrapped as NIMUpdateTeamInfoAttachment type. Muting groups belongs to this type.
    • NIMTeamOperationTypeMute: additional information is wrapped as NIMMuteTeamMemberAttachment type.

For notifications of NIMTeamOperationTypeUpdate type, you need to check the specific content in NIMUpdateTeamInfoAttachment to determine which group information is updated. See the following sample code:

case NIMTeamOperationTypeUpdate:
    {
        id attachment = [content attachment];
        if ([attachment isKindOfClass:[NIMUpdateTeamInfoAttachment class]]) {
            NIMUpdateTeamInfoAttachment *teamAttachment = (NIMUpdateTeamInfoAttachment *)attachment;
            formatedMessage = [NSString stringWithFormat:@"%@ updated %@ information ".nim_localized,source,teamName];

            //If only a single item is modified, display the specific modified item
            if ([teamAttachment.values count] == 1) {
                NIMTeamUpdateTag tag = [[[teamAttachment.values allKeys] firstObject] integerValue];
                switch (tag) {
                    case NIMTeamUpdateTagName:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated %@ group name".nim_localized,source,teamName];
                        break;
                    case NIMTeamUpdateTagIntro:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated %@introduction".nim_localized,source,teamName];
                        break;
                    case NIMTeamUpdateTagAnouncement:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated %@announcement".nim_localized,source,teamName];
                        break;
                    case NIMTeamUpdateTagJoinMode:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated %@verification method".nim_localized,source,teamName];
                        break;
                    case NIMTeamUpdateTagAvatar:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated %@avatar".nim_localized,source,teamName];
                        break;
                    case NIMTeamUpdateTagInviteMode:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated the permissions for invitation".nim_localized,source];
                        break;
                    case NIMTeamUpdateTagBeInviteMode:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated the permissions to require the consent of the invited user".nim_localized,source];
                        break;
                    case NIMTeamUpdateTagUpdateInfoMode:
                        formatedMessage = [NSString stringWithFormat:@"%@ updated the permissions to edit the group information".nim_localized,source];
                        break;
                    case NIMTeamUpdateTagMuteMode:{
                        NSString *muteState = teamAttachment.values.allValues.firstObject;
                        BOOL muted = [muteState isEqualToString:@"0"] ? NO : YES;
                        formatedMessage = muted? [NSString stringWithFormat:@"%@ muted all members".nim_localized,source]: [NSString stringWithFormat:@"%@ unmuted all members".nim_localized,source];
                        break;
                    }
                    default:
                        break;
                                
                }
            }
        }
        if (formatedMessage == nil){
            formatedMessage = [NSString stringWithFormat:@"%@ updated %@ information ".nim_localized,source,teamName];
        }
    }
    break;

Do-Not-Disturb

The SDK can set the configuration of the alert type for group notifications. The upper layer of your application can use the alert type to manage the behavior of group messages.

The alert types NIMTeamNotifyState include:

  • NIMTeamNotifyStateAll: Notify all members (default)
  • NIMTeamNotifyStateOnlyManager: only notify the group owner and administrators
  • NIMTeamNotifyStateNone: no alert for notifications

The alerts are used for [Push service and alerts] (https://doc.commsease.com/messaging/docs/DMxMjU0MDY?platform=iOS).

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

Modify the notification state by calling updateNotifyState:inTeam:completion:.

Parameters:

Parameter Description
teamId group ID
state The state of group notifications
NIMTeamNotifyStateAll: notify all members (default value)
NIMTeamNotifyStateOnlyManager: only notify the group owner and administrators
NIMTeamNotifyStateNone: no alerts for notifications
completion Callback for completion

** Sample code:**

/// The receiving state of group messages
    NIMTeamNotifyState notifyState = NIMTeamNotifyStateNone;
    /// teamId Grouo ID
    NSString *teamId = @"6271272396";
    /// completion Callback for completion
    NIMTeamHandler completion = ^(NSError * __nullable error)
    {
        if (error == nil) {
            /// Edit the state of a group notification, success
            NSLog(@"[Update notify state to %ld succeed.]", notifyState);
            /// your code ...
        } else {
            /// Edit the state of a group notification, failure
            NSLog(@"[NSError message: %@]", error);
        }
    };
    /// Edit the state of a group notification
    [[[NIMSDK sharedSDK] teamManager] updateNotifyState:notifyState
                                                 inTeam:teamId
                                             completion:completion];

Query Do-Not-Disturb

Get the alert type for group notification by calling notifyStateForNewMsg:.

** Sample code**

 /// teamId Grouo ID
    NSString *teamId = @"6271272396";
    /// Get the state of a group notification
    NIMTeamNotifyState notifyState = [[[NIMSDK sharedSDK] teamManager] notifyStateForNewMsg:teamId];

Read receipt for group messages

For read receipt for group messages and queries, see Read receipt for group messages.

API reference

API
Description
updateNotifyState Edit the state of a group notification.
notifyStateForNewMsg Query the alert type of group notifications.
refreshTeamMessageReceipts Update the read and unread count of group messages
sendTeamMessageReceipts Recipients mark group messages as read.
Was this page helpful?
Yes
No
  • Messaging in groups
  • Notification messages
  • Parse notification messages:
  • Do-Not-Disturb
  • Set the Do-Not-Disturb status
  • Query Do-Not-Disturb
  • Read receipt for group messages
  • API reference