Data subscription is a very common function in sports and health applications. Users subscribe to data such as exercise records and health records. When these data change, users can instantly receive push notifications on the app.

For example, a user is exercising recently, and he has set himself a goal of walking 10,000 steps a day and reaching 3 times a week; a regular data subscription can push the user the number of steps of the day every day, but the user needs to do it twice by himself. Calculate to know if you have completed this week's goals.

The scene event subscription can make up for this shortcoming. Scenario event subscription is not only for a certain data type, but focuses on a combination of one or more data types. Taking the above scenario as an example, walking 10,000 steps on any 3 days in a week is a scenario. If the user achieves the scenario goal, they will receive a push reminder from the app.

Huawei Health Kit not only supports data event subscriptions, but the new version also adds scene event subscription capabilities, making sports health data subscriptions more accessible to users. So how can developers implement this capability? Below are the detailed integration steps.

Integrated approach

1. Register as a Subscriber

When applying for the Health Kit service with an alliance card, select the product that has been created, and check the registration subscription notification capability. You can choose the HTTP subscription method, fill in the callback notification address, and check whether the callback address is connected by testing the connectivity. Currently, only enterprise developers are supported to enable the subscription function in the Healthkit card.

If your usage scenario meets the following conditions, you can also choose the terminal-side subscription method, and fill in the application package name and Action:

  1. Use the device-side subscription method
  2. The subscription event type is the scene target event
  3. Use apk and apk information notification

For detailed preparation steps, refer to the Extended Capability Service Development Guide .

2. Registration and subscription records

HTTP request, see Add/Update Subscription Record API

 POST
https://health-api.cloud.huawei.com/healthkit/v1/subscriptions

Request example :

 POST
https://health-api.cloud.huawei.com/healthkit/v1/subscriptions

Request body :

 POST
https://health-api.cloud.huawei.com/healthkit/v1/subscriptions
Content-Type: application/json
Authorization: Bearer ***
x-client-id: ***
x-version: ***
x-caller-trace-id: ***
{
  "subscriberId": "08666998-78f6-46b9-8620-faa06cdbac2b",
  "eventTypes": [
        {
            "type": "SCENARIO_GOAL_EVENT",
            "subType": "ACHIEVE",
            "eventType": "SCENARIO_GOAL_EVENT$ACHIEVE",
            "goalInfo": {
                "createTime": 1654660859105,
                "startDay": 20220608,  // 目标开始计算日期,注意需晚于目标创建日期
                "recurrence": {
                    "unit": 1,  // 周期单位:日
                    "count": 30, // 整个周期30天
                    "expectedAchievedCount": 28
                },
                "goals": [
                    {
                        "goalType": 1,
                        "metricGoal": {
                            "value": 10000, // 设置达标值10000步
                            "fieldName": "steps",
                            "dataType": "com.huawei.continuous.steps.total"
                        }
                    }
                ]
            }
        }
    ]
}

3. Receive notification of compliance events

HTTP request , see Subscription Event Notification API

 POST
https://www.example.com/healthkit/notifications

Request example

 POST
https://www.example.com/healthkit/notifications

request body

 POST
https://lfhealthdev.hwcloudtest.cn/test/healthkit/notifications
Content-Type: application/json
x-notification-signature: ***
[{
    "appId": "101524371",
    "subscriptionId": "3a82f885-97bf-47f8-84d1-21e558fe6e99",
    "periodIndex": 0,
    "periodStartDay": 20220608,
    "periodEndDay": 20220608,
    "goalAchieve": [{
        "goalType": 1,
        "metricGoal": {
            "value": 10000.0,
            "fieldName": "steps",
            "dataType": "com.huawei.continuous.steps.total"
        },
        "achievedFlag": true //已达标
    }
    ]
}

4. If you need to query the event subscription record of a certain scenario, the corresponding target achievement result value in a single cycle can be queried using the following interface. (optional step)

HTTP request , see Querying Scene Event Results .

 GET
https://health-api.cloud.huawei.com/healthkit/v1/subscriptions/3a82f885-97bf-47f8-84d1-21e558fe6e99/achievedRecord

Request example

 GET
https://health-api.cloud.huawei.com/healthkit/v1/subscriptions/3a82f885-97bf-47f8-84d1-21e558fe6e99/achievedRecord

response body

 HTTP/1.1 200 OK
Content-type: application/json;charset=utf-8
[    
    {
    "openId": "MDFAMTAxNTI0MzcxQGQ0Y2M3N2UxZTVmNjcxNWFkMWQ5Y2JjYjlmZDZiaNTY3QDVhNmNkY2FiaMTFhYzc4NDk4NDI0MzJiaNjg0MzViaYmUyMGEzZjZkNzUzYWVjM2Q5ZTgwYWM5NTgzNmY",
    "appId": "101524371",
    "subscriptionId": "3a82f885-97bf-47f8-84d1-21e558fe6e99",
    "periodIndex": 0,
    "periodStartDay": 20220608,
    "periodEndDay": 20220608,
    "goalAchieve": [{
        "goalType": 1,
        "metricGoal": {
            "value": 10000.0,  // 设定的目标值
            "fieldName": "steps",
            "dataType": "com.huawei.continuous.steps.total"
        },
        "achievedResult": "20023",  // 实际值
        "achievedFlag": true // 已达标标记
    }]
    },
    {
    "openId": "MDFAMTAxNTI0MzcxQGQ0Y2M3N2UxZTVmNjcxNWFkMWQ5Y2JjYjlmZDZiaNTY3QDVhNmNkY2FiaMTFhYzc4NDk4NDI0MzJiaNjg0MzViaYmUyMGEzZjZkNzUzYWVjM2Q5ZTgwYWM5NTgzNmY",
    "appId": "101524371",
    "subscriptionId": "3a82f885-97bf-47f8-84d1-21e558fe6e99",
    "periodIndex": 1,
    "periodStartDay": 20220609,
    "periodEndDay": 20220609,
    "goalAchieve": [{
        "goalType": 1,
        "metricGoal": {
            "value": 10000.0,  // 设定的目标值
            "fieldName": "steps",
            "dataType": "com.huawei.continuous.steps.total"
        },
        "achievedResult": "9800",  // 实际值
        "achievedFlag": false // 未达标标记
    }]
    }
]

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙应用,和开发者共同成长。