Skip to content

Chats

Start a new chat

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

// 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")
})
// 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)")
    })
// crate content
var content = new ChatMessageContent();
content.text = "Hello, how are you?"

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

// send the message
Communities.SendChatMessage(content, target, (message) => {
        Debug.Log("Chat message sent");
    }, (error) => {
        Debug.Log("Failed to send message, error: " + error);
    }
);
// create content
const content = new ChatMessageContent();
content.text = 'Hello, how are you?';

// create target, user who will receive the chat message
const target = ChatId.createWithUserId(UserId.create('otheruserid'));

// send the message
Communities.sendChatMessage(content, target)
    .then((result) => {
        console.log('Chat message sent');
    }, (error) => {
        console.log('Failed to send chat message, error: ' + error);
    });

Get existing chats

Communities.getChats(SimplePagingQuery.simple(50),
        { result: PagingResult<Chat> ->
            print("Existing chats: ${result.entries}")
        },
        { error: GetSocialError ->
            print("Failed to get existing chats, error: $error")
        })
Communities.chats(ChatsPagingQuery(),
    success: { result in
        print("Existing chats: \(result.chats)")
    },
    failure: { error in
        print("Failed to get existing chats, error: \(error)")
    })
Communities.Chats(new SimplePagingQuery(), (result) => {
        Debug.Log("Existing chats:" + result.toString());
    }, (error) => {
        Debug.Log("Failed to get existing chats, error: " + error);
    }
);
Communities.getChats(new PagingQuery())
    .then((result) => {
        console.log('Existing chats: ' + result.entries);
    }, (error) => {
        console.log('Failed to get existing chats, error: ' + error);
    });

Retrieve chat messages

// 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")
        })
// 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 messages using chat id
var chatId = ChatId.Create("chatid");
// or using user id
//var chatId = ChatId.Create(UserId.Create("otheruserid"))

// create query
var query = ChatMessagesQuery.InChat(chatId);

Communities.GetChatMessages(new ChatMessagesPagingQuery(query), (result) => {
        Debug.Log("Chat messages: " + result.Messages.ToString());
        // 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) => {
        Debug.Log("Failed to get chat messages, error: " + error);
    }
);
// load messages using chat id
const chatId = ChatId.create('chatId');
// or using user id
//const chatId = ChatId.createWithUserId(UserId.create('otheruserid'));

// create query
const query = ChatMessagesQuery.messagesInChat(chatId);

Communities.getChatMessages(new ChatMessagesPagingQuery(query))
    .then((result) => {
        console.log('Chat messages: ' + result.entries);
        // store result.previous to load more messages. If it's not empty, there are more messages to load.
        // store result.next 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) => {
        console.log('Failed to get chat messages, error: ' + error);
    });

Load older messages

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")
        })
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)")
    })
var chatId = ChatId.Create("chatid");
var query = ChatMessagesQuery.InChat(chatId);
var pagingQuery = new ChatMessagesPagingQuery(query);
pagingQuery = pagingQuery.WithPreviousMessagesCursor(""); // previousMessagesCursor from previous call result

Communities.GetChatMessages(pagingQuery, (result) => {
        Debug.Log("Chat messages: " + result.Messages.ToString());
    }, (error) => {
        Debug.Log("Failed to get chat messages, error: " + error);
    }
);
const query = ChatMessagesQuery.messagesInChat(chatId);
const pagingQuery = new ChatMessagesPagingQuery(query);
pagingQuery.previous = // previous cursor from previous call result
Communities.getChatMessages(pagingQuery)
    .then((result) => {
        console.log('Chat messages: ' + result.entries);
    }, (error) => {
        console.log('Failed to get chat messages, error: ' + error);
    });

Load newer messages

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")
        })
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)")
    })
var chatId = ChatId.Create("chatid");
var query = ChatMessagesQuery.InChat(chatId);
var pagingQuery = new ChatMessagesPagingQuery(query);
pagingQuery = pagingQuery.WithNextMessagesCursor(""); // refreshCursor from previous call result

Communities.GetChatMessages(pagingQuery, (result) => {
        Debug.Log("Chat messages: " + result.Messages.ToString());
    }, (error) => {
        Debug.Log("Failed to get chat messages, error: " + error);
    }
);
const query = ChatMessagesQuery.messagesInChat(chatId);
const pagingQuery = new ChatMessagesPagingQuery(query);
pagingQuery.next = // refreshCursor from previous call result
Communities.getChatMessages(pagingQuery)
    .then((result) => {
        console.log('Chat messages: ' + result.entries);
    }, (error) => {
        console.log('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.

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
    }
})
Notifications.setOnNotificationClickedListener({ notification, context in
    if let action = notification.notificationAction, action.type == ActionType.openChat {
        let chatId = action.data[ActionDataKey.openChat_ChatId]
        // open chat
    }
})
Notifications.SetOnNotificationClickedListener((notification, context) => {
    if (action != null && action.Type.Equals(GetSocialActionType.OpenChat)) {
        var chatId = action.Data[GetSocialActionKeys.OpenChat.ChatId];
        // open chat
    }
});
Notifications.setOnNotificationClickedListener((notification, context) => {
    if (notification.action != null && notification.action.type == 'open_chat') {
        const chatId = notification.action.data('$chat_id');
        // open chat
    }
});

Give us your feedback! Was this article helpful?

😀 🙁