Webex Messaging
Monitor Presence
Presence Status provides real-time status updates about contacts which include predefined states, custom messages and state-specific expiration and last active times.
The presence related methods can be accessed through the PersonClient
object.
Available starting from SDK version 3.10.0.
anchorKey Components
anchorFollowing are the key methods, and statuses provided for presence monitoring:
PresenceHandle
getHandle(): Long
: Returns the unique handle identifier associated with eachcontactID
.getContactId(): String
: Returns the contact ID.isValid(): Boolean
: Indicates if the handle is valid, returningfalse
for invalid or bot contact IDs.
Presence
getContactId(): String
: Returns the contact ID associated with this presence status.getStatus(): PresenceStatus
: Returns the current presence status.getCustomStatus(): String
: Returns a custom status message if set by the user.getLastActiveTime(): Long
: Returns the last active UTC time in milliseconds.getExpiresTime(): Long
: Returns the status expiration UTC time in milliseconds for states like DND, OutOfOffice, etc.
Presence States
- Unknown
- Pending
- Active
- Inactive
- DND (Do Not Disturb)
- Quiet
- Busy
- OutOfOffice
- Call
- Meeting
- Presenting
- CalendarItem
anchorBegin Monitoring Presence
anchorTo monitor presence updates for contacts, use the webex.people.startWatchingPresences()
method:
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)")
}
anchorStop Watching Presence
anchorTo stop presence updates use the webex.people.stopWatchingPresences()
method, passing in presence handles:
webex.people.stopWatchingPresences(presenceHandles: presenceHandles)
anchorHandle Presence Updates
anchorUpon receiving a presence update, use the Presence
struct to manage and display the returned information.
Be sure to check expiresTime
for statuses like DND, OutOfOffice, etc., and use lastActiveTime()
to display how long a user has been inactive:
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")
}
}