Messaging services

Conversations

Create conversation

CMPRoleAttributes *ownerAttributes = [[CMPRoleAttributes alloc] initWithCanSend:YES canAddParticipants:YES canRemoveParticipants:YES];

CMPRoleAttributes *participantAttributes = [[CMPRoleAttributes alloc] initWithCanSend:YES canAddParticipants:NO canRemoveParticipants:NO];

// defines permissions to be applied to conversation
CMPRoles *roles = [[CMPRoles alloc] initWithOwnerAttributes:ownerAttributes participantAttributes:participantAttributes];

CMPConversationParticipant *owner = [[CMPConversationParticipant alloc] initWithID:profileID role:@"owner"];

CMPNewConversation *newConversation = [[CMPNewConversation alloc] initWithID:@"{id}" name:@"{name}" description:@"{description}" roles:roles participants:@[owner] isPublic:@(NO)];

[client.services.messaging addConversation:newConversation completion:^(CMPChatResult<CMPConversation *> *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Update conversation

CMPConversationUpdate *update = [[CMPConversationUpdate alloc] initWithID:@"{id}" name:@"{name}" description:@"{description}" roles:roles isPublic:@(NO)];

[client.services.messaging updateConversation:@"{id}" eTag:nil conversation:update completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Delete conversation

[client.services.messaging deleteConversation:@"{id}" eTag:nil completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Participants

Get participants

[client.services.messaging getParticipants:@"{id}" participantIDs:@[@"{id1}", @"{id2}"] completion:^(NSArray<CMPChatParticipant *> *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Add participants

NSArray<CMPConversationParticipant *> *participants = @[[[CMPConversationParticipant alloc] initWithID:@"@{id1}" role:@"{role}"], [[CMPConversationParticipant alloc] initWithID:@"@{id2}" role:@"{role}"]];

[client.services.messaging addParticipants:@"{id}" participants:participants completion:^(CMPChatResult *result) {
    if (result.error) {
    		// error occurred
    } else {
      	// success
    }
}];

Remove participants

[client.services.messaging removeParticipants:@"{id}" participants:participants completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Set participant typing

[client.services.messaging participantIsTyping:@"{conversationId}" isTyping:true completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Messages

Send message

CMPMessagePart *part = [[CMPMessagePart alloc] initWithName:@"{name}" type:@"{type}" url:nil data:@"{data}" size:@(123)];

CMPMessageAlert *alert = [[CMPMessageAlert alloc] initWithPlatforms:[[CMPMessageAlertPlatforms alloc] initWithApns:@{} fcm:@{}]];
    
CMPSendableMessage *message = [[CMPSendableMessage alloc] initWithMetadata:@{} parts:@[part] alert:alert];

[client.services.messaging sendMessage:@"{id}" message:message completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Send message with attachments

ContentData *dataFromUrl = [[ContentData alloc] initWithUrl:url type:@"type" name:@"name"];

CMPChatAttachment *attachment = [[CMPChatAttachment alloc] initWithContentData:dataFromUrl folder:@"folder"];

[client.services.messaging sendMessage:@"{id}" message:message attachments:@[attachment] completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Mark message as read

[client.services.messaging markMessagesAsRead:@"{id}" messageIDs:@[@"{id1}", @"{id2}"] completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

Synchronise local store

The local data synchronisation is called by the SDK on a regular basis, but can be triggered manually as well.

Local store synchronisation updates conversations and messages.

[client.services.messaging synchroniseStore:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];

You can also update message data for a single conversation:

[client.services.messaging synchroniseConversation:@"{id}" completion:^(CMPChatResult *result) {
    if (result.error) {
        // error occurred
    } else {
        // success
    }
}];