• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 广告标识服务部件
2
3## 简介
4
5开放匿名设备标识符(Open Anonymous Device Identifier, OAID,以下简称OAID):是一种非永久性设备标识符,基于开放匿名设备标识符,可在保护用户个人数据隐私安全的前提下,向用户提供个性化广告,同时三方监测平台也可以向广告主提供转化归因分析。
6
7
8## 目录
9
10```
11/domains/advertising/oaid  # 广告标识服务部件业务代码
12├── interfaces                         # 接口代码
13├── profile                            # 服务配置文件
14├── services                           # 服务代码
15├── test                               # 测试用例
16├── LICENSE                            # 证书文件
17└── bundle.json                        # 编译文件
18```
19
20## 使用说明
21
22### 获取OAID
23
24可以使用此仓库内提供的接口获取OAID。以下步骤描述了如何使用接口获取OAID。
25
261. 申请广告跟踪权限
27
28   在模块的module.json5文件中,申请[ohos.permission.APP_TRACKING_CONSENT](https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/security/permission-list.md/)权限。
29
30   ```
31
32   {
33     "module": {
34       "requestPermissions": [
35         {
36           "name": "ohos.permission.APP_TRACKING_CONSENT" // 申请广告跟踪权限
37         }
38       ]
39     }
40   }
41   ```
42
432. 在应用启动时触发动态授权弹框,向用户请求授权。
44
45  ```
46  import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
47  import { BusinessError } from '@ohos.base';
48  import hilog from '@ohos.hilog';
49
50  private requestOAIDTrackingConsentPermissions(context: common.Context): void {
51    // 进入页面时触发动态授权弹框,向用户请求授权广告跟踪权限
52    const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
53     try {
54       atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => {
55         if (data.authResults[0] == 0) {
56           hilog.info(0x0000, 'testTag', '%{public}s', 'request permission success');
57         } else {
58           hilog.info(0x0000, 'testTag', '%{public}s', `user rejected`);
59         }
60       }).catch((err) => {
61         const e: BusinessError = err as BusinessError;
62         hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error message: ${e.message}`);
63       })
64     } catch(err) {
65       const e: BusinessError = err as BusinessError;
66       hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${JSON.stringify(e)}`);
67     }
68   }
69  ```
70
713. 获取OAID信息
72
73- 通过Callback回调函数获取OAID
74
75  ```
76
77  import identifier from '@ohos.identifier.oaid';
78  import hilog from '@ohos.hilog';
79  import { BusinessError } from '@ohos.base';
80
81  try {
82    identifier.getOAID((err, data) => {
83      if (err.code) {
84        hilog.info(0x0000, 'testTag', '%{public}s', `getOAID failed, message: ${err.message}`);
85    } else {
86      const oaid: string = data;
87      hilog.info(0x0000, 'testTag', '%{public}s', `getOAID by callback success`);
88    }
89    });
90  } catch (err) {
91    const e: BusinessError = err as BusinessError;
92    hilog.error(0x0000, 'testTag', 'get oaid catch error: %{public}d %{public}s', e.code, e.message);
93  }
94  ```
95
96- 通过Promise异步获取OAID
97
98  ```
99
100  import identifier from '@ohos.identifier.oaid';
101  import hilog from '@ohos.hilog';
102  import { BusinessError } from '@ohos.base';
103
104  try {
105    identifier.getOAID().then((data) => {
106      const oaid: string = data;
107      hilog.info(0x0000, 'testTag', '%{public}s', `get oaid by callback success`);
108    }).catch((err) => {
109      hilog.info(0x0000, 'testTag', '%{public}s', `get oaid failed, message: ${(err as BusinessError).message}`);
110    })
111  } catch (err) {
112    const e: BusinessError = err as BusinessError;
113    hilog.error(0x0000, 'testTag', 'get oaid catch error: %{public}d %{public}s', e.code, e.message);
114  }
115  ```
116
117### 重置OAID
118
119可以使用此仓库内提供的接口重置OAID。以下步骤描述了如何使用接口重置OAID。该接口为系统接口。
120
121```
122
123  import identifier from '@ohos.identifier.oaid';
124  import hilog from '@ohos.hilog';
125  import { BusinessError } from '@ohos.base';
126
127  try {
128    identifier.resetOAID();
129  } catch (err) {
130    const e: BusinessError = err as BusinessError;
131    hilog.error(0x0000, 'testTag', 'reset oaid catch error: %{public}d %{public}s', e.code, e.message);
132  }
133```
134
135## 相关仓