Live Activities
A quick start guide to adding Live Activity notification support to your Android application with the Pushly PushSDK
Android Live Activity notifications provides a similar feature-set and experience to iOS Live Activities on the Android platform allowing for real-time updated content via push notifications.
The PushSDK helps you streamline Live Activity registration and updates by allowing developers to register and manage the Live Activity handlers which can then be updated via our server API.
Prerequisites
Before you can integrate Live Activities with the SDK the following requirements must be met:
An Android Send Integration configured with a Firebase Sender ID and Server API Key or Service Account.
An Android app.
A device or emulator running API 19+ (Android 4.4) with Google Play Store Services installed.
PushSDK release supporting Live Activities (See version support chart below).
Step 1: Android SDK Setup
For applications not already using the PushSDK please see our Native App Push setup guide for Android.
For applications already using the PushSDK you might need to upgrade to a later version that includes Live Activity support. Use the following tables to determine if you need to upgrade your installation.
1.3.0+
Step 2: Creating a Handler for Live Activity Updates
To handle and display updates you will need to implement the PNLiveActivityHandler
class. The handler requires a name
that will uniquely identifier this type of activity and start via push. An optional notification channelId
can also be specified to group Live Activity notifications into their own channel. When no custom channel ID is specified the PushSDK will use a default channel.
An onUpdate
method must be implemented and is responsible for handling any Live Activity event as it is received (start, update, or end). Use this method to customize how updates are displayed to users.
Registering the Handler
Once you have implemented your handler you will need to register it with the PushSDK. The following code snippet shows how to register the handler:
Step 3: Starting a Live Activity
Starting from Within the App
The following code snippet requests to start a Live Activity using a unique activityId
, a handler
name that corresponds to a previously register handler class, and the initial contentState
of the activity. In this scenario we will use the PNLiveActivityHandler
class registered with the name: game_updates
.
Content State
ContentState
is used to pass data to Live Activities. You can create your state using convenience method contentStateOf()
:
ContentState
supports the following value types: String, Int, Long, Float, Double, Boolean, null.
Starting via Push
Once you have registered the handler with the PushSDK you can use our Live Activity API documentation to send a start event by specifying the corresponding handler name name in the activity.android.data.handler_name
property and passing the associated unique activity identifier in the request path - in this scenario we used my_activity_id
which we can then use in subsequent update events.
Step 4: Send Your Live Activity an Update
Once the Live Activity has been started, either locally or via push, you can use our Live Activity API documentation to send updates to all registered Live Activities via a single request.
Step 5: Removing Live Activities
In a similar fashion to how iOS Live Activities work Android Live Activity notifications will automatically end and self-cleanup after 12 hours from the time they are started. However, there might be situations where you want to cleanup the activity before that time limit has occurred. In those scenarios you can remove a single activity or remove all active activities with the following methods:
Remove a Specific Activity
Remove All Activities
Best Practices
Always register handlers in your application's
onCreate
method for proper initialization.Return a properly configured
Notification.Builder
in the handler'sonUpdate
method.Use consistent activity IDs to properly track and update specific activities.
Handle all possible states in your handler's
onUpdate
method.Implement proper error handling in your handler.
Use meaningful handler names that reflect their purpose.
Clean up activities when they're no longer needed using and
end
event or manually calling one of the remove methods.
Last updated