更新记录

1.0.0(2025-04-12)

本地通知消息推送创建、删除


平台兼容性

uni-app

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

uni-app x

Chrome Safari Android iOS 鸿蒙 微信小程序
- - 5.0 12 × ×

本地通知消息推送创建、删除

集成步骤

  1. 拷贝demo里的nativeResources文件到项目根目录
  2. 集成插件,集成插件步骤请参考 https://www.cnblogs.com/wenrisheng/p/18323027

接口

    import {
    UTSLocalNotification
} from "@/uni_modules/wrs-uts-localNotification"
  • 设置通知回调

var params = {}

// params参数主要用于Android的动作按钮
params.actions = ["next", "before", "play"]

UTSLocalNotification.onCallback(params, (resp) => {
    console.log(JSON.stringify(resp))
    if(this.isAndroid) {

    } else { // ios
        let opt = resp.opt
        switch (opt) {
            // 将要显示通知
            case "willPresent":
                break;
                // 用户点击打开通知
            case "didReceive":
                console.log("用户点击打开通知:" + JSON.stringify(resp))
                break;
            default:
                break;
        }
    }
})
  • 请求通知权限

if (this.isAndroid) {
    this.requestPermission([
        "android.permission.POST_NOTIFICATIONS"
    ])
} else {
    // 请求通知权限
    UTSLocalNotification.requestAuthorization({
        types: ["badge", "sound", "alert"]
    }, (resp) => {
        let flag = resp.flag
        if (!flag) { // 请求权限失败
            console.log("requestAuthorization:" + JSON.stringify(resp))
        }
    })

}
  • 初始化本地通知,调用一次就可以了,仅支持iOS

UTSLocalNotification.notificationInit()


- 设置通知的分类,仅支持iOS,创建复杂通知(如通知了展示输入框)的时候用到,普通业务一般用不上

```javascript

let params = {}
params.categories = [{
    identifier: this.categorieIdentifier,
    actions: [{
            identifier: "name",
            actionType: "textInputNotification",
            title: "点击输入姓名",
            buttonTitle: "提交",
            placeholder: "请输入姓名",
            options: ["foreground"]
        },
        {
            identifier: "ignore",
            actionType: "notification",
            title: "忽略",
            options: ["destructive"]
        },
        {
            identifier: "opt",
            actionType: "notification",
            title: "多操作",
            options: ["authenticationRequired"]
        }
    ]
}]
UTSLocalNotification.setNotificationCategories(params)
  • 创建发起通知消息

let params = {}
if (this.isAndroid) { // android
    params.identifier = this.identifier // 通知ID,主要用于修改、删除通知,android里的identifier必须是数字,iOS的identifier可以是任意字符串
    params.channel = { // 如果已经channelId的channel则不创建,如果没有则会自动创建
        channelId: this.channelId,
        channelName: "支付channel",
        // importance: 0, // 3: default 4: high 2: low 5: max 1: min 0: none
        // lockscreenVisibility: 1, //1: public 0: private -1: secret
        description: "收付款通知channel"
    }
    params.notification = {
        channelId: this.channelId, // 此消息通知是属于哪个channel的
        contentTitle: "张三来电",
        contentText: "您好",
        contentIntent: {
            intent: {
                className: "io.dcloud.PandoraEntry",
                extra: {
                    name: "张三",
                    age: 14
                }
            }
        },
        // visibility: 1, // 1: public 0: private -1: secret

        smallIcon: { // 小图标,必传
            type: "resource", // 固定
            defType: "drawable", // 固定
            name: "not" // 文件名,不要带文件后缀,对应nativeResources/android/res/drawable文件夹下的图片
        },
        autoCancel: true,
        // priority: 1, // 1: high 0: default -1: low -2:min 2: max
        // actions: [ // 给消息通知添加按钮,可选
        //  {
        //      action: "before",
        //      title: "上一个",
        //      requestCode: 100,
        //      flags: 67108864,
        //      extra: {
        //          name: "12",
        //          age: 552
        //      },
        //      icon: {
        //          type: "resource",
        //          defType: "drawable",
        //          name: "before"
        //      }
        //  },
        //  {
        //      action: "play",
        //      title: "播放",
        //      requestCode: 100,
        //      flags: 67108864,
        //      icon: {
        //          type: "resource",
        //          defType: "drawable",
        //          name: "play"
        //      }
        //  }, {
        //      action: "next", // 动作名称,对标registerReceiver按钮事件回调里的按钮标识
        //      title: "下一个",
        //      requestCode: 100,
        //      flags: 67108864,
        //      icon: {
        //          type: "resource",
        //          defType: "drawable",
        //          name: "next"
        //      }
        //  }

        // ]
    }
} else { // ios
    params.title = "张三来电"
    params.subtitle = "您好"
    params.body = "您收到了一条新消息"
    params.sound = "default"
    params.trigger = {
        timeInterval: 0.2, // 多少秒后执行
        repeats: false // 是否重复
    }
    params.identifier = this.identifier // 通知ID,主要用于修改、删除通知
}
UTSLocalNotification.add(params, (resp) => {
    console.log(JSON.stringify(resp))
})
  • 删除通知

let params = {}
params.identifiers = [
    this.identifier
]
UTSLocalNotification.remove(params)
  • 删除所有通知,仅支持Android

UTSLocalNotification.removeAll()
  • 用户点击某个消息的回调
  1. Android通过在onShow时通过获取getUTSIntentData获取到通知的数据
onShow () {
    if(this.isAndroid) {
        let intent = UTSLocalNotification.getUTSIntentData()
        this.showMsg("intent:" + JSON.stringify(intent))
    }   
}
  1. iOS通过回调获取点击事件

var params = {}

UTSLocalNotification.onCallback(params, (resp) => {
    console.log(JSON.stringify(resp))
        let opt = resp.opt
        switch (opt) {
            // 将要显示通知
            case "willPresent":
                break;
                // 用户点击打开通知
            case "didReceive":
                console.log("用户点击打开通知:" + JSON.stringify(resp))
                break;
            default:
                break;
        }
})

隐私、权限声明

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

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

插件不采集任何数据

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

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问