Talk not much, first look at the good chat software interface:
First drag a uitableview and a view in the storyboard to enter text or voice, the right button to switch text and voice:
There are three kinds of id:orderid in the chat: Chat Idmessageid: Idsessionid per message: The session ID of each order, if it is empty through OrderID request.
then do some interface operations and some initialization operations in Viewdidload:
1. Set the TableView Headview
2. Initialize recording, user avatar, get Order Details
<strong> </strong><span style= "FONT-SIZE:14PX;" >//initial change of recording [self Initrecord]; Get user avatar [self getheadimg]; Get Order Details [self getorderdetailinfo];</span>
3. In Viewwillappear:A. Registration of notifications for keyboard and chat messagesB. Started a 20-second Nstimer poll to get chat messages
C.self.chatarray reads a chat message from the database if the array is empty and returns if the TableView is not empty.d. If you are coming from the evaluation page, refresh the order status
4. Look at the code for the polling message:
<span style= "FONT-SIZE:14PX;" > #pragma mark-Polling message-(void) Runloopmessage { Speaktype speaker = [Ycchatinfo Getspeakerpassengerby: Self.orderInfo.bigType]; [[Ycreceivemessagecenter Defaultmessagecenter] getmessagelistbyspeaker:speaker isremote:no AndPushId:nil ];} </span>
The first line of code is to get the session type, where an enumeration value is defined, mainly with the following roles:
Session type enum Speaktype { //business ycdrivertype = one, ycpassenger =, ycsystem = 13, //v5.2.2 Adding system roles Ycloctionautoreply = +, //v5.2.2 add Local auto reply ycdriverautoreply =, //v5.2.2 Add driver automatically reply driver role ycloctionupdateversionreply =//v5.2.2 Unknown message type reply "Prompt for unsupported message types. Please upgrade "};typedef Nsinteger Speaktype;
The Chat list interface is then requested based on the session type, and then the data is processed after the request succeeds:
Isremote Click on the push bar message if (response && [response[@ "Ret_code"] integervalue] = =) { Nsarray *array = respon se[@ "Result"]; ID TOPVC = [[Ycappdelegate shareddelegate] topviewcontroller]; __block nsstring *orderid = nil; __block nsstring *dtype = nil; [Array enumerateobjectsusingblock:^ (Nsdictionary *result, Nsuinteger idx, BOOL *stop) { NSString *type = [self controlmessagedispatch:result]; DType = type; <span style= "White-space:pre" ></span>//omitted here 500 Words } } else { DLog (@ "Polling data failed response =%@, Error =%@ ", response, error); }
If code = = 200 proves that the request was successful, and then uses the array to fetch all the chat messages, and then uses the Enumerateobjectsusingblock method to facilitate each element in the array, each element is a chat message. The type of message (DType) is then obtained through Controlmessagedispatch:
typedef ns_enum (Nsinteger, ClassType) { Orderclass = 1, chatclass = 2, userclass = 3,};
The first one is the order type message, the second is the chat type message, and the third is the account message. If it is an order message, according to the type to determine what the state, and then to send a different notification. If the chat type has the result passed in:
-(void) Receivechatmessage: (ID) object
method, and then there is a message status field kchatrepeatstate, if kchatrepeatstate = = 20, which represents the read message, is returned directly. If not, initialize Ycchatinfo with content:
Nsdictionary *content = dic[@ "Content"]; Ycchatinfo *item;item = [[Ycchatinfo alloc] initwithdictionary:content];
Then to judge the Chattype, there are several:
The chat type enum Chattype { Chattext = 1, chatimage = 2, chataudio = 3, chatpoi = 4, Chatmix = 5,//mixed content Chatcard = 6,//v5.2.2 card message chatupdatehint = 701//v5.2.2 does not support type escalation hints } ; typedef Nsinteger Chattype;
If it is a voice message, you need to go to the asynchronous first request to download the voice message, after the download is displayed to the interface of the colleague to set the unread message and then stored in the database, Then go back to the main line Cheng notifactionname:kchatmessagenotification notify the chat interface to receive chat messages, display the interface and then save to the database:
[[Nsnotificationcenter Defaultcenter] postnotificationname:kchatmessagenotification object:nil userinfo:@{@ "Chatinfo": Chatinfo}]; [Self.chatstore Insertchat:chatinfo];
The last time there is a bug, the chat message to go to the heavy after the voice message is not received, is because I first put the new chat message first inserted data, and then to send a notification, resulting in the interface to display the chat message is always displayed does not go up.
If it is a text message, set the state to Messageread read, then send notifaction to notify the chat page, and then save to the database, if it is another message type, change the content to "unsupported message type | Your current version is too low, click Upgrade Client", The notification is then stored in the database.
If it is a third account message, show the Little Red dot, and then send Notifaction to notify the Viewcontroller message center that there is a new message.Then get to Dtype
First determine if it is a push message and determine if the push ID of the click is entered (isremote && [pushid isequaltostring:result[@ "id"]) { if (!orderid && ([Type isequaltostring:@ "New_chat"] | | [Type isequaltostring:@ "Driver_arrive"] | | [Type isequaltostring:@ "Reception_driver"]| | [Type isequaltostring:@ "Service_done"])) {if ([Type isequaltostring:@ "New_chat"]) {OrderID = result[@ "Content"] [@ "topic"]; } else if (![ TOPVC Iskindofclass:[ycselectdriverviewcontroller class] && ([Type Isequaltostr ing:@ "Driver_arrive" | | [Type isequaltostring:@ "Reception_driver"] | | [Type isequaltostring:@ "Service_done"])) {OrderID = result[@ "Content"][@ "order_id"]; } } } }]; if (OrderID) {if ([Defaultvalueforkey (Kshowwelcome) Boolvalue]) {if (![ TOPVC ISKINDOFCLASS:[YCWELCOMEVC class]) {[[Nsnotificationcenter Defaultcenter] Postnotificationn AME:KREMOTEPUSHVC Object:nil userinfo:@{@ "OrderID": OrderID, @ "type":d type}]; } } }
First determine whether it is a push message and determine if the push ID of the click is entered, if the OrderID does not exist and if the type is a new chat message or if the driver has arrived or the driver orders or the service ends up in the if judgment, then in judging the type is not a chat, If the chat OrderID is the topic field in the content, If it is not a new chat and the current topmost viewcontroller is not the Ycselectdriverviewcontroller class and the type is the driver has arrived or the driver orders or the service ends in the If judgment, OrderID is the order_id field in the content.
If OrderID exists, first determine whether the Welcome page is displayed, and then judge that the current topmost viewcontroller is not a welcome page class, then issue the Notifaction, and then notify the view to jump to the specified Viewcontroller page.
When a chat message is received, after a series of data processing, a notification is sent, and then Ycchatviewcontroller is notified of the call-(void) ReceiveMessage: (Nsnotification *) Notification method:
-(void) ReceiveMessage: (nsnotification *) notification { ycchatinfo *chatinfo = notification.userinfo[@ " Chatinfo "]; If the message is not entered at this time, the current session page does not operate nsstring *string1 = [[NSString alloc] initwithformat:@ "%@", Chatinfo.orderid]; NSString *string2 = [[NSString alloc] initwithformat:@ "%@", self.orderInfo.serverOrderId]; if (![ String1 isequaltostring:string2]) { return; } Nsinteger MessageID = Chatinfo.messageid; Ycchatstore *chatstore = [[Ycchatstore alloc]init]; BOOL isnotrepeat = [Chatstore Selectdbwithmessageid:messageid]; if (!isnotrepeat) { return; } [Self Insertrowtotableviewbyindexpath:chatinfo issend:no];}
The first sentence is to get the content of the chat message in the notification, and then take the chatinfo in the OrderID and get order details through the interface obtained by the OrderID do compare, if OrderID inconsistent, return directly. If you have been to the Ycchatstore in the Selectdbwithmessageid method to query whether the database has the same messageid exist, if there is a direct return, if it does not exist, insert the interface
-(Nsindexpath *) Insertrowtotableviewbyindexpath: (Ycchatinfo *) chatinfo issend: (BOOL) issend {Nsindexpath *indexPath; [Self.chatarray Addobject:chatinfo]; Indexpath = [Nsindexpath indexpathforrow:[self.chatarray count]-1 insection:0]; void (^scrollblock) () = ^{DLog (@ "%d", indexpath.row); [Self.tableview Scrolltorowatindexpath:indexpath Atscrollposition:uitableviewscrollpositionbo Ttom Animated:yes]; }; [Self.tableview Insertrowsatindexpaths:@[indexpath] Withrowanimation:yctableviewrowanimationfromb Ottom completion:^{if (issend) { Scrollblock (); } }]; if (!issend) {scrollblock (); } return Indexpath;}
-(void) Scrolltorowatindexpath: (Nsindexpath *) Indexpath atscrollposition: (uitableviewscrollposition) Scrollposition animated: (BOOL) Animated This method is to slide the first few indexpath to the bottom of the TableView,-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths
Withrowanimation: (yctableviewrowanimation) animation
Completion: (void (^) (void)) animationcompletion
This is an animated effect at the time of insertion.
briefly introduce the names and functions of all the model, view, controller related classes in the chat system:
MODEL:
Ycchatinfo: The main definition of the chat interface all used in the field definition, chat-related enumeration information and chat messages of some bool judgment. Ycchatstore: Mainly to the chat data database to build table, insert, delete, query, deduplication, update and other operations. ycchatrequest: Encapsulates a chat-related network request. Ycchatrecord: Mainly recording related package, including start recording, end recording, get recording length, audio format conversion, and get audio path through SessionID. Ycreceivemessagecenter: Equivalent to Notifactioncenter, is the chat message processing of a message center, all accepted chat messages and sent chat messages will be messagecenter processed.
Views :
Ycchattableview: Inherits the TableView class, which overrides the TableView insert animation, which is the insertion animation every time a new message comes in. Ycchatbasecell: All cells in the chat are inherited from this cell (text, voice, future may also include pictures, geographical location, etc.) inside the main definition of the chat time of the label, background picture, Avatar, driver name, send time loading
Ycchataudiocell: Voice chat cell, including play button, Little red dot hint, recording time label
Ycchatimagecell: Temporarily not used in the system, may be for later chat can send pictures to prepareYcchattextcell: Send text chat cell
Ycheadview: Avatar cell
Ycrecordview: Hold down the talking view in the chat screen
Ycorderrecordview: It's okay to hold the talking view and chat system in the next order.
Ycplaybutton: Play audio button
ycchatdriveracceptcardcell:5.2.3 version new booking success Card
ycchatjourneystartcell:5.2.3 version New driver has started, driver is in place card
Ycchatupdathintcell: Type not supported, upgrade prompt
Controllers:
Ycchatviewcontroller: main interface of Chat
Ycchatmenuviewcontroller: The button to the right of chatYCSHAREJOURNEYCARDVC: Details page of the booking success cardYCCHATMAPVC: A map in a chat card