php环信发送消息,环信即时通讯 —— 消息、会话
標(biāo)簽:
1、消息EMMessage
環(huán)信支持文字、圖片、位置、語音、視頻、文件、透傳、自定義八種消息構(gòu)造。消息,即IM交互實體,在SDK中對應(yīng)的類型是 **EMMessage**,EMMessage可以由多個符合協(xié)議的body組成,但是 推薦使用一個body。
2、消息構(gòu)造
1> 設(shè)置消息的內(nèi)容
文字消息:
EMChatText *msgChat = [[EMChatText alloc] initWithText:@"要發(fā)送的消息"];
圖片消息:
EMChatImage *msgChat = [[EMChatImage alloc] initWithUIImage:img displayName:@"displayName"];
位置消息:
EMChatLocation *msgChat = [[EMChatLocation alloc] initWithLatitude:35.1 longitude:35.1 address:@"地址"];
語音消息:
EMChatVoice *msgChat = [[EMChatVoice alloc] initWithFile:recordPath displayName:@"audio"];
voice.duration= aDuration;
視頻消息:
EMChatVideo *msgChat = [[EMChatVideo alloc] initWithFile:localPath displayName:@"displayName"];
文件消息:
EMChatFile *msgChat = [[EMChatFile alloc] initWithFile:localPath displayName:@"displayName"];
透傳消息 (SDK提供的一種特殊類型的消息,即CMD,不會存db,也不會走apns推送,類似一種指令型的消息,比如您的服務(wù)器要通知客戶端做某些操作,您可以服務(wù)器和客戶端提前約定好某個字段,當(dāng)客戶端收到約定好的字段時,執(zhí)行某種特殊操作):
EMChatCommand *msgChat =[[EMChatCommand alloc] init];
msgChat.cmd= @"reason";
2> 消息構(gòu)造,通過前面設(shè)置的消息內(nèi)容,生成body及消息:
EMCommandMessageBody *body =[[EMCommandMessageBody alloc] initWithChatObject:msgChat];//生成message
EMMessage *message = [[EMMessage alloc] initWithReceiver:@"接收消息的用戶名"bodies:@[body]];
自定義消息擴展:
有時候需要在消息中攜帶一些擴展內(nèi)容,用來實現(xiàn)特殊需求,比如閱后即焚等。EMMessage提供了ext屬性,撰文用來存放擴展內(nèi)容。ext屬性是NSDictionary類型,key和value必須是基本類型,且不能是json。可以這樣使用:EMMessage.ext = @{@”key”:@”value”};
3> 消息類型
message.messageType = eMessageTypeChat; //設(shè)置為單聊消息//message.messageType = eConversationTypeGroupChat;//設(shè)置為群聊消息//message.messageType = eConversationTypeChatRoom;//設(shè)置為聊天室消息
4> 插入消息
message.deliveryState =eMessageDeliveryState_Delivered;
[[EaseMob sharedInstance].chatManager insertMessageToDB:message];
3、更新消息屬性
/*!
@method
@brief 更新消息發(fā)送狀態(tài)
@result 是否更新成功*/
-(BOOL)updateMessageDeliveryStateToDB;/*!
@method
@brief 更新消息擴展屬性
@result 是否更新成功*/
-(BOOL)updateMessageExtToDB;/*!
@method
@brief 更新消息的消息體
@result 是否更新成功*/
-(BOOL)updateMessageBodiesToDB;/*!
@method
@brief 修改當(dāng)前 message 的發(fā)送狀態(tài), 下載狀態(tài)為 failed (crash 時或者 terminate)
@return 是否更新成功*/
- (BOOL)updateMessageStatusFailedToDB;
4、會話
會話:操作聊天消息EMMessage*的容器,在SDK中對應(yīng)的類型是EMConversation*;
1> ?創(chuàng)建會話
EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:@"聊天對象" conversationType:eConversationTypeChat];
2> ?刪除會話
// deleteMessage,是否刪除會話中的message,YES為刪除// append2Chat,是否更新內(nèi)存中內(nèi)容[[EaseMob sharedInstance].chatManager removeConversationByChatter:@"聊天對象" deleteMessages:YES append2Chat:YES]; ?//刪除單個會話
[[EaseMob sharedInstance].chatManager removeConversationsByChatters:chatters deleteMessages:YES append2Chat:YES]; //批量刪除會話
[[EaseMob sharedInstance].chatManager removeAllConversationsWithDeleteMessages:YES append2Chat:YES]; //刪除所有會話
3> ?獲取會話列表(三種方法)
EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:@"聊天對象" conversationType:eConversationTypeChat]; //直接創(chuàng)建
NSArray *conversations = [[EaseMob sharedInstance].chatManager conversations]; //獲取內(nèi)存中所有會話
NSArray *conversations = [[EaseMob sharedInstance].chatManager loadAllConversationsFromDatabaseWithAppend2Chat:YES]; //獲取DB中所有會話
4> ?獲取會話未讀消息數(shù)
[EMConversation unreadMessagesCount];
標(biāo)簽:
總結(jié)
以上是生活随笔為你收集整理的php环信发送消息,环信即时通讯 —— 消息、会话的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 32位Windows7上8G内存使用感受
- 下一篇: 动态规划算法php,php算法学习之动态