Skip to content

Presence Status

knamoach edited this page Oct 16, 2023 · 1 revision

Presence Status

Overview

Presence Status API provides real-time status updates about contacts which include predefined states, custom messages, and state-specific expiration and last active times. The usage of the API revolves around starting and stopping to watch the presence status of specific contacts. The related APIs can be accessed through PersonClient.swift. This functionality is available starting from SDK version 3.10.0.

Key API Components

  • PresenceHandle

    • handle: NSNumber: Get the unique handle identifier. PresenceHandle object (returned while startWatchingPresences) will have distinct handle identifier for each contactID.
    • contactId: String: Retrieve the contact ID.
    • isValid: Bool: Check if the handle is valid. For invalid contact Ids or bot contact Ids this will return false.
  • Presence

    • contactId: String: Obtain the contact's ID for this presence status.
    • status: PresenceStatus: Get the current presence status.
    • customStatus: String: Retrieve any custom status message set by user.
    • lastActiveTime: Long: Obtain the last active UTC time in milliseconds.
    • expiresTime: Date: Get the expiry UTC time in milliseconds for statuses like DND, OutOfOffice, etc.

Presence States

  1. Unknown
  2. Pending
  3. Active
  4. Inactive
  5. Dnd (Do Not Disturb)
  6. Quiet
  7. Busy
  8. OutOfOffice
  9. Call
  10. Meeting
  11. Presenting
  12. CalendarItem

API Usage with Examples

1. Start Watching Presences

To start watching presence status updates for a list of contact IDs, use the webex.people.startWatchingPresences() API.

let contactIds = ["ContactId1", "ContactId2"]
let presenceHandles = webex.people.startWatchingPresences(contactIds: contactIds, completionHandler: { presence in
    // Handle presence updates
    print("Updated presence for \(presence.contactId): \(presence.status)")
}
2. Stop Watching Presences

When no longer needed, use webex.people.stopWatchingPresences() to stop receiving updates for a list of presence handles.

webex.people.stopWatchingPresences(presenceHandles: presenceHandles)
3. Handling Presence Updates

Upon receiving a presence update, utilize the Presence struct to manage and display the obtained information. Ensure to respect the expiresTime for statuses like DND, OutOfOffice, etc., and use lastActiveTime to display how long a user has been inactive.

Example:

let presenceHandles = webex.people.startWatchingPresences(contactIds: contactIds, completionHandler: { presence in
    // Handle presence updates
    print("Contact: \(presence.contactId)")
    print("Status: \(presence.status}")
    print("Custom Message: \(customStatus)")
    
    if presence.status == PresenceStatus.Inactive {
        print("Last active: (\presence.lastActiveTime)ms ago")
    }

    if [PresenceStatus.OutOfOffice, PresenceStatus.Dnd, PresenceStatus.Quiet].contains(presence.status)
    {
        print("Status expires in: \(presence.expiresTime)ms")                     
    }
}