ClickCease
Presence state of app users

Quickly implement Presence State feature for Native Applications

Italo Orihuela
Italo Orihuela
Engineer
Android
iOS
Web
Apr 10, 2024

The ability to track and display a user’s online status, or ‘presence state’, is a crucial feature for many applications. Another important feature is the ‘heartbeat sync’, a mechanic that signals the system whether a user is online or offline. Delve into the intricacies of implementing the presence state and heartbeat sync features in your iOS and Android applications using the Amity Social Cloud SDK.

We will guide you through the process of enabling, disabling, querying the presence settings, and managing the heartbeat sync, providing you with the tools to enhance user engagement and interaction in your app. So, whether you’re a seasoned developer or a beginner, join us as we explore the power of presence state and heartbeat sync in app development.

Pre-requisites

Before we dive into the tutorial, ensure you have the following:

  1. Basic knowledge of iOS and Android development
  2. Familiarity with Swift for iOS and Kotlin for Android
  3. The Amity SDK installed in your project
  4. An Amity Social Cloud Portal account
  5. An Amity Social Cloud Console Account

Note: If you haven’t already registered for an Amity account, we recommend following our comprehensive step-by-step guide in the Amity Portal to create your new network

Step 1: Understanding Presence State and Heartbeat Sync

Presence state is a feature that shows the user’s current availability. It’s a driving factor for engagement products, allowing users to see if their friends or colleagues are online. The AmitySDK supports both observing and notifying the presence of users, similar to being online and observing users’ online statuses.

The heartbeat sync is a mechanic that signals the system whether a user is online or offline. Amity Social Cloud SDK offers two convenient methods that allow users to periodically sync or unsync their presence status with the server. When the server receives a heartbeat sync request, it records the timestamp at the time of the request, designating it as the lastHeartbeat timestamp for that user.

Step 2: Enabling Presence State

By default, the presence state feature is disabled at both the network and user levels. To enable it, you need to invoke the enable() method within client.presence. Here's how to do it:

For iOS:

For Android:

Step 4: Querying Presence Status

To check the presence status, you can invoke the isEnabled() method within client.presence. This method also determines if the presence state feature is available for the app. Here's how to do it

For iOS:

For Android:

Step 5: Managing Heartbeat Sync

To start the heartbeat synchronization process, invoke the startHeartbeat method in client.presence. This method automatically checks the user's presence settings within the network, and if enabled, begins to sync the heartbeat at specified intervals defined in the SDK.

For iOS:

For Android:

To stop the heartbeat synchronization process, utilize the stopHeartbeat() method within client.presence.

For iOS:

For Android:

Final Thoughts

Presence state and heartbeat sync features can significantly enhance the user experience in your app. They allow users to know when their friends or colleagues are online, fostering interaction and engagement. With the Amity Social Cloud SDK, implementing these features in your iOS and Android apps is a breeze. Remember, practice makes perfect. So, keep experimenting with these features and make your app more interactive and user-friendly.