这是我的代码.
- (IBAction)sendMessageNow:(id)sender { Nsstring *messageStr =messageField.text; if([messageStr length] > 0) { NSLog(@"Message sending fron Gmail"); NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:messageStr]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; [message addAttributeWithName:@"to" stringValue:@"destination address"]; [message addChild:body]; NSLog(@"message1%@",message); [[self appDelegate].xmppStream sendElement:message]; } NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"hh:mm a"]; //[self sendMessage]; if ([self.messageField.text length]>0) { if (isfromMe) { Nsstring *rowNum=[Nsstring stringWithFormat:@"%d",(int)sphBubbledata.count]; [self adddMediaBubbledata:kTextByme mediaPath:self.messageField.text mtime:[formatter stringFromDate:date] thumb:@"" downloadstatus:@"" sendingStatus:kSending msg_ID:[self genRandStringLength:7]]; [self performSelector:@selector(messageSent:) withObject:rowNum afterDelay:1]; isfromMe=NO; } else { [self adddMediaBubbledata:kTextByOther mediaPath:self.messageField.text mtime:[formatter stringFromDate:date] thumb:@"" downloadstatus:@"" sendingStatus:kSent msg_ID:[self genRandStringLength:7]]; isfromMe=YES; } self.messageField.text=@""; [self.chattable reloadData]; [self scrollTableview]; } }
这是appdelegate.m中的senderdidreceivemessage方法
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { DDLogVerbose(@"%@: %@",THIS_FILE,THIS_METHOD); [[self xmppStream] sendElement:message]; UIlocalnotification *localnotification = [[UIlocalnotification alloc] init]; localnotification.alertAction = @"OK"; localnotification.fireDate = [NSDate date]; // localnotification.alertBody = xmppmessage; localnotification.soundName = UIlocalnotificationDefaultSoundName; [[UIApplication sharedApplication] presentlocalnotificationNow:localnotification]; // A simple example of inbound message handling. if ([message isChatMessageWithBody]) { XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from] xmppStream:xmppStream managedobjectContext:[self managedobjectContext_roster]]; Nsstring *body = [[message elementForName:@"body"] stringValue]; Nsstring *displayName = [user displayName]; if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName message:body delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; } else { // We are not active,so use a local notification instead UIlocalnotification *localnotification = [[UIlocalnotification alloc] init]; localnotification.alertAction = @"Ok"; localnotification.alertBody = [Nsstring stringWithFormat:@"From: %@\n\n%@",displayName,body]; [[UIApplication sharedApplication] presentlocalnotificationNow:localnotification]; } } }
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static Nsstring *L_CellIdentifier = @"SPHTextBubbleCell"; static Nsstring *R_CellIdentifier = @"SPHMediaBubbleCell"; SPH_ParaM_List *Feed_data=[[SPH_ParaM_List alloc]init]; Feed_data=[sphBubbledata objectAtIndex:indexPath.row]; if ([Feed_data.chat_media_type isEqualToString:kTextByme]||[Feed_data.chat_media_type isEqualToString:kTextByOther]) { SPHTextBubbleCell *cell = (SPHTextBubbleCell *) [tableView dequeueReusableCellWithIdentifier:L_CellIdentifier]; if (cell == nil) { cell = [[SPHTextBubbleCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:L_CellIdentifier]; } XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath]; //cell.textLabel.text = user.displayName; //[self configurePhotoForCell:cell user:user]; cell.bubbletype=([Feed_data.chat_media_type isEqualToString:kTextByme])?@"LEFT":@"RIGHT"; cell.textLabel.text = user.displayName; cell.textLabel.tag=indexPath.row; cell.timestampLabel.text = @"02:20 AM"; cell.CustomDelegate=self; cell.AvatarImageView.image=([Feed_data.chat_media_type isEqualToString:kTextByme])?[UIImage imageNamed:@"ProfilePic"]:[UIImage imageNamed:@"person"]; // cell.AvatarImageView.image=[ [self configurePhotoForCell:cell user:user] ; return cell; } SPHMediaBubbleCell *cell = (SPHMediaBubbleCell *) [tableView dequeueReusableCellWithIdentifier:R_CellIdentifier]; if (cell == nil) { cell = [[SPHMediaBubbleCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:R_CellIdentifier]; } cell.bubbletype=([Feed_data.chat_media_type isEqualToString:kImagebyme])?@"LEFT":@"RIGHT"; cell.textLabel.text = Feed_data.chat_message; cell.messageImageView.tag=indexPath.row; cell.CustomDelegate=self; cell.timestampLabel.text = @"02:20 AM"; cell.AvatarImageView.image=([Feed_data.chat_media_type isEqualToString:kImagebyme])?[UIImage imageNamed:@"ProfilePic"]:[UIImage imageNamed:@"person"]; return cell; }
并配置照片方法
- (void)configurePhotoForCell:(UITableViewCell *)cell user:(XMPPUserCoreDataStorageObject *)user { // Our xmppRosterStorage will cache photos as they arrive from the xmppvCardAvatarModule. // We only need to ask the avatar module for a photo,if the roster doesn't have it. if (user.photo != nil) { cell.imageView.image = user.photo; } else { NSData *photoData = [[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid]; if (photoData != nil) cell.imageView.image = [UIImage imageWithData:photoData]; else cell.imageView.image = [UIImage imageNamed:@"user2"]; } }
编辑
两者都是由我发送但它看起来在同一个窗口,当我点击一次发送按钮时,它在发送者消息中查找,第二次查看接收者消息.和接收者不会收到消息.
编辑
点击发送按钮后,这是响应.
function:-[ChathistryViewController sendMessageNow:] line:414 content:Message sending fron Gmail
function:-[ChathistryViewController sendMessageNow:] line:421 content:message1ghfhxfghh
2015-06-12 13:03:22:071 projectname[7556:2f03] SEND: ghfhxfghh
2015-06-12 13:03:22:389 projectname[7556:7347] RECV: ghfhxfghh
2015-06-12 13:03:22:390 CloseChat[7556:2f03] SEND: ghfhxfghh
2015-06-12 13:03:22.393 projectname[7556:49110] Attempting to schedule a local notification {fire date = (null),time zone = (null),repeat interval = 0,repeat count = UIlocalnotificationInfiniteRepeatCount,next fire date = Friday,June 12,2015 at 1:03:22 PM India Standard Time,user info = (null)} with a sound but haven’t received permission from the user to play sounds
2015-06-12 13:03:22:697 CloseChat[7556:7347] RECV:
2015-06-12 13:03:23:004 projectname[7556:7347] SEND:
2015-06-12 13:03:23:004 projectname[7556:7347] SEND: http://etherx.jabber.org/streams’ version=’1.0′ to=’jwchat.org’>
2015-06-12 13:03:23:619 projectname[7556:7347] RECV:
2015-06-12 13:03:23:619 projectname[7556:7347] RECV:
2015-06-12 13:03:23:619 CloseChat[7556:7347] SEND:
2015-06-12 13:03:23:925 projectname[7556:7347] RECV:
2015-06-12 13:03:23:926 projectname[7556:7347] SEND: http://etherx.jabber.org/streams’ version=’1.0′ to=’jwchat.org’>
2015-06-12 13:03:25:063 projectname[7556:7627] RECV:
2015-06-12 13:03:25:064 projectname[7556:7347] RECV: PLAINDIGEST-MD5SCRAM-SHA-1
2015-06-12 13:03:25:065 CloseChat[7556:607] SEND: biwsbj1jaGlyYWcscj1EMjUxMEQ2NC05MzZFLTQxMkUtQjY1Ri0zM0VFQjE1ODcxRjc=
2015-06-12 13:03:25:306 projectname[7556:7347] RECV: cj1EMjUxMEQ2NC05MzZFLTQxMkUtQjY1Ri0zM0VFQjE1ODcxRjdXVnB5eEp3b2lCRC9rUThvRkdhbGdRPT0scz1DZVZpSFFXNW9XRDVVNU90WUNMWExnPT0saT00MDk2
2015-06-12 13:03:25:321 projectname[7556:7347] SEND: Yz1iaXdzLHI9RDI1MTBENjQtOTM2RS00MTJFLUI2NUYtMzNFRUIxNTg3MUY3V1ZweXhKd29pQkQva1E4b0ZHYWxnuT09LHA9TWlsaW5TczI4b2VBd1dtQ3pWY21TWXZQWEFnPQ==
2015-06-12 13:03:25:562 projectname[7556:7627] RECV: dj1WeVdSaWRTTHVxYzhkV0E2aEg3OW9mU0FpYmc9
2015-06-12 13:03:25:562 projectname[7556:7627] SEND: http://etherx.jabber.org/streams’ version=’1.0′ to=’jwchat.org’>
2015-06-12 13:03:25:871 projectname[7556:7627] RECV:
2015-06-12 13:03:25:871 projectname[7556:7627] RECV:
2015-06-12 13:03:25:872 projectname[7556:7627] SEND:
2015-06-12 13:03:26:178 projectname[7556:7347] RECV: chirag@jwchat.org/341204969114349440629043
2015-06-12 13:03:26:179 projectname[7556:7347] SEND:
2015-06-12 13:03:26:485 projectname[7556:7347] RECV:
2015-06-12 13:03:26:486 projectname[7556:7627] SEND:
2015-06-12 13:03:26:487 projectname[7556:7347] SEND:
2015-06-12 13:03:26:488 projectname[7556:7627] SEND:
2015-06-12 13:03:26:714 projectname[7556:7347] RECV:
2015-06-12 13:03:26:946 projectname[7556:7347] RECV:
2015-06-12 13:03:26:947 projectname[7556:6e13] SEND:
2015-06-12 13:03:26:948 projectname[7556:6e13] RECV:
2015-06-12 13:03:27:180 projectname[7556:7627] RECV: