更新记录
1.0.0(2026-08-07) 下载此版本
·支持推送初始化 ·推送通知权限管理 ·推送消息点击处理 ·定期检查通知权限状态
平台兼容性
uni-app(5.15)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| - | √ | × | × | √ | - | × | × | API12及以上 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × | × |
ohos-umengpush
ohos-umengpush是鸿蒙端友盟推送、统计专属插件,支持推送初始化、权限管理、推送消息点击处理(后台唤醒/冷启动)和定期检查通知状态并提醒用户开启。
准备工作
-
配置 /harmony-configs/entry/src/main/module.json5
{ "module": { "name": "entry", "type": "entry", "description": "$string:module_desc", "mainElement": "EntryAbility", "srcEntry": "./ets/abilityStage/MyAbilityStage.ets" //新增srcEntry } }// module.json5 新增友盟所需权限 "requestPermissions": [ { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.GET_NETWORK_INFO" }, { "name": "ohos.permission.APP_TRACKING_CONSENT", "reason": "$string:oaid_permission_reason", "usedScene": { "abilities": [ "EntryAbility" ], "when": "inuse" } } ] -
配置 /harmony-configs/entry/src/main/resources/base/element/string.json
{ "string": [ { "name": "oaid_permission_reason", "value": "用于读取OAID信息,用来消息推送个性化分析。" } ] } -
配置 harmony-configs/AppScope/resources/rawfile/umconfig.json
{ "appKey": "你的appKey",//友盟后台获取 "channel": "official" } -
配置 harmony-configs/entry/src/main/ets/abilityStage/MyAbilityStage.ets
<script setup>
import AbilityStage from '@ohos.app.ability.AbilityStage';
import { preInit, InternalPlugin } from '@umeng/analytics';
import { PushPlugin } from "@umeng/push";
export default class MyAbilityStage extends AbilityStage {
onCreate() {
preInit({
context: this.context.getApplicationContext(),
enableLog: false, // 开发环境开启,正式环境改为true
plugins: [
new InternalPlugin(),
new PushPlugin({
appMessageSecret: "你的Umeng Message Secret" //友盟后台获取
})
]
});
// 注意:不在这里调用 init()
}
}
</script>
- 打开终端(macOS)进入harmony-configs/entry目录下执行ohpm命令安装友盟 SDK 依赖
<script setup>
ohpm install @umeng/common --registry=https://ohpm.openharmony.cn/ohpm
ohpm install @umeng/analytics --registry=https://ohpm.openharmony.cn/ohpm
ohpm install @umeng/utunnel --registry=https://ohpm.openharmony.cn/ohpm
ohpm install @umeng/push --registry=https://ohpm.openharmony.cn/ohpm
</script>
插件方法
| 方法名称 | 说明 |
|---|---|
| uni.initUmengPush | 初始化友盟推送 |
| uni.getDeviceToken | 获取device token |
| uni.isNotificationEnabled | 查询手机通知权限是否已授权 |
| uni.requestEnableNotification | 请求发送通知权限的许可(会弹窗,只弹一次) |
| uni.openNotificationSettings | 拉起系统弹窗设置通知权限管理 |
| uni.getPendingNotification | 获取待处理的推送数据 |
| uni.hasPendingNotification | 检查是否有待处理数据 |
前端引用
<script setup>
// 引入插件,确保方法被注册到 uni 对象上
import '@/uni_modules/ohos-umengpush';
// 导入通知管理工具
import notificationHelper from '@/uni_modules/ohos-umengpush/CheckNoticeTimer.js';
//导入推送点击处理工具
import NoticeClick from '@/uni_modules/ohos-umengpush/NoticeClick.js';
// 通知状态
const notificationStatus = ref('未知');
// 隐私弹窗回调
const popupState = (isAgree) => {
if (isAgree) {
console.log('用户已同意隐私协议,正常加载首页内容')
// 调用插件的初始化方法
uni.initUmengPush({
success: (res) => {
console.log('友盟推送初始化成功', res);
// 初始化成功后,可以获取 device token
uni.getDeviceToken({
success: (tokenRes) => {
console.log('获取 device token 成功', tokenRes.data;);
// 将 token 上报到你的服务器
// 请求发送通知的许可
handleRequestPermission();
},
fail: (err) => {
console.error('获取 device token 失败', err);
}
});
// 初始化完成检测推送缓存
setTimeout(() => {
NoticeClick.checkPendingNow();
}, 200);
},
fail: (err) => {
console.error('友盟推送初始化失败', err);
}
});
} else {
console.log('用户拒绝隐私协议,App已退出')
}
}
// 查询通知状态
const checkNotificationStatus = () => {
uni.isNotificationEnabled({
success: (res) => {
const isEnabled = res.data;
notificationStatus.value = isEnabled ? '已开启' : '未开启';
console.log('通知状态:', notificationStatus.value);
},
fail: (err) => {
console.error('查询通知状态失败:', err);
notificationStatus.value = '查询失败';
}
});
}
// 请求发送通知的许可(会弹窗 只弹一次)
const handleRequestPermission = () => {
uni.requestEnableNotification({
success: (res) => {
const isEnabled = res.data;
notificationStatus.value = isEnabled ? '已开启' : '未开启';
console.log('请求通知权限结果:', isEnabled ? '已允许' : '已拒绝');
uni.showToast({
title: isEnabled ? '通知权限已开启' : '通知权限被拒绝',
icon: isEnabled ? 'success' : 'none'
});
},
fail: (err) => {
console.error('请求通知权限失败:', err);
uni.showToast({
title: '请求权限失败',
icon: 'none'
});
}
});
}
// 再次拉起系统弹窗设置通知管理
const handleOpenSettings = () => {
uni.openNotificationSettings({
success: (res) => {
const isEnabled = res.data;
notificationStatus.value = isEnabled ? '已开启' : '未开启';
console.log('设置后通知状态:', notificationStatus.value);
uni.showToast({
title: isEnabled ? '通知已开启' : '通知未开启',
icon: isEnabled ? 'success' : 'none'
});
},
fail: (err) => {
console.error('打开通知设置失败:', err);
uni.showToast({
title: '打开设置失败',
icon: 'none'
});
}
});
}
// 定时器触发提醒,检测到通知未开启时,调用
const handleTimerReminder = () => {
uni.showModal({
title: '开启通知提醒',
content: '为了及时接收重要的消息和更新,建议您开启通知权限。',
confirmText: '去开启',
cancelText: '稍后',
success: (res) => {
if (res.confirm) {
console.log('[定时器] 用户点击"去开启"');
handleOpenSettings();
} else {
console.log('[定时器] 用户点击"稍后"');
}
}
});
}
// 启动定时器
const initNotificationTimer = () => {
notificationHelper.startNotificationCheck({
immediate: false, // 不立即检查,因为已经在 popupState 中处理了
onStatusChange: (isEnabled) => {
// 状态变化时更新 UI
notificationStatus.value = isEnabled ? '已开启' : '未开启';
console.log('[定时器] 状态变化:', notificationStatus.value);
},
onNeedRequest: () => {
// 当检测到通知未开启时,触发提醒
handleTimerReminder();
}
});
};
onMounted(() => {
// 启动定时器
initNotificationTimer();
});
// 页面卸载时清理定时器(选用)
onBeforeUnmount(() => {
notificationHelper.stopNotificationCheck();
});
</script >
后台唤醒/冷启动
App.vue全局onShow检测,点击推送通知每次从后台唤醒/冷启动完成跳转到自定义目标页面
<script setup>
import NoticeClick from '@/uni_modules/ohos-umengpush/NoticeClick.js';
onShow(() => {
console.log('App Show')
setTimeout(() => {
NoticeClick.checkPendingNow();
}, 300);
})
</script >
细节问题
-
NoticeClick.js工具里路由路径的是定制化的,需根据自己项目路径进行修改。
const targetUrl = newsType === '3' ? `/pages_detail/videodetail?id=${newsId}` : `/pages_detail/newsdetail?id=${newsId}`; -
推送通知时,post-body里需携带extra扩展参数 {newsType, newsId},否则会无法完成点击跳转详情页动作,具体查看友盟API集成文档。
{"extra":{"newsType":"3","newsId":"12345"}} -
定时检查通知工具CheckNoticeTimer.js,目前设置是每天检查,可以根据自己需求进行修改。
// 设置定时器,每天检查一次 const TEN_DAYS = 1 * 24 * 60 * 60 * 1000;
开发文档
友盟SDK 厂商通道集成文档 友盟API集成文档 调用鸿蒙原生API harmonyOS平台uts开发指南 华为查询通知权限

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 1
赞赏 0
下载 12490186
赞赏 1938
赞赏
京公网安备:11010802035340号