> For the complete documentation index, see [llms.txt](https://documentation.pushly.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.pushly.com/pushly-ja/integration/implementation-steps/apple-ios/sdk-swift-obj-c/live-activities.md).

# Live Activities

[Live Activities](https://developer.apple.com/design/human-interface-guidelines/live-activities)2022年10月にAppleが導入したもので、アプリ開発者がデバイスのロック画面やDynamic Island（対応デバイス向け）上のウィジェットを通じて、動的にリアルタイム更新されるコンテンツを表示できるようにします。

PushSDKは、Live Activityに関連付けられた短時間のみ有効なプッシュトークンの登録と管理を開発者が行えるようにすることで、Live Activityの登録と更新を効率化します。これらはその後、当社のサーバーAPI経由で更新できます。

## 前提条件

SDKにLive Activitiesを統合する前に、以下の要件を満たしている必要があります:

* .p8キーで構成されたiOS送信統合。
* iOSアプリ（Live ActivitiesはiOSとiPadOSでのみ利用可能です）。
* iOS 16.1以降がインストールされたデバイスまたはエミュレーター
* Live ActivitiesをサポートするPushSDKリリース（下のバージョン対応表を参照）

## ステップ1: iOS SDKのセットアップ

アプリケーションが **まだ** PushSDKを使用していない場合は、ネイティブアプリのPushセットアップガイドを参照してください。 [Apple / iOS](/pushly-ja/integration/implementation-steps/apple-ios.md).

すでにPushSDKを使用しているアプリケーションでは、Live Activity対応を含む後のバージョンへのアップグレードが必要になる場合があります。以下の表を使って、インストールのアップグレードが必要かどうかを確認してください。

<table><thead><tr><th width="190">SDK</th><th>Live Activitiesを含む最初のバージョン</th></tr></thead><tbody><tr><td><a href="/pages/39b0cf329f28c0d2050f4731161d426692ed693d">Swift</a></td><td>1.2.0+</td></tr><tr><td><a href="/pages/7da103785af19cf2dddb0f0bdc52093ebca8d247">React Native SDK</a></td><td>1.1.0+</td></tr><tr><td><a href="/pages/342db951b1a038ed6a5bc35dc35fa015cae6d146">Flutter SDK</a></td><td>1.1.0+</td></tr></tbody></table>

<table><thead><tr><th width="190">SDK</th><th>Push-to-Start Live Activities対応を含む最初のバージョン</th></tr></thead><tbody><tr><td><a href="/pages/39b0cf329f28c0d2050f4731161d426692ed693d">Swift</a></td><td>1.3.9以上（iOS 17.2以上が必要）</td></tr></tbody></table>

{% tabs %}
{% tab title="Swift Package Manager" %}
Xcodeのプロジェクトナビゲーターパネル内で `Package Dependencies`を見つけて右クリックし、 `Pushly` を選択します。 `Update Package`.

<figure><img src="/files/1cbf6eef8069de48fcea6d6a0d285d494c812ba6" alt=""><figcaption></figcaption></figure>

更新後にパッケージが1.2.0以上でない場合は、プロジェクトナビゲーターでProjectを選択し、プロジェクトを選んだうえで、 `Package Dependencies`。次を選択します `Pushly` に移動し、 `Dependency Rule` が `Up to Next Major Version` に設定されており、ターゲットが1.0.0以上であることを確認してください。

<figure><img src="/files/7c5ee0fa90616cec3af31f9d56929d09159c30c7" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="CocoaPods" %}

* 次を対象にしていることを確認してください `'Pushly', '>= 1.0', '< 2.0'` をPodfileに記載してください。
* 実行 `pod update Pushly`
* Podfile.lockを確認して、最新バージョンがダウンロードされていることを確認してください。
  {% endtab %}
  {% endtabs %}

## ステップ2: Info.plistにLive Activityサポートを追加

Live Activitiesを実装するアプリケーションは、 `Supports Live Activities` キーを `YES` に、メインターゲットのinfo.plistで設定する必要があります。

<figure><img src="/files/f62b051d2fdbf60dd7c6de68637d53cb0d1323d2" alt=""><figcaption></figcaption></figure>

ユースケースの1つが頻繁な更新を利用する場合は、 `Supports Live Activities Frequent Updates` を `YES` にinfo.plistで設定することも検討してください。

## ステップ3: Live Activity Widget Extensionを追加

アプリのXcodeプロジェクト内で `File > New > Target`。次を選択します `Widget Extension` をiOSテンプレートタブ内で選択して、 `Next`.

<figure><img src="/files/bc2b16343a5a384d6809b7c15e5ef95683808cda" alt=""><figcaption></figcaption></figure>

に希望するLive Activityウィジェット名を入力し、 `Product Name` およびウィジェット拡張のその他の設定詳細を入力して、 `Include Live Activity` が選択されていることを確認したうえで、 `Finish`をクリックします。続くダイアログで `キャンセル` をクリックして、新しいウィジェット拡張を現在のターゲットとして有効化せずに開発を続行します。

<figure><img src="/files/b2a5d122995686ef3a2511cda178be53f907a6d8" alt=""><figcaption></figcaption></figure>

## ステップ4: Live Activityをセットアップする

### ユーザーインターフェース

新しいLive Activityを開始する前に、Appleの以下のガイドを読むことをおすすめします。 [Live Activitiesでライブデータを表示する](https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities).

### Live Activityの開始

Live Activityの設計とセットアップが完了したら、PushSDKを通じてそれを開始および登録する準備が整います。

単一のAPI呼び出しで数百万台のデバイスにまたがる複数のLive Activityインスタンスの追跡と更新を支援するために、activity IDを使用します。activity IDは、開発者であるあなたが提供する文字列値です。

スポーツイベントのような一部のシナリオでは、単一のイベントに登録されたすべてのデバイスに1回の更新を送信したい場合があります。このような場合は、個々のユーザーごとのIDではなく、イベントを識別する一意のIDを使用すべきです。

食事注文のような別のシナリオでは、Live Activitiesはユーザー固有です。このような場合は、個々のユーザーを識別する一意のIDを使用すべきです。

## ステップ5: Live Activityを開始する

### アプリ内から開始する

以下のコードスニペットは、 `MyLiveActivityAttributes` structを使い、 `eventId` 属性によってこの活動インスタンスを一意に識別し、その後PushSDKに活動を登録して、トークン更新を監視・収集するよう要求します。

{% tabs %}
{% tab title="Swift" %}
{% code fullWidth="false" %}

```swift
import UIKit
import ActivityKit
import Pushly

class ViewController: UIViewController {
    public func startLiveActivity() {
        if #available(iOS 16.1, *) {
            let attributes = MyLiveActivityAttributes(eventId: "my_activity_id")
            let contentState = MyLiveActivityAttributes.ContentState(
                home_team: "Chiefs",
                home_team_score: 0,
                away_team: "Chargers",
                away_team_score: 0)
            let activityContent = ActivityContent(
                state: contentState,
                staleDate: nil)
        
            do {
                let activity = try Activity<MyLiveActivityAttributes>.request(
                    attributes: attributes,
                    contentState: activityContent,
                    pushType: .token)
            
                // eventId属性を使用して活動をPushlyに登録
                PushSDK.LiveActivities.register(
                    activity,
                    withId: activity.attributes.eventId)
            } catch (let error) {
                print(error.localizedDescription)
            }
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### プッシュ経由で開始するための活動タイプの登録

以下のコードスニペットは、Activityタイプの監視を開始し、push-to-startトークンを登録します。また、すでに開始済みまたは実行中の活動もプッシュトークン収集用に登録されることを保証します。ここでも再び、 `MyLiveActivityAttributes` class `eventId` を使用します。このclassは、活動のこのインスタンスを一意に識別する属性を持ちます。

{% tabs %}
{% tab title="Swift" %}
{% code fullWidth="false" %}

```swift
import UIKit
import ActivityKit
import Pushly

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ...

        if #available(iOS 16.1, *) {
            // 活動タイプをPushlyに登録
            PushSDK.LiveActivities.register(Activity<MyLiveActivityAttributes>.self) { activity in
                // 特定の活動をPushlyに登録
                PushSDK.LiveActivities.register(activity, withId: activity.attributes.eventId)
            }
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### 開始イベントの送信

活動タイプをPushSDKに登録したら、当社の [Live Activity APIドキュメント](https://developers.pushly.com/reference/management-update-live-activity) を使って、 `activity.ios.data.attributes_type` プロパティで対応するattributes struct名を指定し、 `activity.ios.data.attributes` オブジェクトとリクエストパスの両方に関連付けられた一意のactivity識別子を渡すことで、開始イベントを送信できます。このシナリオでは、 `eventId` 属性を `my_activity_id` に設定して使用しました。これは後続の更新イベントで使用できます。

{% tabs %}
{% tab title="開始イベントの例" %}
{% code fullWidth="false" %}

```json
POSTリクエスト先:
https://api.pushly.com/domains/{domain_id}/live-activities/my_activity_id
{
    "name": "my-request-event-name",
    "event": "start",
    "activity": {
        "ios": {
            "data": {
                "relevance_score": 100,
                "priority": 10,
                "attributes_type": "MyLiveActivityAttributes",
                "attributes": {
                    "eventId": "my_activity_id"
                },
                "content_state": {
                    "home_team": "Chiefs",
                    "home_team_score": 0,
                    "away_team": "Chargers",
                    "away_team_score": 0
                },
                "notification": {
                    "title": "ライブイベントが開始されました",
                    "body": "今後の更新をお待ちください！"
                }
            }
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### PNActivityAttributesプロトコル

PushSDKは、シンプルなプロトコルである `PNActivityAttributes`も提供しており、これには `pnActivityId` という属性が必要で、これが活動を一意に識別するために使用されます。このプロトコルを使用すると、ローカルで開始された活動とプッシュ経由で開始された活動の両方でIDがトークン登録のために自動収集されるため、メソッドシグネチャが簡略化されます。

{% tabs %}
{% tab title="Swift" %}
{% code fullWidth="false" %}

```swift
import UIKit
import ActivityKit
import Pushly

struct MyLiveActivityAttributes: PNActivityAttributes {
    public struct ContentState: Codable, Hashable {
        var home_team: String
        var home_team_score: Int
        var away_team: String
        var away_team_score: Int
    }

    var pnActivityId: String
}

class ViewController: UIViewController {
    public func startLiveActivity() {
        if #available(iOS 16.1, *) {
            ...

            // 活動をPushlyに登録
            PushSDK.LiveActivities.register(activity)
        }
    }
}

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ...

        if #available(iOS 16.1, *) {
            // 活動タイプをPushlyに登録
            PushSDK.LiveActivities.register(Activity<MyLiveActivityAttributes>.self)
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## ステップ5: Live Activityに更新を送信する

短時間のみ有効なプッシュトークンがactivity IDに登録されたら、当社の [Live Activity APIドキュメント](https://documentation.pushly-dev.com/#live-activities) を使って、単一のリクエストで登録済みのすべてのLive Activityに更新を送信できます。

{% tabs %}
{% tab title="更新イベントの例" %}
{% code fullWidth="false" %}

```json
POSTリクエスト先:
https://api.pushly.com/domains/{domain_id}/live-activities/my_activity_id
{
    "name": "my-request-event-name",
    "event": "update",
    "activity": {
        "ios": {
            "data": {
                "relevance_score": 100,
                "priority": 10,
                "content_state": {
                    "home_team": "Chiefs",
                    "home_team_score": 7,
                    "away_team": "Chargers",
                    "away_team_score": 0
                }
            }
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

詳細については [ActivityKitプッシュ通知でLive Activitiesを開始および更新する](https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities) Appleの開発者ドキュメントを参照してください。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.pushly.com/pushly-ja/integration/implementation-steps/apple-ios/sdk-swift-obj-c/live-activities.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
