Change XMPPPresence to Away/Busy/Invisible

后端 未结 2 1016
甜味超标
甜味超标 2021-02-04 22:02

How do you change your presence to show dnd/away and etc.?

XMPPPresence *presence = [XMPPPresence presenceWithType:status];
[[[self appDelegate] xmppStream] send         


        
2条回答
  •  甜味超标
    2021-02-04 22:43

    On top of the answer above, there is also a element that should be used in conjunction with the element. By using both elements, you can customize user's presence for each availability states.

    Default: Available / Offline

    By using : Available / Busy / Away / Extended Away / Offline

    By using with : "Free to chat" / "Hard at work" / "In a meeting" / "Out for lunch".


    If you use Openfire with this method: In User Sessions > Presence column, you will see:

    1. Different colored icons for each user (e.g. green for available, red for busy, etc.)

    2. A descriptive text beside the icons (e.g. "In a meeting")


    Presence child elements

    There are 3 elements that can change types of presence in XMPP.

    1. (we'll exclude this for discussion)

    Show

    specifies an availability status of a user.

    Values of the element must be specified according to the list below.

    "chat" -- user is actively interested in chatting.
    "dnd"  -- user is busy (dnd a.k.a 'Do Not Disturb').
    "away" -- user is temporarily away.
    "xa"   -- user is away for an extended period (xa a.k.a. 'eXtended Away').
    

    If this element is not provided, user is assumed to only be either online and available.

    Status

    describes the availability status of a user. It is usually used in conjunction with the element to provide a detailed description of an availability state.

    Values of the element can be of any descriptive text. For instance:

    "Available to chat" -- can be used for "chat"
    "Busy at work"      -- can be used for "dnd"
    "In a meeting"      -- can be used for "away"
    "On a vacation"     -- can be used for "xa"
    

    Usage in Objective-C

    Here's how you should apply the above concept in code.

    // Initialize variables
    XMPPPresence *presence = [XMPPPresence presence];
    NSXMLElement *show = [NSXMLElement elementWithName:@"show"];
    NSXMLElement *status = [NSXMLElement elementWithName:@"status"];
    
    // If user is available
    [show setStringValue:@"chat"];
    [status setStringValue:@"Available to chat"];
    
    // If user is busy
    [show setStringValue:@"dnd"];
    [status setStringValue:@"Busy at work"];
    
    // If user is away
    [show setStringValue:@"away"];
    [status setStringValue:@"In a meeting"];
    
    // If user is away for a long period of time
    [show setStringValue:@"xa"];
    [status setStringValue:@"On a vacation"];
    
    // Add the XML child elements to XMPPPresence
    [presence addChild:show];
    [presence addChild:status];
    
    // Update new presence to server
    [[[self appDelegate] xmppStream] sendElement:presence];
    

    There you go, your customized user's presence will now be accurately reflected in your server.

    See also: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence

提交回复
热议问题