XMPPFramework “typing..” status for message

2019-04-02 01:49发布

问题:

I am using robbiehanson/XMPPFramework for my current project. How to get the message typing status using XMPPFramework? There XEP- 184 protocol but those are deprecated right now . Need assistance here for getting composing status in iOS . Regards, Bhat

回答1:

The most commonly used protocol for "contact is typing" notifications is XEP-0085: Chat State Notifications. As described in more detail there, the first message to a contact should contain an "active" state element (next to the <body/> element):

<active xmlns='http://jabber.org/protocol/chatstates'/>

If the contact responds with a chat state, the client can go ahead and use other states, such as "composing":

<composing xmlns='http://jabber.org/protocol/chatstates'/>

or "paused" (the user has entered text, but isn't actively typing):

<paused xmlns='http://jabber.org/protocol/chatstates'/>

or "inactive", and finally "gone" when the user ends the conversation by closing the chat window or similar.



回答2:

First you import:

#import "XMPPMessage+XEP_0085.h"

and then you add the following methods according to your purpose.

composing.....

- (void)sendComposingChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addComposingChatState];
    [xmppStream sendElement:message];
}

Active.....

- (void)sendActiveChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addActiveChatState];
    [xmppStream sendElement:message];
}

Inactive...

- (void)sendInactiveChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
     [xmppMessage addInactiveChatState];
    [xmppStream sendElement:message];
}

Gone...

- (void)sendExitChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addGoneChatState];
   [xmppStream sendElement:xmppMessage];
}

Paused...

- (void)sendPausedChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addPausedChatState];
    [xmppStream sendElement:message];
}

then You should write following code in appdelgate. (didReceiveMessage) method.

Ex:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
    if ([message isChatMessageWithBody]){
}
else{
if([message elementForName:@"composing"] != nil){



        } else if ([message elementForName:@"paused"] != nil) {



        } else if ([message elementForName:@"gone"] || [message elementForName:@"inactive"] ) {


        }
}