Chats

Start a new chat

You can start a new chat by sending a chat message:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// create content
val content = ChatMessageContent().withText("Hello, how are you?")

// create target, user who will receive the chat message
val target = ChatId.createWithUserId(UserId.create("otheruserid"))

// send the message
Communities.sendChatMessage(content, target,
        { result: ChatMessage ->
            print("Chat message sent.")
        }, { error: GetSocialError ->
            print("Failed to send chat message, error: $error")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// create content
let content = ChatMessageContent()
content.text = "Hello, how are you?"

// create target, user who will receive the chat message
let target = ChatId.create(UserId("otheruserid"))

// send the message
Communities.sendChatMessage(content, target: target, 
    success: { message in
        print("Chat message sent.")
    }, 
    failure: { error in
        print("Failed to send chat message, error: \(error)")
    })

Get existing chats

1
2
3
4
5
6
7
Communities.getChats(SimplePagingQuery.simple(50),
        { result: PagingResult<Chat> ->
            print("Existing chats: ${result.entries}")
        },
        { error: GetSocialError ->
            print("Failed to get existing chats, error: $error")
        })
1
2
3
4
5
6
7
Communities.chats(ChatsPagingQuery(), 
    success: { result in 
        print("Existing chats: \(result.chats)")
    }, 
    failure: { error in 
        print("Failed to get existing chats, error: \(error)")
    })

Retrieve chat messages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// load messages using chat id
val chatId = ChatId.create("chatId")
// or using user id
// val chatId = ChatId.createWithUserId(UserId.create("otheruserid"))

// create query
val query = ChatMessagesQuery.messagesInChat(chatId)

Communities.getChatMessages(ChatMessagesPagingQuery(query),
        { result: ChatMessagesPagingResult ->
            print("Chat messages: ${result.entries}")
            // store result.previousMessagesCursor to load more messages. If it's not empty, there are more messages to load.
            // store result.nextMessagesCursor to load more newer messages. Not empty only if you used `refreshCursor` and there are more messages to load.
            // store result.refreshCursor to load new messages.
        },
        { error: GetSocialError ->
            print("Failed to get chat messages, error: $error")
        })
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// load messages using chat id
let chatId = ChatId("chatId") 
// or using user id
//let chatId = ChatId(UserId("otheruserid")) 

// create query
let query = ChatMessagesQuery.inChat(chatId)

Communities.chatMessages(ChatMessagesPagingQuery(query), 
    success: { result in 
        print("Chat messages: \(result.messages)")
        // store result.previousMessagesCursor to load more messages. If it's not empty, there are more messages to load.
        // store result.nextMessagesCursor to load more newer messages. Not empty only if you used `refreshCursor` and there are more messages to load.
        // store result.refreshCursor to load new messages.
    }, 
    failure: { error in 
        print("Failed to get chat messages, error: \(error)")
    })

Load older messages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val query = ChatMessagesQuery.messagesInChat(chatId)
var pagingQuery = ChatMessagesPagingQuery(query)
pagingQuery = pagingQuery.withPreviousMessagesCursor("") // previousMessagesCursor from previous call result

Communities.getChatMessages(pagingQuery,
        { result: ChatMessagesPagingResult ->
            print("Chat messages: ${result.entries}")
        },
        { error: GetSocialError ->
            print("Failed to get chat messages, error: $error")
        })
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let query = ChatMessagesQuery.inChat(chatId)
let pagingQuery = ChatMessagesPagingQuery(query)
pagingQuery.previousMessagesCursor = // previousMessagesCursor from previous call result

Communities.chatMessages(pagingQuery, 
    success: { result in 
        print("Chat messages: \(result.messages)")
    }, 
    failure: { error in 
        print("Failed to get chat messages, error: \(error)")
    })

Load newer messages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val query = ChatMessagesQuery.messagesInChat(chatId)
var pagingQuery = ChatMessagesPagingQuery(query)
pagingQuery = pagingQuery.withNextMessagesCursor("") // refreshCursor from previous call result

Communities.getChatMessages(pagingQuery,
        { result: ChatMessagesPagingResult ->
            print("Chat messages: ${result.entries}")
        },
        { error: GetSocialError ->
            print("Failed to get chat messages, error: $error")
        })
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let query = ChatMessagesQuery.inChat(chatId)
let pagingQuery = ChatMessagesPagingQuery(query)
pagingQuery.nextMessagesCursor = // refreshCursor from previous call result

Communities.chatMessages(pagingQuery, 
    success: { result in 
        print("Chat messages: \(result.messages)")
    }, 
    failure: { error in 
        print("Failed to get chat messages, error: \(error)")
    })

Push notifications

On every chat message the recipient receives a push notification, which you can use to open the chat.

1
2
3
4
5
6
Notifications.setOnNotificationClickedListener({ notification, context ->
    if (notification.action != null && notification.action.type == ActionTypes.OPEN_CHAT) {
        val chatId = notification.action.data[ActionDataKeys.OpenChat.CHAT_ID]
        // open chat
    }
})
1
2
3
4
5
6
Notifications.setOnNotificationClickedListener({ notification, context in
    if let action = notification.notificationAction, action.type == ActionType.openChat {
        let chatId = action.data[ActionDataKey.openChat_ChatId]
        // open chat
    }
})

Give us your feedback! Was this article helpful?

😀 🙁