文档管理中心
您当前浏览的HarmonyOS元服务5.0.0(API 12)文档归档不再维护,推荐您使用最新版本的文档。

推送基于账号的订阅消息

场景介绍

当用户授权同意订阅消息模板后,元服务的服务端主动调用服务通知REST API接口,向登录了账号的设备发送订阅消息(服务通知消息)。

频控规则

若订阅模板类型为“一次性订阅”,推送消息数量受控于元服务发起订阅请求并获得用户授权的次数。每次发送订阅消息都需要获取用户授权。

若订阅模板类型为“长期订阅”,推送消息数量将在服务通知的模板详情页说明,元服务可在模板限制的订阅周期及频次内多次下发通知,详情见选用订阅模板

说明

当前Push Kit仅支持订阅“一次性订阅”模板。

开发指导

推送订阅消息的消息示例如下,更多详情请见服务通知

收起
自动换行
深色代码主题
复制
  1. // Request URL
  2. POST https://push-api.cloud.huawei.com/v1/[projectId]/service_notification/send
  3. // Request Header
  4. Authorization: Bearer eyJr*****OiIx---****.eyJh*****iJodHR--***.QRod*****4Gp---****
  5. // Request Body
  6. {
  7. "msgId": "20240514ABCDE",
  8. "appId": "6**********972",
  9. "toOpenId": "A**********O",
  10. "templateId": "1**********2",
  11. "templateParams": {
  12. "thing_0": "待取号",
  13. "time_1": "2024年5月30日 13:00~2024年5月30日 14:00",
  14. "thing_2": "XX银行XX支行"
  15. }
  16. }
  • [projectId]:项目ID,登录AppGallery Connect网站,选择“我的项目”,在项目列表中选择对应的项目,左侧导航栏选择“项目设置”,在该页面获取。
  • Authorization:JWT格式字符串,可参见Authorization获取。
  • msgId:消息id,唯一确定一条消息,由元服务自行设置。
  • appId:元服务的APP ID,登录AppGallery Connect网站,选择“我的元服务”,在HarmonyOS页签下点击对应元服务,左侧导航栏选择“信息中心”,在该页面获取。
  • toOpenId:接收者(用户)账号登录的 openID。使用从端侧上报的openID,或请求华为账号服务器获取用户信息
  • templateId:订阅消息对应的模板ID,与发起订阅请求时用户授权订阅的entityId相同,详情见开发指导
  • templateParams:订阅消息模板中的占位符需要被替换的变量值。

    以“取号提醒”模板为例:

    templateParams中的参数项为元服务选用的模板中的配置参数。具体可配置项请见选用订阅模板

    注意

    填写templateParams时,请确保填入元服务选用模板的所有已勾选模板参数。

点击消息动作

点击消息进入应用首页并传递数据

消息示例如下,更多详情请见服务通知

  1. 发送消息时,在clickAction中携带data字段并设置actionType字段为0:

    收起
    自动换行
    深色代码主题
    复制
    1. // Request URL
    2. POST https://push-api.cloud.huawei.com/v1/[projectId]/service_notification/send
    3. // Request Header
    4. Authorization: Bearer eyJr*****OiIx---****.eyJh*****iJodHR--***.QRod*****4Gp---****
    5. // Request Body
    6. {
    7. "msgId": "2**********80",
    8. "appId": "6**********972",
    9. "toOpenId": "A**********O",
    10. "templateId": "1**********2",
    11. "templateParams": {
    12. "thing_0": "N0001",
    13. "time_1": "2024年4月27日 22:22",
    14. "thing_2": "软件大道101号"
    15. },
    16. "clickAction": {
    17. "actionType": 0,
    18. "data": { "testKey": "testValue" }
    19. }
    • actionType:点击消息的动作,0表示点击消息后进入首页。
    • data:点击消息时携带的JSON格式的数据,必须是key-value格式。

  2. 在应用首页中(通常为项目模块级别下的src/main/module.json5mainElement的值)的onCreate()方法中覆写如下代码:

    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility, Want } from '@kit.AbilityKit';
    2. import { hilog } from '@kit.PerformanceAnalysisKit';
    3. export default class MainAbility extends UIAbility {
    4. onCreate(want: Want): void {
    5. // 获取消息中传递的data数据
    6. const data = want.parameters;
    7. hilog.info(0x0000, 'testTag', 'Succeeded in getting message data');
    8. // 根据实际业务场景对data进行处理
    9. }
    10. }

    onNewWant()方法中覆写如下代码:

    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility, Want } from '@kit.AbilityKit';
    2. import { hilog } from '@kit.PerformanceAnalysisKit';
    3. export default class MainAbility extends UIAbility {
    4. onNewWant(want: Want): void {
    5. // 获取消息中传递的data数据
    6. const data = want.parameters;
    7. hilog.info(0x0000, 'testTag', 'Succeeded in getting message data');
    8. // 根据实际业务场景对data进行处理
    9. }
    10. }
    注意

    onNewWant()方法仅在单例(singleton)模式下可用。

  3. 当点击消息首次进入应用首页时,会在onCreate()方法中获取消息data数据,当前应用进程存在时,点击新消息进入首页会在onNewWant()方法中获取消息数据。

点击消息进入应用指定页并传递数据

消息示例如下,更多详情请见服务通知

  1. 在您的项目模块级别下的src/main/module.json5 中设置待跳转Ability的skills标签中的actions或uris值。

    设置actions参数完成点击消息进入应用指定页示例(若skills中添加了uris参数,则uris内容需为空):

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "name": "TestAbility",
    3. "srcEntry": "./ets/abilities/TestAbility.ets",
    4. "exported": false,
    5. "startWindowIcon": "$media:icon",
    6. "startWindowBackground": "$color:start_window_background",
    7. "skills": [
    8. {
    9. "actions": [
    10. "com.test.action"
    11. ]
    12. }
    13. ]
    14. }

    设置uris参数完成点击消息进入应用指定页示例(skills中必须同时设置actions参数,actions参数为空字符),uris对象内部结构说明请参见skills标签

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "name": "TestAbility",
    3. "srcEntry": "./ets/abilities/TestAbility.ets",
    4. "exported": false,
    5. "skills": [
    6. {
    7. "actions": [""],
    8. "uris": [
    9. {
    10. "scheme": "https",
    11. "host": "www.xxx.com",
    12. "port": "8080",
    13. "path": "push/test"
    14. }
    15. ]
    16. }
    17. ]
    18. }
    注意
    • 设置待跳转ability时,仅设置上述action方式或uris方式中的一种即可。
    • module.json5文件中的skills标签下可以同时存在多个skill对象,每个对象对应一种能力。若您需要同时设置推送消息跳转能力和其他跳转能力(如NFC跳转、浏览器跳转等),需要在skills数组中创建不同的skill对象,分别映射对应的能力。

  2. 发送消息时clickAction中携带data字段并设置actionType字段为1:

    收起
    自动换行
    深色代码主题
    复制
    1. // Request URL
    2. POST https://push-api.cloud.huawei.com/v1/[projectId]/service_notification/send
    3. // Request Header
    4. Authorization: Bearer eyJr*****OiIx---****.eyJh*****iJodHR--***.QRod*****4Gp---****
    5. // Request Body
    6. {
    7. "msgId": "2**********80",
    8. "appId": "6**********972",
    9. "toOpenId": "A**********O",
    10. "templateId": "1**********2",
    11. "templateParams": {
    12. "thing_0": "N0001",
    13. "time_1": "2024年4月27日 22:22",
    14. "thing_2": "软件大道101号"
    15. },
    16. "clickAction": {
    17. "actionType": 1,
    18. "action": "com.test.action",
    19. "uri": "https://www.xxx.com:8080/push/test",
    20. "data": { "testKey": "testValue" }
    21. }
    22. }
    • actionType:点击消息动作,1表示点击消息后进入元服务指定页。当本字段设置为1时,uri和action至少填写一个,若都填写优先寻找与action匹配的应用页面。
    • action:表示能够接收Want的action值的集合,取值可以自定义。与步骤1中设置的action保持一致。
    • uri:表示与Want中uris相匹配的集合,uris规则请参见skills标签。与步骤1中设置的uris保持一致。
    • data:点击消息时携带的JSON格式的数据,必须是key-value格式。

  3. 在待跳转页面(下以TestAbility为例)中的onCreate()方法中覆写如下代码:

    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility, Want } from '@kit.AbilityKit';
    2. import { hilog } from '@kit.PerformanceAnalysisKit';
    3. export default class TestAbility extends UIAbility {
    4. onCreate(want: Want): void {
    5. // 获取消息中传递的data数据
    6. const data = want.parameters;
    7. hilog.info(0x0000, 'testTag', 'Succeeded in getting message data');
    8. // 根据实际业务场景对data进行处理
    9. }
    10. }

    onNewWant()方法中覆写如下代码:

    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility, Want } from '@kit.AbilityKit';
    2. import { hilog } from '@kit.PerformanceAnalysisKit';
    3. export default class TestAbility extends UIAbility {
    4. onNewWant(want: Want): void {
    5. // 获取消息中传递的data数据
    6. const data = want.parameters;
    7. hilog.info(0x0000, 'testTag', 'Succeeded in getting message data');
    8. // 根据实际业务场景对data进行处理
    9. }
    10. }
    注意

    onNewWant()方法仅在单例(singleton)模式下可用。

  4. 当点击消息首次进入应用内页面时,会在onCreate()方法中获取消息data数据;当前应用进程存在时,点击消息会在onNewWant()方法中获取消息数据。
在 指南 中进行搜索
请输入您想要搜索的关键词