Skip to content

Netmera/Netmera-LiveActivity-Sample

Repository files navigation

Netmera Live Activity Sample

This project demonstrates iOS Live Activities with Netmera integration. For more detailed information, please refer to the Netmera Developer Guide on Live Activities.

Examples include:

  • Match Score Tracking
  • Delivery Tracking
  • Public Transport Tracking
  • Flight Tracking
  • Financial Tracking

Requirements

  • Swift SDK version 4.2.0 or higher
  • p8 push certificate
  • iOS 17.2 and above (for remote Live Activity initiation)

1. Define the Live Activity Structure

1.1 Configure Info.plist

<key>NSSupportsLiveActivities</key>
<true/>
<key>NSSupportsLiveActivitiesFrequentUpdates</key>
<true/>

1.2 Implement ActivityAttributes

Each Live Activity must define its own custom ActivityAttributes struct that conforms to NetmeraLiveActivityAttributes.

Required fields:

  • netmeraGroupId: Unique identifier for grouping activities
  • ActivityAttributes: Static properties
  • ContentState: Dynamic updatable fields
import Foundation
import ActivityKit
import NetmeraLiveActivity

struct MatchScoreAttributes: ActivityAttributes, NetmeraLiveActivityAttributes {
    var netmeraGroupId: String?
    var homeTeamName: String
    var awayTeamName: String
    var homeTeamLogo: String
    var awayTeamLogo: String
    
    public static var activityIdentifier: String = "MatchScoreAttributes"
    
    public struct ContentState: Codable, Hashable {
        var homeTeamScore: Int
        var awayTeamScore: Int
        var matchStatus: String
    }
}

Target Membership Requirement

The ActivityAttributes file must be included in both:

  • Main app target
  • Widget extension target

In Xcode: Select the file → File Inspector → Check both targets in Target Membership.

2. Start a Live Activity

You can start an activity remotely or locally:

Remote: You must call the Netmera.register(forType:name:) method early in your app's lifecycle, before the push-to-start token is generated, then start an activity using the /rest/3.0/sendBulkNotification endpoint.

Local: Create an instance of your Live Activity, then use the Netmera.observeActivity method to let Netmera manage token and state updates of your activity.

Remote (iOS 17.2+)

2.1. Register Activity Type

To enable Live Activity tracking in iOS 17.2 and later, you must register the Live Activity type with Netmera. This allows Netmera to track and associate push tokens for the specified activity type.

Registration Method:

Netmera.register(forType:name:)

You can call this method:

  • Inside application(_:didFinishLaunchingWithOptions:) in your AppDelegate, or
  • At an appropriate point in your app's lifecycle before showing the Live Activity.

Example use case: When a user adds a team to favorites, you can register the related activity type in advance to prepare for future updates.

Example:

if #available(iOS 17.2, *) {
    Netmera.register(forType: Activity<MatchScoreAttributes>.self, name: "MatchScoreAttributes")
}

2.2. Start via REST API

curl --location 'https://restapi.netmera.com/rest/3.0/sendBulkNotification' \
--header 'X-netmera-api-key: your_rest_api_key' \
--header 'Content-Type: application/json' \
--data '{
    "message": {
        "title": "Live Activity Start",
        "text": "Here your live activity",
        "platforms": ["IOS"],
        "contentState": {
            "homeTeamScore": 0,
            "awayTeamScore": 0,
            "matchStatus": "1st Half"
        },
        "liveActAttr": {
            "netmeraGroupId": "ars-liv-2025",
            "homeTeamName": "Arsenal",
            "awayTeamName": "Liverpool",
            "homeTeamLogo": "barcelona_logo",
            "awayTeamLogo": "madrid_logo"
        },
        "liveActAttrType": "MatchScoreAttributes"
    },
    "type": "LIVE_ACTIVITY",
    "target": {
        "sendToAll": true
    }
}'
Local

You can use Apple's ActivityKit framework to create a Live Activity instance locally on the device. The Netmera SDK manages the push token generated by ActivityKit, enabling you to update Live Activities via the Netmera API. Netmera sends this push token to the Apple Push Notification service (APNs) on the backend.

Steps to Start a Live Activity Locally

  1. Create an instance of your Live Activity using Apple's ActivityKit APIs.
  2. Set the pushType parameter to .token to generate a push token.
  3. Pass the previously defined ActivityAttributes and ContentState when creating the activity.
  4. Register the Live Activity with Netmera by calling: (This registration step is required only for Live Activities started locally)
Netmera.observeActivity(matchActivity)

Example: LiveActivityManager Class Below is an example implementation of a manager class that starts a Live Activity for a match score.

import NetmeraCore
import NetmeraLiveActivity
import ActivityKit

class LiveActivityManager {
    func startMatchActivity() {
        let attributes = MatchScoreAttributes(
            netmeraGroupId: "ars-liv-2025",
            homeTeamName: "Arsenal",
            awayTeamName: "Liverpool",
            homeTeamLogo: "arsenal_logo",
            awayTeamLogo: "liverpool_logo"
        )
        
        let contentState = MatchScoreAttributes.ContentState(
            homeTeamScore: 0,
            awayTeamScore: 0,
            matchStatus: "1st half"
        )
        
        matchActivity = try Activity.request(
            attributes: attributes,
            contentState: contentState,
            pushType: .token // Important: Use `.token` to enable Netmera to manage push-based updates
        )
        
        if let matchActivity {
            Netmera.observeActivity(matchActivity)
        }
    }
}

3. Resume Activity Tracking

When the app is terminated, the Live Activity connection is lost. To maintain tracking: To resume tracking token updates or activity state changes, the existing Live Activity must be observed again when the app is relaunched. This should be handled inside the application(_:didFinishLaunchingWithOptions:) method of your AppDelegate Doing so ensures that both locally and remotely started activities are properly re-observed on app launch.

Use the following method:

Netmera.resumeObservingActivities(ofType: Activity<MatchScoreAttributes>.self)
import UIKit
import ActivityKit
import NetmeraLiveActivity

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        Netmera.initialize()
        ...
        
        if #available(iOS 16.1, *) {
            Netmera.resumeObservingActivities(ofType: Activity<MatchScoreAttributes>.self)
        }
        return true
    }
}

4. Update Live Activity

curl --location 'https://restapi.netmera.com/rest/3.0/update-live-activity' \
--header 'X-netmera-api-key: your_rest_api_key' \
--header 'Content-Type: application/json' \
--data '{
    "groupId": "ars-liv-2025",
    "action": "UPDATE",
    "contentState": {
        "homeTeamScore": 1,
        "awayTeamScore": 0,
        "matchStatus": "2nd Half"
    },
    "priority": 10
}'

5. End Live Activity

curl --location 'https://restapi.netmera.com/rest/3.0/update-live-activity' \
--header 'X-netmera-api-key: your_rest_api_key' \
--header 'Content-Type: application/json' \
--data '{
    "groupId": "ars-liv-2025",
    "action": "END",
    "priority": 10
}'

Unregistering Activity

You can stop tracking a specific Live Activity in your app by calling:

Netmera.unregister(name: activityName)

Important Notes

  • The name parameter must exactly match the identifier used during registration with Netmera.register(...).
  • If the names do not match exactly, the unregistration will not take effect.
  • Example use case: When a user removes a football match from their favorites and no longer wants updates on the lock screen or widget, call the unregister method.

Debugging

If you encounter the error "Cannot observe activity, missing required attribute: netmeraGroupId", ensure that netmeraGroupId is properly defined in your ActivityAttributes structure.

About

This project demonstrates iOS Live Activities with Netmera integration.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages