【HarmonyOS】广告标识ID设置设备唯一标识
前言
1.广告标识ID-OAID是什么?:
开放匿名设备标识符(Open Anonymous Device Identifier, OAID,以下简称OAID):是一种非永久性设备标识符,基于开放匿名设备标识符,可在保护用户个人数据隐私安全的前提下,向用户提供个性化广告
2.OAID并非持久的唯一标识,不能当做DevicID使用:
虽然OAID是设备级标识符,同一台设备上不同的App获取到的OAID值一样。
但是当用户恢复手机出厂设置。用户操作重置OAID。都会导致OAID变化。并且最重要的是默认手机设备是没有开启【跨应用关联访问权限】,通过系统API获取到的是全0的值,没有用。只有当用户主动开启了,你去申请才能拿到值。
3.开启权限弹框的前置步骤
**【跨应用关联访问权限】**这个名字挺抽象的,你可以理解为安卓和苹果手机常说的跟踪访问或者广告ID权限。并且在鸿蒙中,同一台设备上当有应用去申请"跨应用关联访问权限"开关时,会首次生成OAID。
只有当用户开启时,调用API才会有主动提示用户授权的弹框,如下图:
代码实现:
代码运行日志:
调用函数:
dart
import { identifier } from '@kit.AdsKit';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
onClickGetOAID = ()=>{
this.requestOAIDTrackingConsentPermissions(getContext());
}
private requestOAIDTrackingConsentPermissions(context: common.Context): void {
// 进入页面时,向用户请求授权广告跨应用关联访问权限
const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
try {
atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => {
if (data.authResults[0] === 0) {
hilog.info(0x0000, 'testTag', '%{public}s', 'succeeded in requesting permission');
identifier.getOAID((err: BusinessError, data: string) => {
if (err.code) {
hilog.error(0x0000, 'testTag', '%{public}s', `get oaid failed, error: ${err.code} ${err.message}`);
} else {
const oaid: string = data;
hilog.info(0x0000, 'testTag', '%{public}s', `succeeded in getting oaid by callback , oaid: ${oaid}`);
}
});
} else {
hilog.error(0x0000, 'testTag', '%{public}s', 'user rejected');
}
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error: ${err.code} ${err.message}`);
})
} catch (err) {
hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${err.code}, ${err.message}`);
}
}
build() {
RelativeContainer() {
Text("点击请求用户OAID")
.id('IndexHelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(this.onClickGetOAID)
}
.height('100%')
.width('100%')
}
}
申请权限配置文件:
ohos.permission.APP_TRACKING_CONSENT
dart
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone",
"tablet",
"2in1"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
}
],
"extensionAbilities": [
{
"name": "EntryBackupAbility",
"srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
"type": "backup",
"exported": false,
"metadata": [
{
"name": "ohos.extension.backup",
"resource": "$profile:backup_config"
}
],
}
],
"requestPermissions": [
{
"name": "ohos.permission.APP_TRACKING_CONSENT",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "inuse"
}
}
]
}
}