uniapp安卓通知栏
配置 manifest.json 文件
在项目根目录的 manifest.json 文件中启用 Android 通知权限。需添加以下配置:
"app-plus": {
"distribute": {
"android": {
"permissions": [
"<uses-permission android:name=\"android.permission.POST_NOTIFICATIONS\"/>"
]
}
}
}
使用 UniPush 服务
通过 UniPush 实现跨平台通知功能,需在 manifest.json 中配置推送模块:
"push": {
"unipush": {
"enable": true,
"vendor": "unipush"
}
}
创建本地通知
通过 uni.createPushMessage API 发送本地通知栏消息:

uni.createPushMessage({
title: "标题",
content: "通知内容",
payload: { customData: "example" }, // 可选自定义数据
success: () => console.log("通知发送成功"),
fail: (err) => console.error("发送失败:", err)
});
处理通知点击事件
监听通知点击事件需在 App.vue 的 onLaunch 方法中实现:
onLaunch: function(options) {
if (options.path === 'pages/notify') {
console.log("通知携带参数:", options.query);
}
}
动态图标与声音配置
通过 plus.android.import 调用原生 API 自定义通知图标和声音:

const Notification = plus.android.importClass("android.app.Notification");
const builder = new Notification.Builder(context);
builder.setSmallIcon(R.drawable.icon) // 替换为实际资源 ID
.setSound(Uri.parse("android.resource://" + packageName + "/raw/notify_sound"));
高优先级通知设置
针对重要通知,可设置优先级为 MAX 并启用 heads-up 样式:
builder.setPriority(Notification.PRIORITY_MAX)
.setFullScreenIntent(pendingIntent, true);
清除特定通知
通过通知 ID 清除已显示的通知:
const notificationManager = plus.android.importClass("android.app.NotificationManager");
notificationManager.cancel(notificationId);
兼容性处理
检测系统版本并处理 Android 13+ 的运行时权限:
if (plus.os.version >= 13) {
plus.android.requestPermissions(["android.permission.POST_NOTIFICATIONS"]);
}
注意事项
- 真机测试需打包自定义基座或正式包
- 图标资源需放在
nativeResources/android/drawable目录 - 华为/小米等厂商通道需单独配置






