1# 发布公共事件(C/C++) 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: Notification--> 5<!--Owner: @peixu--> 6<!--Designer: @dongqingran; @wulong158--> 7<!--Tester: @wanghong1997--> 8<!--Adviser: @huipeizi--> 9 10## 场景介绍 11 12当需要发布某个公共事件时,可以通过[OH_CommonEvent_Publish](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publish)和[OH_CommonEvent_PublishWithInfo](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publishwithinfo)方法发布事件。发布的公共事件可以携带数据,供订阅者解析并进行下一步处理。 13 14## 接口说明 15 16详细的API说明请参考[CommonEvent API参考](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md)。 17 18| 接口名 | 描述 | 19| ------------------------------------ | ---------------------------------------------------------------- | 20|[struct CommonEvent_PublishInfo](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#结构体)|发布公共事件时使用的公共事件属性对象。| 21|[CommonEvent_ErrCode OH_CommonEvent_Publish(const char* event)](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publish)|发布公共事件。| 22|[CommonEvent_ErrCode OH_CommonEvent_PublishWithInfo(const char* event, const CommonEvent_PublishInfo* info)](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publishwithinfo)| 发布带有指定属性的公共事件。 | 23|[CommonEvent_PublishInfo* OH_CommonEvent_CreatePublishInfo(bool ordered)](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_createpublishinfo)|创建公共事件属性对象。| 24|[void OH_CommonEvent_DestroyPublishInfo(CommonEvent_PublishInfo* info)](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_destroypublishinfo)|销毁公共事件属性对象。| 25|[CommonEvent_Parameters* OH_CommonEvent_CreateParameters()](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_createparameters)|创建公共事件附加信息对象。| 26|[void OH_CommonEvent_DestroyParameters(CommonEvent_Parameters* param)](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_destroyparameters)|销毁公共事件附加信息对象。| 27 28## 开发步骤 29 301. 引用头文件。 31 32 ```c++ 33 #include <cstdint> 34 #include <cstdio> 35 #include <cwchar> 36 #include <string.h> 37 #include "hilog/log.h" 38 #include "BasicServicesKit/oh_commonevent.h" 39 ``` 40 412. 在CMake脚本中添加动态链接库。 42 43 ```txt 44 target_link_libraries(entry PUBLIC 45 libace_napi.z.so 46 libhilog_ndk.z.so 47 libohcommonevent.so 48 ) 49 ``` 50 513. (可选)创建公共事件属性对象。 52 53 发布携带数据的公共事件时,需要通过[OH_CommonEvent_CreatePublishInfo](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_createpublishinfo)创建公共事件属性对象,并通过以下接口设置公共事件属性。 54 55 ```c++ 56 // 创建并添加公共事件属性附加信息 57 CommonEvent_Parameters* CreateParameters() 58 { 59 int32_t ret = -1; 60 // 创建公共事件附加信息 61 CommonEvent_Parameters* param = OH_CommonEvent_CreateParameters(); 62 63 // 设置int类型附加信息和key 64 ret = OH_CommonEvent_SetIntToParameters(param, "intKey", 10); 65 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetIntToParameters ret <%{public}d>.", ret); 66 67 // 设置int数组类型附加信息和key 68 int intArray[] = {123, 234, 567}; 69 size_t arraySize = 3; 70 ret = OH_CommonEvent_SetIntArrayToParameters(param, "intArrayKey", intArray, arraySize); 71 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetIntArrayToParameters ret <%{public}d>.", ret); 72 73 // 设置long类型附加信息和key 74 ret = OH_CommonEvent_SetLongToParameters(param, "longKey", 2147483646); 75 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetLongToParameters ret <%{public}d>.", ret); 76 77 // 设置long数组类型附加信息和key 78 long longArray[] = {2147483646, 555, 2147483645}; 79 ret = OH_CommonEvent_SetLongArrayToParameters(param, "longArrayKey", longArray, arraySize); 80 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetLongArrayToParameters ret <%{public}d>.", ret); 81 82 // 设置double类型附加信息和key 83 ret = OH_CommonEvent_SetDoubleToParameters(param, "doubleKey", 11.22); 84 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetDoubleToParameters ret <%{public}d>.", ret); 85 86 // 设置double数组类型附加信息和key 87 double doubleArray[] = {11.22, 33.44, 55.66}; 88 ret = OH_CommonEvent_SetDoubleArrayToParameters(param, "doubleArrayKey", doubleArray, arraySize); 89 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetDoubleArrayToParameters ret <%{public}d>.", ret); 90 91 // 设置boolean类型附加信息和key 92 ret = OH_CommonEvent_SetBoolToParameters(param, "boolKey", true); 93 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetBoolToParameters ret <%{public}d>.", ret); 94 95 // 设置boolean数组类型附加信息和key 96 bool boolArray[] = {true, false, true}; 97 ret = OH_CommonEvent_SetBoolArrayToParameters(param, "boolArrayKey", boolArray, arraySize); 98 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetBoolArrayToParameters ret <%{public}d>.", ret); 99 100 // 设置char类型附加信息和key 101 ret = OH_CommonEvent_SetCharToParameters(param, "charKey", 'A'); 102 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetCharToParameters ret <%{public}d>.", ret); 103 104 // 设置char数组类型附加信息和key 105 const char* value= "Char Array"; 106 size_t valueLength = strlen(value); 107 ret = OH_CommonEvent_SetCharArrayToParameters(param, "charArrayKey", value, valueLength); 108 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetCharArrayToParameters ret <%{public}d>.", ret); 109 return param; 110 } 111 112 // 设置公共事件属性 113 void SetPublishInfo(const char* bundleName, const char* permissions[], int32_t num, const int32_t code, const char* data) 114 { 115 int32_t ret = -1; 116 // 创建publishInfo,设置是否为有序公共事件,取值为true,表示有序公共事件;取值为false,表示无序公共事件 117 CommonEvent_PublishInfo* info = OH_CommonEvent_CreatePublishInfo(true); 118 119 // 设置公共事件包名称 120 ret = OH_CommonEvent_SetPublishInfoBundleName(info, bundleName); 121 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoBundleName ret <%{public}d>.", ret); 122 123 // 设置公共事件权限,参数为权限数组和权限的数量 124 ret = OH_CommonEvent_SetPublishInfoPermissions(info, permissions, num); 125 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoPermissions ret <%{public}d>.", ret); 126 127 // 设置公共事件结果码 128 ret = OH_CommonEvent_SetPublishInfoCode(info, code); 129 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoCode ret <%{public}d>.", ret); 130 131 // 设置公共事件结果数据 132 size_t dataLength = strlen(data); 133 ret = OH_CommonEvent_SetPublishInfoData(info, data, dataLength); 134 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoData ret <%{public}d>.", ret); 135 136 // 设置公共事件附加信息 137 CommonEvent_Parameters* param = CreateParameters(); 138 ret = OH_CommonEvent_SetPublishInfoParameters(info, param); 139 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoParameters ret <%{public}d>.", ret); 140 } 141 ``` 142 1434. 发布公共事件。 144 145 - 通过[OH_CommonEvent_Publish](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publish)发布不携带信息的公共事件。 146 147 > **说明:** 148 > 149 > 不携带信息的公共事件,只能发布为无序公共事件。 150 151 ```c++ 152 void Publish(const char* event) 153 { 154 int32_t ret = OH_CommonEvent_Publish(event); 155 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_Publish ret <%{public}d>.", ret); 156 } 157 ``` 158 159 - 通过[OH_CommonEvent_PublishWithInfo](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_publishwithinfo)发布携带信息的公共事件。 160 161 ```c++ 162 void PublishWithInfo(const char* event, CommonEvent_PublishInfo* info) 163 { 164 // 创建时带入公共事件属性对象 165 int32_t ret = OH_CommonEvent_PublishWithInfo(event, info); 166 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_PublishWithInfo ret <%{public}d>.", ret); 167 } 168 ``` 169 1705. 销毁公共事件对象。 171 172 如果后续无需使用已创建的公共事件对象来发布公共事件,需要先通过[OH_CommonEvent_DestroyParameters](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_destroyparameters)销毁`CommonEvent_Parameters`对象,然后再通过[OH_CommonEvent_DestroyPublishInfo](../../reference/apis-basic-services-kit/capi-oh-commonevent-h.md#oh_commonevent_destroypublishinfo)销毁公共事件对象。 173 174 ```c++ 175 void DestroyPublishInfo(CommonEvent_Parameters* param, CommonEvent_PublishInfo* info) 176 { 177 // 先销毁Parameters 178 OH_CommonEvent_DestroyParameters(param); 179 param = nullptr; 180 // 销毁PublishInfo 181 OH_CommonEvent_DestroyPublishInfo(info); 182 info = nullptr; 183 } 184 ```