iOS XMPP无法发送消息

时间:2015-06-09 10:21:23

标签: ios xmppframework

我正在开发基于XMPP的ios项目。我想发送消息,但我无法发送消息。表示接收方不会收到该消息。 这是我的代码。

- (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"];
  }
 }

修改

两者都是由我发送但它看起来在同一个窗口,当我点击一次发送按钮时,它在发送者消息中查找,第二次查看接收者消息。和接收者不会收到消息。

Error

修改

点击发送按钮后,这是响应。

  

功能: - [ChathistryViewController sendMessageNow:]行:414内容:Gmail发送消息   功能: - [ChathistryViewController sendMessageNow:]行:421内容:message1ghfhxfghh   2015-06-12 13:03:22:071 projectname [7556:2f03]发送:ghfhxfghh   2015-06-12 13:03:22:389 projectname [7556:7347] RECV:ghfhxfghh   2015-06-12 13:03:22:390 CloseChat [7556:2f03]发送:ghfhxfghh   2015-06-12 13:03:22.393 projectname [7556:49110]尝试安排本地通知{fire date =(null),time zone =(null),repeat interval = 0,repeat count = UILocalNotificationInfiniteRepeatCount,next fire date = 2015年6月12日星期五下午1:03:22印度标准时间,用户信息=(null)}声音但未获得用户播放声音的许可   2015-06-12 13:03:22:697 CloseChat [7556:7347] RECV:   2015-06-12 13:03:23:004 projectname [7556:7347]发送:   2015-06-12 13:03:23:004 projectname [7556:7347]发送:http://etherx.jabber.org/streams'版本=' 1.0'到=' 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]发送:   2015-06-12 13:03:23:925 projectname [7556:7347] RECV:   2015-06-12 13:03:23:926 projectname [7556:7347]发送:http://etherx.jabber.org/streams'版本=' 1.0'到=' 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]发送:biwsbj1jaGlyYWcscj1EMjUxMEQ2NC05MzZFLTQxMkUtQjY1Ri0zM0VFQjE1ODcxRjc =   2015-06-12 13:03:25:306 projectname [7556:7347] RECV:cj1EMjUxMEQ2NC05MzZFLTQxMkUtQjY1Ri0zM0VFQjE1ODcxRjdXVnB5eEp3b2lCRC9rUThvRkdhbGdRPT0scz1DZVZpSFFXNW9XRDVVNU90WUNMWExnPT0saT00MDk2   2015-06-12 13:03:25:321 projectname [7556:7347]发送:Yz1iaXdzLHI9RDI1MTBENjQtOTM2RS00MTJFLUI2NUYtMzNFRUIxNTg3MUY3V1ZweXhKd29pQkQva1E4b0ZHYWxnUT09LHA9TWlsaW5TczI4b2VBd1dtQ3pWY21TWXZQWEFnPQ ==   2015-06-12 13:03:25:562 projectname [7556:7627] RECV:dj1WeVdSaWRTTHVxYzhkV0E2aEg3OW9mU0FpYmc9   2015-06-12 13:03:25:562 projectname [7556:7627]发送:http://etherx.jabber.org/streams'版本=' 1.0'到=' 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]发送:   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]发送:   2015-06-12 13:03:26:485 projectname [7556:7347] RECV:   2015-06-12 13:03:26:486 projectname [7556:7627]发送:   2015-06-12 13:03:26:487 projectname [7556:7347]发送:   2015-06-12 13:03:26:488 projectname [7556:7627]发送:   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]发送:   2015-06-12 13:03:26:948 projectname [7556:6e13] RECV:   2015-06-12 13:03:27:180 projectname [7556:7627] RECV:

3 个答案:

答案 0 :(得分:4)

您必须首先确保您的XMMP客户端已正确设置。然后确保您正在发送邮件,并确保您拥有正确的发件人。

对于那些现在说这是评论而不是答案的人,我没有足够的声誉来发表评论!

答案 1 :(得分:1)

Control仅用于UI目的,我从未编写任何代码来发送或接收来自服务器的消息,因为我不确定用户是否将使用任何第三方SDK或XMPP或普通REST API。

所以,你使服务器部分和UI部分分开。

你的问题:  两者都是由我发送的,但是当我点击发送者消息中的一次性发送按钮并且第二次查看接收者消息时,它看起来在同一个窗口中。和接收者不会收到消息。

Ans:是的,我已经为演示目的做了这个,根据您从服务器获得的响应,您必须在代码中进行更改。

使用方法: 1.声明一个NsmutableArray&在viewdidload上初始化它。 2.当您发送消息,图像时,创建一个Bubbledata&将它添加到数组&然后重新加载Tableview。

 [self adddMediaBubbledata:kTextByme mediaPath:@"Hi, check this new control!" 
mtime:@"8:30 AM" thumb:@"NA"  downloadstatus:@"" sendingStatus:kSent  msg_ID:@"AB4353GH"];

使用以下类型进行发送和接收:

 kTextByme: if you sending a text
 kImageByme : if you are sending an Image
 kTextByOther : if you receiving an Text
 kImageByOther : if you are receiving an Image

现在更新发送状态

    kSent      : you successfully sent an message
    kFailed    : message sendig failed
    kSending : your message is sending to server

因此,根据上述信息,进行更改并在需要任何其他帮助时告诉我

答案 2 :(得分:0)

请在XMPP客户端(XMMPFramework)中找到以下代表,它将处理收到的即时消息给您。

// XMPP委托方法:xmpp客户端收到消息

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{

  // some computation stuff

  return ;

}