Chat Extension

Update time: 2021/12/03 01:55:29

Message replies

A message thread represents a group of messages, consisting of a parent message and message replies.

A message can have multiple replies in a thread. A message that has a thread of replies is a parent message. A parent message and the replies are called a message thread. Every message within a thread is a threaded message. A message that has no replies is an unthreaded message.

Replying to a message

  • API prototype
java/**
 * Reply to a message.<br>
 * If the sending progress must be updated, you must invoke {@link MsgServiceObserve#observeMsgStatus(com.netease.nimlib.sdk.Observer, boolean)}
 *
 * @param msg - Message body to be sent, constituted by {@link MessageBuilder}
 * @param replyMsg - Message being replied
 * @param resend - If a message is sent unsuccessfully and then re-sent, it is marked as "true". Otherwise, as "false"
 * @return InvocationFuture - Configurable callback feature. It can be invoked only when the message is sent. If an error occurs, a detailed error code will be returned.
 */
InvocationFuture<Void> replyMessage(IMMessage msg, IMMessage replyMsg, boolean resend);
  • Parameters
Parameter Description
msg Message body to be sent, constituted by {@link MessageBuilder}
replyMsg Replied message
resend If a message is sent unsuccessfully and then sent again, it is marked as "true". Otherwise, as "false".
  • Example
javaNIMClient.getService(MsgService.class).replyMessage(message, replymessage.get(0), false).setCallback(new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    //Reply to a message
                }

                @Override
                public void onFailed(int code) {
                  // Failed to replay to a message. Code is error code.
                }

                @Override
                public void onException(Throwable exception) {
                  // Exception in replying to a message
                }
            })

Notes: a message is replied with superteamService in a superteam.

Querying the threaded chat history on cloud

  • API prototype
java/**
 * Query threaded chat history on cloud (available to peer-to-peer, team, superteam)
 *
 * @param anchor - Find anchor. The queried object is the message in corresponding thread.
 * @param fromTime - Start time
 * @param toTime - End time
 * @param limit - Number limit
 * @param direction - Direction
 * @param persist - It determines to persist.
 * @return InvocationFuture
 */
InvocationFuture<ThreadTalkHistory> queryThreadTalkHistory(IMMessage anchor, long fromTime, long toTime, int limit, QueryDirectionEnum direction, boolean persist);
  • Parameters
Parameter Description
anchor Find the anchor. Find object is the message in the corresponding thread
fromTime Start time
toTime End time
limit Limit for the number of messages
direction Direction
persist Determine that it is persisted
  • Example
javaNIMClient.getService(MsgService.class).queryThreadTalkHistory(replymessage.get(0), aTime, tTime, msgCount, direction, true).setCallback(new RequestCallback<ThreadTalkHistory>() {
    @Override
    public void onSuccess(ThreadTalkHistory thread) {
        if (thread == null) {
            Toast.makeText(SessionExtension.this, R.string.msg_history_empty, Toast.LENGTH_SHORT).show();
            return;
        }
        MsgHistoryActivity.startActivity(SessionExtension.this, thread.getReplyList());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable throwable) {
        Log.e(TAG, "pull server message exception:" + throwable);
    }
});

Getting the number of replies to a threaded message

Get the number of replies to a threaded message from a local database.

  • API prototype
java/**
 * Get the replied message count for a threaded message from the local message. Thread message is not included in the total count.
 *
 * @param msg - A certain message in a thread
 * @return - Replied message count
 */
int queryReplyCountInThreadTalkBlock(IMMessage msg);
  • Parameters
Parameter Description
msg A certain message in a thread
  • Example
javaint count = NIMClient.getService(MsgService.class).queryReplyCountInThreadTalkBlock(replymessage.get(0));

Quick comments

You can add a quick comment to a message. The comment content is long type, instead of a message. The correlation between comment content and interface display is specified by the upper layer.

Adding a comment

  • API prototype
java/**
 * Add a quick comment
 *
 * @param msg - Reply object
 * @param replyType - Reply type
 * @param ext - Custom extension field, max. size 8 characters
 * @param needPush - It determines that push is required.
 * @param needBadge - It determines that corner mark is required.
 * @param pushTitle - Push title
 * @param pushContent - Push content
 * @param pushPayload - The third-party custom push attribute, limited to json type
 * @return InvocationFuture
 */
InvocationFuture<Long> addQuickComment(IMMessage msg, long replyType, String ext, boolean needPush, boolean needBadge, String pushTitle, String pushContent, Map<String, Object> pushPayload);
  • Parameters
Parameter Description
msg Reply object
replyType Reply type
ext User-defined extension field, max. size 8 characters
needPush Determine if a push is needed
needBadge Determine that corner mark is needed
pushTitle Push title
pushContent Push content
pushPayload The third-party defined push attribute, limited to json type
  • Example
javaNIMClient.getService(MsgService.class).addQuickComment(message, replytype, qcmsgExtEdit.getText().toString(), needpush, needbadge,
        pushTitleEdit.getText().toString(), pushContentEdit.getText().toString(), null).setCallback(new RequestCallback<Long>() {
    @Override
    public void onSuccess(Long param) {
        Toast.makeText(SessionExtension.this, "Add a quick comment successfully", Toast.LENGTH_SHORT).show();
        showOnLog(logText, logJson.toString());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Deleting a comment

  • API prototype
java/**
 * Delete a quick comment
 *
 * @param msg - Reply object
 * @param replyType - Reply type
 * @param ext - Custom extension field, max. size 8 characters
 * @param needPush - It determines that push is required.
 * @param needBadge - It determines that corner mark is required.
 * @param pushTitle - Push title
 * @param pushContent - Push content
 * @param pushPayload - The third-party custom push attribute, limited to json type
 * @return InvocationFuture
 */
InvocationFuture<Long> removeQuickComment(IMMessage msg, long replyType, String ext, boolean needPush, boolean needBadge, String pushTitle, String pushContent, Map<String, Object> pushPayload);
  • Parameters
Parameter Description
msg Reply object
replyType Reply type
ext User-defined extension field, max. size 8 characters
needPush Determine if a push is needed
needBadge Determine that corner mark is needed
pushTitle Push title
pushContent Push content
pushPayload The third-party defined push attribute, limited to json type
  • Example
javaNIMClient.getService(MsgService.class).removeQuickComment(message, replytype, qcmsgExtEdit.getText().toString(), needpush, needbadge,
        pushTitleEdit.getText().toString(), pushContentEdit.getText().toString(), null).setCallback(new RequestCallback<Long>() {
    @Override
    public void onSuccess(Long param) {
        Toast.makeText(SessionExtension.this, "Delete a quick comment successfully", Toast.LENGTH_SHORT).show
        showOnLog(logText, logJson.toString());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Getting comments

  • API prototype
java/**
 * Get the quick comments.
 *
 * @param msgList - Each element is the message of queried quick comment, of which the number cannot exceed 20.
 * @return InvocationFuture
 */
InvocationFuture<List<QuickCommentOptionWrapper>> queryQuickComment(List<IMMessage> msgList);
  • Parameters
Parameter Description
msgList Each element is a message which is queried for a quick comment, of which the number cannot exceed 20.
  • Example
javaNIMClient.getService(MsgService.class).queryQuickComment(msglist).setCallback(new RequestCallback<List<QuickCommentOptionWrapper>>() {
            @Override
            public void onSuccess(List<QuickCommentOptionWrapper> param) {
                showQuickComment(logText, logJson.toString());
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        });

Favorites

Users can add a character string within 20 kb in size to Favorites.

Adding a comment to Favorites

  • API prototype
java/**
 * Add a collection
 *
 * @param type - Collection
 * @param date - Collection content, max. size 20k
 * @param ext - Extension field, max. size 1k
 * @param uniqueId - De-duplicate the unique ID
 * @return InvocationFuture
 */
InvocationFuture<CollectInfo> addCollect(int type, String date, String ext, String uniqueId);
  • Parameters
Parameter Description
type Type of favorite
date Favorite content, max. size 20k
ext Extension field, max. size 1k
uniqueId A unique ID
  • Example
javaNIMClient.getService(MsgService.class).addCollect(1, logJson.toString(), extEdit.getText().toString(), UniqueIdEdit.getText().toString()).setCallback(
        new RequestCallback<CollectInfo>() {
            @Override
            public void onSuccess(CollectInfo param) {
                Toast.makeText(SessionExtension.this, "Add a collection successfully", Toast.LENGTH_SHORT).show();
                showOnLog(logText, logJson.toString());
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable exception) {
                Log.e(TAG, "pull server message exception:" + exception);
            }
        }
);

Removing multiple messages from Favorites at a time

  • API prototype
java/**
 * Remove collections at a time
 *
 * @param collectInfo - List of key information about collections to be removed. Each option is a Pair. The first option is collection ID and the second option is creation time of collection.
 * @see CollectInfo
 * @return InvocationFuture
 */
InvocationFuture<Integer> removeCollect(List<Pair<Long, Long>> collectInfo);
  • Parameters
Parameter Description
collectInfo List of key information to be removed from favorites. Each item is a Pair. The first item is the ID of favorite and the second item is the creation time of favorite.
  • Example
javaNIMClient.getService(MsgService.class).removeCollect(collectInfo).setCallback(
        new RequestCallback<Integer>() {
            @Override
            public void onSuccess(Integer param) {
                Toast.makeText(SessionExtension.this, "Remove collections at a time successfully", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable exception) {
                Log.e(TAG, "pull server message exception:" + exception);
            }
        }
);

Updating the extension field of Favorites

  • API prototype
java/**
 * Update extension field of a collection
 *
 * @param infoId - ID of collection to be updated
 * @param createTime - Creation time of collection to be updated
 * @param ext - Extension field after updat
 * @return InvocationFuture
 */
InvocationFuture<CollectInfo> updateCollect(long infoId, long createTime, String ext);
  • Parameters
Parameter Description
infoId ID of favorite to be updated
createTime Creation time of favorite to be updated
ext Updated extension field
  • Example
javaNIMClient.getService(MsgService.class).updateCollect(collectId,collectCreateTime,extEdit.getText().toString()).setCallback(new RequestCallback<CollectInfo>() {
    @Override
    public void onSuccess(CollectInfo param) {
        Toast.makeText(SessionExtension.this, "Update extension field of a collection successfully", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Querying Favorites by page

  • API prototype
java/**
 * Query list of collections by paging from the server
 *
 * @param anchor - Final collection in end query (not included in query results)
 * @param toTime - End time point, unit: ms
 * @param limit   upper limit of messages for this query (up to 100 messages)
 * @param direction query direction
 * @return InvocationFuture
 */
InvocationFuture<CollectInfoPage> queryCollect(CollectInfo anchor, long toTime, int limit, QueryDirectionEnum direction);
  • Parameters
Parameter Description
anchor Final favorite of end query (not included in query results)
toTime End time. Unit: millisecond
limit Limit for the number of messages for query (max. 100)
direction Query direction
  • Example
javaNIMClient.getService(MsgService.class).queryCollect(null, tTime, limit, direction).setCallback(
        new RequestCallback<CollectInfoPage>() {
            @Override
            public void onSuccess(CollectInfoPage param) {
                if (param == null) {
                    Toast.makeText(SessionExtension.this, "The list of collections queried by paging from the server is null",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                addCollects(param.getCollectList());
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        }

        );

PIN tags

All users of a session can mark PIN for a message, cancel PIN, query PIN, and update the extension field. A message can be marked with one PIN only. If multiple users mark PIN for a message, the final PIN will cover the early PIN.

Adding a PIN tag to a message

  • API prototype
java/**
 * Add a PIN tag to a message
 *
 * @param msg - Message marked with PIN
 * @param ext extension field
 * @return InvocationFuture
 */
InvocationFuture<Long> addMsgPin(IMMessage msg, String ext);
  • Parameters
Parameter Description
msg Message marked with PIN
ext Extension field
  • Example
javaNIMClient.getService(MsgService.class).addMsgPin(message, pinmsgExtEdit.getText().toString()).setCallback(new RequestCallback<Long>() {
    @Override
    public void onSuccess(Long param) {
        Toast.makeText(SessionExtension.this, "Mark PIN for a message successfully", Toast.LENGTH_SHORT).show();
        showOnLog(logText, logJson.toString());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Updating the PIN tag of a message

  • API prototype
java/**
 * Update PIN of a message
 *
 * @param msg - Message marked with PIN
 * @param ext extension field
 * @return InvocationFuture
 */
InvocationFuture<Long> updateMsgPin(IMMessage msg, String ext);
  • Parameters
Parameter Description
msg Message marked with PIN
ext Extension field
  • Example
javaNIMClient.getService(MsgService.class).updateMsgPin(message, pinmsgExtEdit.getText().toString()).setCallback(new RequestCallback<Long>() {
    @Override
    public void onSuccess(Long param) {
        Toast.makeText(SessionExtension.this, "Update PIN of a message successfully", Toast.LENGTH_SHORT).show();
        showOnLog(logText, logJson.toString());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Remove the PIN tag from a message

  • API prototype
java/**
 * Remove PIN of a message
 *
 * @param msg - Message marked with PIN
 * @param ext extension field
 * @return InvocationFuture
 */
InvocationFuture<Long> removeMsgPin(IMMessage msg, String ext);
  • Parameters
Parameter Description
msg Message marked with PIN
ext Extension field
  • Example
javaNIMClient.getService(MsgService.class).removeMsgPin(message, pinmsgExtEdit.getText().toString()).setCallback(new RequestCallback<Long>() {
    @Override
    public void onSuccess(Long param) {
        Toast.makeText(SessionExtension.this, "Remove PIN of a message successfully", Toast.LENGTH_SHORT).show();
        showOnLog(logText, logJson.toString());
    }

    @Override
    public void onFailed(int code) {
        Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onException(Throwable exception) {
        Log.e(TAG, "pull server message exception:" + exception);
    }
});

Syncing PIN tags in a session

  • API prototype
java/**
 * Sync PIN tags of a session
 *
 * @param sessionType session type
 * @param sessionId - Session ID
 * @param timestamp - Timestamp, PIN after synchronizing timestamp
 * @return InvocationFuture
 */
InvocationFuture<MsgPinSyncResponseOptionWrapper> syncMsgPin(SessionTypeEnum sessionType, String sessionId, long timestamp);
  • Parameters
Parameter Description
sessionType Session type
sessionId Session ID
timestamp timestamp, PIN after synchronizing timestamp
  • Example
javaNIMClient.getService(MsgService.class).syncMsgPin(getSessionType(), recipientEdit.getText().toString(), 0).setCallback(new RequestCallback<MsgPinSyncResponseOptionWrapper>() {
    @Override
    public void onSuccess(MsgPinSyncResponseOptionWrapper msgPinSync) {
        if (msgPinSync == null) {
            Toast.makeText(SessionExtension.this, "PIN information about a session queried in server is null",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if (!msgPinSync.isChanged()) {
            Toast.makeText(SessionExtension.this, "No change", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<IMMessage> msgList = new ArrayList<>();
        for (MsgPinSyncResponseOption infoList : msgPinSync.getMsgPinInfoList()) {
            String uuid = infoList.getKey().getUuid();
            IMMessage msg = MsgDBHelper.queryMessageByUuid(uuid);
            if (msg == null) {
                continue;
            }
            msgList.add(msg);
        }
        MsgHistoryActivity.startActivity(SessionExtension.this, msgList);
    }

    @Overridetong
    public void onFailed(int code) {

    }

    @Override
    public void onException(Throwable exception) {

    }
});

Getting the messages with PIN tags

  • API prototype
java/**
 * Get the list of PIN information about sessions from the local database
 *
 * @param sessionId - Session ID
 * @param sessionType session type
 * @return - List of PIN information
 */
List<MsgPinDbOption> queryMsgPinBlock(String sessionId, SessionTypeEnum sessionType);
  • Parameters
Parameter Description
sessionId Session ID
sessionType Session type
  • Example
javaList<MsgPinDbOption> msgPinDbOptions = NIMClient.getService(MsgService.class).queryMsgPinBlock(recipientEdit.getText().toString(), getSessionType());

Pinning sessions

Pinning a session

  • API prototype
java/**
 * Add a pinned session
 *
 * @param sessionId - Session ID
 * @param sessionType session type
 * @param ext - Extension field, max. size 512 characters
 * @return InvocationFuture
 */
InvocationFuture<StickTopSessionInfo> addStickTopSession(String sessionId, SessionTypeEnum sessionType, String ext);
  • Parameters
Parameter Description
sessionId Session ID
sessionType Session type
ext Extension field, can contain up to 512 characters
  • Example
javaNIMClient.getService(MsgService.class).addStickTopSession(recipientEdit.getText().toString(), getSessionType(), extEdit.getText().toString()).setCallback(
        new RequestCallback<StickTopSessionInfo>() {
            @Override
            public void onSuccess(StickTopSessionInfo param) {
                Toast.makeText(SessionExtension.this, "Add a pinned session successfully", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable exception) {
                Log.e(TAG, "pull server message exception:" + exception);
            }
        }
);

Deleting a pinned session

  • API prototype
java/**
 * Delete a stick top session
 *
 * @param sessionId - Session ID
 * @param sessionType session type
 * @param ext - Extension field, max. size 512 characters
 * @return InvocationFuture
 */
InvocationFuture<Void> removeStickTopSession(String sessionId, SessionTypeEnum sessionType, String ext);
  • Parameters
Parameter Description
sessionId Session ID
sessionType Session type
ext Extension field, can contain up to 512 characters
  • Example
javaNIMClient.getService(MsgService.class).removeStickTopSession(recipientEdit.getText().toString(), getSessionType(), extEdit.getText().toString()).setCallback(
        new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void param) {
                Toast.makeText(SessionExtension.this, "Delete a stick top session successfully", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable exception) {
                Log.e(TAG, "pull server message exception:" + exception);
            }
        }
);

Updating an extension field

  • API prototype
java/**
 * Update an extension field of a pinned session
 *
 * @param sessionId - Session ID
 * @param sessionType session type
 * @param ext - Extension field, max. size 512 characters
 * @return InvocationFuture
 */
InvocationFuture<StickTopSessionInfo> updateStickTopSession(String sessionId, SessionTypeEnum sessionType, String ext);
  • Parameters
Parameter Description
sessionId Session ID
sessionType Session type
ext Extension field, can contain up to 512 characters
  • Example
javaNIMClient.getService(MsgService.class).updateStickTopSession(recipientEdit.getText().toString(), getSessionType(), extEdit.getText().toString()).setCallback(
        new RequestCallback<StickTopSessionInfo>() {
            @Override
            public void onSuccess(StickTopSessionInfo param) {
                Toast.makeText(SessionExtension.this, "Update extension field of a stick top session successfully", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(SessionExtension.this, "Failed:" + code, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable exception) {
                Log.e(TAG, "pull server message exception:" + exception);
            }
        }
);

Getting the list of pinned sessions

Notes: You must set "notifyStickTopSession" of initialization parameter "SDKOptions" as "true" and then enable the feature of synchronizing top session list to query detailed top session list using the following interface. Otherwise, the queried list does not include offline alteration.

  • API prototype
java/**
 * Get the list of information about pinned sessions
 *
 * @return - List of stick top information
 */
List<StickTopSessionInfo> queryStickTopSessionBlock();
  • Example
javaList<StickTopSessionInfo> sticktopsession = NIMClient.getService(MsgService.class).queryStickTopSessionBlock();
Was this page helpful?
Yes
No
  • Message replies
  • Replying to a message
  • Querying the threaded chat history on cloud
  • Getting the number of replies to a threaded message
  • Quick comments
  • Adding a comment
  • Deleting a comment
  • Getting comments
  • Favorites
  • Adding a comment to Favorites
  • Removing multiple messages from Favorites at a time
  • Updating the extension field of Favorites
  • Querying Favorites by page
  • PIN tags
  • Adding a PIN tag to a message
  • Updating the PIN tag of a message
  • Remove the PIN tag from a message
  • Syncing PIN tags in a session
  • Getting the messages with PIN tags
  • Pinning sessions
  • Pinning a session
  • Deleting a pinned session
  • Updating an extension field
  • Getting the list of pinned sessions