gpt4 book ai didi

ios - 如何使用XMPP框架获取消息传递状态

转载 作者:可可西里 更新时间:2023-11-01 03:06:12 26 4
gpt4 key购买 nike

我正在使用 XMPP 框架在用户之间聊天,聊天进行得很顺利。现在我需要显示消息已完美传送到两个设备,例如(What's app messenger)。我没有得到任何教程。下面的代码代表我们向其他用户发送消息。

//Sending the message to the other user
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:txtChat.text];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:self.userjid];
[message addChild:body];
[[APPDELEGATE xmppStream] sendElement:message];

在使用 XMPP 框架检索消息时,我使用以下代码获取数据

- (void)loadarchivemsg
{
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(bareJidStr like %@) && (streamBareJidStr like %@)", self.userjid,[[NSUserDefaults standardUserDefaults] stringForKey:@"kXMPPmyJID"]];
request.predicate = predicate;
NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"kXMPPmyJID"]);
[request setEntity:entityDescription];
NSError *error;
NSArray *messages_arc = [moc executeFetchRequest:request error:&error];

[self print:[[NSMutableArray alloc]initWithArray:messages_arc]];
}

- (void)print:(NSMutableArray*)messages_arc{
@autoreleasepool {
for (XMPPMessageArchiving_Message_CoreDataObject *message in messages_arc) {

NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
XMPPMessage *message12=[[XMPPMessage alloc]init];
message12 = [message message];
}
}

这就是我发送消息和检索消息的原因,你能帮我如何检索消息的传递状态吗?等待您宝贵的答案。

最佳答案

@Romance 你需要在连接方法中添加以下代码

  XMPPMessageDeliveryReceipts* xmppMessageDeliveryRecipts = [[XMPPMessageDeliveryReceipts alloc] initWithDispatchQueue:dispatch_get_main_queue()];
xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
xmppMessageDeliveryRecipts.autoSendMessageDeliveryRequests = YES;
[xmppMessageDeliveryRecipts activate:self.xmppStream];

记得在发送消息时添加这些行

NSString *messageID=[self.xmppStream generateUUID];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"id" stringValue:messageID];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"senderJabberID_Here"]];

[message addChild:body];

希望这对你有帮助:)

关于ios - 如何使用XMPP框架获取消息传递状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20852336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com