IM SDK Callbacks

Last updated: 2021-01-05 10:11:30

    IM SDK Event Callbacks

    TIMRecvNewMsgCallback

    Callback for new messages.

    Prototype

    typedef void (*TIMRecvNewMsgCallback)(const char* json_msg_array, const void* user_data);

    Parameters

    Parameter Type Description
    json_msg_array Const char* New message array
    user_data Const void* User-defined data, which is passed through by the IM SDK without being processed.

    Note:

    This callback is used to obtain the newly received message array. Note that the elements in the message are also an array, and each element is defined by elem_type.

    Example 1: parse the message array.

    Json::Value json_value_msgs; // Parse the message.
    Json::Reader reader;
    if (!reader.parse(json_msg_array, json_value_msgs)) {
        printf("reader parse failure!%s", reader.getFormattedErrorMessages().c_str());
        return;
    }
    for (Json::ArrayIndex i = 0; i < json_value_msgs.size(); i++) {  // Traverse the messages.
        Json::Value& json_value_msg = json_value_msgs[i];
        Json::Value& elems = json_value_msg[kTIMMsgElemArray];
        for (Json::ArrayIndex m = 0; m < elems.size(); m++) {   // Traverse the elements.
            Json::Value& elem = elems[i];
    
            uint32_t elem_type = elem[kTIMElemType].asUInt();
            if (elem_type == TIMElemType::kTIMElem_Text) {  // Text
    
            } else if (elem_type == TIMElemType::kTIMElem_Sound) {  // Sound
    
            } else if (elem_type == TIMElemType::kTIMElem_File) {  // File
    
            } else if (elem_type == TIMElemType::kTIMElem_Image) { // Image
    
            } else if (elem_type == TIMElemType::kTIMElem_Custom) { // Custom element
    
            } else if (elem_type == TIMElemType::kTIMElem_GroupTips) { // Group system message
    
            } else if (elem_type == TIMElemType::kTIMElem_Face) { // Emoji
    
            } else if (elem_type == TIMElemType::kTIMElem_Location) { // Location
    
            } else if (elem_type == TIMElemType::kTIMElem_GroupReport) { // Group system notification
    
            } else if (elem_type == TIMElemType::kTIMElem_Video) { // Video
    
            }
        }
    }

    Example 2: return the JSON string of a text message. For details on the JSON keys, see Message and TextElem.

    [
       {
          "message_client_time" : 1551080111,
          "message_conv_id" : "user2",
          "message_conv_type" : 1,
          "message_elem_array" : [
             {
                "elem_type" : 0,
                "text_elem_content" : "123213213"
             }
          ],
          "message_is_from_self" : true,
          "message_is_read" : true,
          "message_rand" : 2130485001,
          "message_sender" : "user1",
          "message_seq" : 1,
          "message_server_time" : 1551080111,
          "message_status" : 2
       }
    ]

    Example 3: return the JSON string of a group notification. For details on the JSON keys, see Message and GroupReportElem.

    [
       {
          "message_client_time" : 1551344977,
          "message_conv_id" : "",
          "message_conv_type" : 3,
          "message_elem_array" : [
             {
                "elem_type" : 9,
                "group_report_elem_group_id" : "first group id",
                "group_report_elem_group_name" : "first group name",
                "group_report_elem_msg" : "",
                "group_report_elem_op_group_memberinfo" : {
                   "group_member_info_custom_info" : {},
                   "group_member_info_identifier" : "user1",
                   "group_member_info_join_time" : 0,
                   "group_member_info_member_role" : 0,
                   "group_member_info_msg_flag" : 0,
                   "group_member_info_msg_seq" : 0,
                   "group_member_info_name_card" : "",
                   "group_member_info_shutup_time" : 0
                },
                "group_report_elem_op_user" : "",
                "group_report_elem_platform" : "Windows",
                "group_report_elem_report_type" : 6,
                "group_report_elem_user_data" : ""
             }
          ],
          "message_is_from_self" : false,
          "message_is_read" : true,
          "message_rand" : 2207687390,
          "message_sender" : "@TIM#SYSTEM",
          "message_seq" : 1,
          "message_server_time" : 1551344977,
          "message_status" : 2
       }
    ]

    Example 4: return the JSON string of a group tip. For details on the JSON keys, see Message and GroupTipsElem.

    [
       {
          "message_client_time" : 1551412814,
          "message_conv_id" : "first group id",
          "message_conv_type" : 2,
          "message_elem_array" : [
             {
                "elem_type" : 6,
                "group_tips_elem_changed_group_memberinfo_array" : [],
                "group_tips_elem_group_change_info_array" : [
                   {
                      "group_tips_group_change_info_flag" : 10,
                      "group_tips_group_change_info_value" : "first grou