更新记录

1.0.0(2026-10-12) 下载此版本

  • 首次发布。
  • Android:基于 NotificationCompat + NotificationChannel + AlarmManager 实现通知渠道、权限申请、发送/取消通知、常驻通知、进度条通知、定时通知及点击/清除回调。
  • iOS:基于 UserNotifications 实现权限申请、发送/取消通知、定时通知及点击/清除回调。
  • Web:基于浏览器 Notification API 实现发送/取消/更新/定时通知与点击回调。
  • 微信小程序:降级为应用内 toast。
  • 统一错误码:9020001 ~ 9020007。

平台兼容性

uni-app(5.0)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
√ √ √ √ √ √ √ √ ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
√ × - - - - - - - - - -

uni-app x(5.0)

Chrome Safari Android iOS 鸿蒙 微信小程序
√ √ √ √ × √

其他

多语言 暗黑模式 宽屏模式 蒸汽模式
× × × √

hy-notification 本地通知

跨平台本地通知 UTS 插件,基于各端原生能力实现通知渠道、权限申请、常驻/进度条通知、定时通知与点击回调,同一套 API 适配 Android / iOS 原生,Web 与微信小程序降级。

平台支持

平台 支持 实现方式
Android √ NotificationCompat + NotificationChannel + AlarmManager
iOS √ UserNotifications(UNUserNotificationCenter)
Web √ 浏览器 Notification API(定时用前台定时器模拟)
微信小程序 √(降级) 无系统通知栏,降级为应用内 toast

iOS 进度条为更新式(进度文本追加到内容),无系统原生进度条。 iOS 需在 macOS 环境下编译验证。

安装

将 hy-notification 目录放入项目的 uni_modules 下即可,无需手动注册(uni_modules 自动引入)。

使用

引入

import {
  initNotification,
  requestNotificationPermission,
  getNotificationPermission,
  createNotificationChannel,
  deleteNotificationChannel,
  sendNotification,
  updateNotificationProgress,
  cancelNotification,
  scheduleNotification,
  cancelScheduleNotification,
  onNotificationClick,
  offNotificationClick
} from '@/uni_modules/hy-notification'

初始化

initNotification({
  channel: {
    id: 'default',
    name: '默认通知',
    description: '应用默认通知渠道',
    importance: 'default'
  },
  requestPermission: true,
  success: (res) => {
    console.log('权限状态', res.granted, res.status)
  }
})

发送通知

sendNotification({
  id: 1001,
  title: '标题',
  content: '通知内容',
  channelId: 'default',
  payload: { page: 'detail', id: '123' },
  success: (res) => {
    console.log('已发送', res.id)
  },
  fail: (err) => {
    console.error('发送失败', err.errCode, err.errMsg)
  }
})

进度条通知

const id = 2001
sendNotification({ id, title: '下载中', progress: { max: 100, current: 0 } })

updateNotificationProgress({
  id,
  progress: { max: 100, current: 50 },
  title: '下载中',
  content: '已完成一半'
})

常驻通知

sendNotification({
  id: 3001,
  title: '正在运行',
  content: '后台服务运行中',
  ongoing: true,
  autoCancel: false
})

定时通知

scheduleNotification({
  id: 4001,
  title: '会议提醒',
  content: '5 分钟后开始',
  triggerAt: Date.now() + 5 * 60 * 1000,
  repeat: 'none'
})

// 取消
cancelScheduleNotification({ id: 4001 })

监听点击 / 清除

onNotificationClick((event) => {
  console.log('动作', event.action, '通知 id', event.id, '透传数据', event.payload)
})

// 取消监听
offNotificationClick()

API

initNotification(options)

初始化:创建 Android 通知渠道并(可选)申请通知权限。options 为必填(可为空对象 {})。

参数 类型 必填 说明
channel object 否 初始化时创建的 Android 通知渠道,见 NotificationChannelOptions
requestPermission boolean 否 是否在初始化时申请权限,默认 true
success / fail / complete function 否 回调

requestNotificationPermission(options?)

申请通知权限。

  • Android:申请 POST_NOTIFICATIONS(Android 13+)。
  • iOS:调用系统授权弹窗。
  • Web:调用 Notification.requestPermission()。
  • 微信小程序:返回 status: 'unsupported'。

成功回调 res:{ granted: boolean, status: 'granted' | 'denied' | 'notDetermined' | 'unsupported' }

getNotificationPermission(options?)

查询当前权限状态,回调结构同上。

createNotificationChannel(options)

创建 Android 通知渠道(iOS / Web / 小程序为空实现或直接成功)。

参数 类型 必填 说明
id string 是 渠道 id
name string 是 渠道名称
description string 否 渠道描述
importance string 否 重要性:min / low / default / high / max,默认 default
sound boolean 否 是否声音,默认 true
vibrate boolean 否 是否震动,默认 true
lights boolean 否 是否呼吸灯,默认 false

deleteNotificationChannel(options)

删除 Android 通知渠道。

参数 类型 必填 说明
id string 是 渠道 id

sendNotification(options)

发送通知。

参数 类型 必填 说明
id number 否 通知 id,相同 id 互相覆盖,不传自动生成
title string 是 通知标题
content string 否 通知内容
subTitle string 否 副标题
icon string 否 小图标资源名(Android drawable/mipmap)
largeIcon string 否 大图标资源名(Android)
badge number 否 角标数字
sound boolean 否 是否声音,默认 true
vibrate boolean 否 是否震动,默认 true(Android)
lights boolean 否 是否呼吸灯,默认 false(Android)
ongoing boolean 否 是否常驻,默认 false
autoCancel boolean 否 点击后是否自动清除,默认 true
channelId string 否 渠道 id,默认 default
importance string 否 重要性,默认 default
payload object 否 透传数据,点击时原样回调
progress object 否 进度条配置,见下

进度对象:{ max: number, current: number, indeterminate?: boolean }

成功回调 res:{ id: number }

updateNotificationProgress(options)

更新进度通知(同 id 覆盖)。

参数 类型 必填 说明
id number 是 需要更新的通知 id
progress object 是 新的进度配置
title string 否 更新后的标题
content string 否 更新后的内容

cancelNotification(options?)

取消通知。不传 id 时清除全部。

参数 类型 必填 说明
id number 否 需要取消的通知 id,不传清除全部

scheduleNotification(options)

定时通知。

参数 类型 必填 说明
id number 是 定时通知 id(用于取消)
title string 是 通知标题
content string 否 通知内容
triggerAt number 是 触发时间戳(毫秒),≤ 当前时间立即触发
repeat string 否 重复类型:none / minute / hourly / daily / weekly,默认 none
channelId string 否 渠道 id
importance string 否 重要性
sound boolean 否 是否声音,默认 true
vibrate boolean 否 是否震动,默认 true
payload object 否 透传数据

cancelScheduleNotification(options)

取消定时通知。{ id: number }

onNotificationClick(callback)

注册通知点击 / 清除监听,回调参数 NotificationClickEvent:

字段 类型 说明
id number 通知 id
action string click 或 dismiss
title string 通知标题
content string 通知内容
payload object | null 发送时透传的数据

offNotificationClick()

移除点击 / 清除监听。

错误码

errCode 说明
9020001 参数错误
9020002 通知权限被拒绝
9020003 通知发送失败
9020004 当前平台不支持系统通知
9020005 取消通知失败
9020006 定时通知调度失败
9020007 通知渠道操作失败

注意事项

  • Android 13+ 需申请 POST_NOTIFICATIONS 权限,initNotification / requestNotificationPermission 会自动处理。
  • Android 8.0+ 若发送时目标渠道不存在,插件会自动兜底创建该渠道,避免通知被系统静默丢弃。
  • Android 精确闹钟(SCHEDULE_EXACT_ALARM)在部分机型需用户手动开启,否则定时可能不精确;未授权时插件自动降级为非精确闹钟。
  • Android 定时通知在设备重启后会自动恢复(依赖 RECEIVE_BOOT_COMPLETED)。
  • iOS 无通知渠道概念,createNotificationChannel / deleteNotificationChannel 直接成功。
  • iOS 无原生进度条,进度以文本形式追加到通知内容中。
  • iOS 用户主动清除通知触发的 dismiss 事件需注册通知分类才会回调,当前仅在点击通知时触发 click。
  • iOS repeat 为 minute / hourly 时从设置时刻起按固定间隔重复,不使用 triggerAt。
  • iOS 插件会设置 UNUserNotificationCenter 的 delegate,若与其它通知方案(如 uniPush)同时使用可能存在回调冲突。
  • Web 通知依赖浏览器授权与页面存活,页面关闭后无法接收点击回调。
  • 微信小程序不支持系统通知栏,sendNotification 等接口降级为应用内 toast,权限状态恒为 unsupported。

更新日志

见 changelog.md

隐私、权限声明

1. 本插件需要申请的系统权限列表:

无

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

无

许可协议

MIT协议

暂无用户评论。