我正在开发基于XMPP的项目。我可以发送消息并在会话屏幕中显示,但是当我收到消息时,它只显示在alertview中,无法在会话屏幕中看到。
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
if ([message isChatMessageWithBody])
{
XMPPMessageArchivingCoreDataStorage *xmppMessageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
XMPPMessageArchiving *xmppMessageArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageStorage];
[xmppMessageArchiving activate:xmppStream];
[xmppMessageArchiving addDelegate:self delegateQueue:dispatch_get_main_queue()];
XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
xmppStream:xmppStream
managedObjectContext:[self managedObjectContext_roster]];
[[NSNotificationCenter defaultCenter] postNotificationName:MessageRecivedNotif object:message];
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];
}
}
我的错误在哪里?
修改
displayHistory()
答案 0 :(得分:1)
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage*)message
{
//in this the (XMPPMessage*)message parameter is xml data
//parse you xml and create dictionary For eg : sampledict
}
通常,当您收到XMPP的回复时,它就像XML数据一样。您必须解析并用于您的操作。
在你的代码中
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];
}
您使用过alertview。因此,您必须隐藏您的提醒视图,以便您不会收到提醒。而是管理您的观察者方法以获取数据并使用它
-(void)messageReceived:(NSNotification*)notif
{
XMPPMessage *message=(XMPPMessage*)notif.object;
NSString *body = [[message elementForName:@"body"] stringValue];
NSMutableDictionary *dic_recive = [[NSMutableDictionary alloc]init];
[dic_recive setObject:body forKey:@"text"];
[data addObject:dic_recive];
chatLblRight = 1;
[tblobj reloadData];
}
我认为这会对你有帮助..
答案 1 :(得分:1)
你需要在uitableviewcellforrowatindexpath方法中设置你的逻辑。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
UILabel *lbl;
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"2"])
//if (chatLblRight == 1)
{
lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
[lbl setTextColor:[UIColor redColor]];
[cell.contentView addSubview:lbl];
}
}
if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"2"])
//if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"1"])
//if (chatLblRight == 1)
{
lbl.text = [[data objectAtIndex:indexPath.row]valueForKey:@"text"];
chatLblRight = 0;
lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
[lbl setTextColor:[UIColor redColor]];
[cell.contentView addSubview:lbl];
return cell;
}
else
{
cell.textLabel.text=[[data objectAtIndex:indexPath.row]valueForKey:@"text"];
return cell;
}
}
请检查此代码一次。
答案 2 :(得分:0)
请转到xmpp自定义类并发布此类通知
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
//parse you xml and create dictionary For eg : sampledict
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:@"updateValue" object:nil userInfo:sampledict];
}
并接收来自您班级的通知 使用像
这样的通知[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveUpdatedValues:)
name:@"updateValue"
object:nil];
-(void)receiveUpdatedValues : (NSNotification *)notification
{
NSDictionary *dictinfo=notification.userInfo;
// here use your info dict to get your message
}