HamonyOS进度条通知

本文介绍了如何在HarmonyOS中使用内置的NotificationManager模块创建并发送动态进度条通知,包括检查设备支持、设置模板、定时更新进度和发送请求的过程。
摘要由CSDN通过智能技术生成

今天
我们来说 harmonyos 发送进度条通知

首先 什么是进度条通知?
进度条通知 是 展示一个静态的进度条,主要用于 长任务 或者 文件下载的进度提醒

那么 话不多说 直接打开项目
编写 页面代码如下

import notificationManager from "@ohos.notificationManager"
@Entry
@Component
struct Dom {

  @State progressValue:number = 0;

  async SendNotifications(){
    // 获取判断条件 判断当前设备是否支持进度条模板
    let isSupport = await notificationManager.isSupportTemplate('downloadTemplate')
    // 如果不支持
    if(!isSupport) {
      // 直接返回 终止函数运行
      return
    }
    // 通知模板
    let template = {
      // 模板名称  与 isSupportTemplate参数一直即可
      name: "downloadTemplate",
      data: {
        // 当前进度的数值
        progressValue: this.progressValue,
        // 最大值  进度到达多少满
        progressMaxValue: 100
      }
    }

    let request: notificationManager.NotificationRequest = {
      id: 999,
      template: template,
      content:{
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: "文件正在下载",
          text:"",
          additionalText: this.progressValue+"%"
        }
      }
    }
    //发送通知 传入 我们处理好的 request
    notificationManager.publish(request)
    .then(()=>console.log('test','通知发送成功'))
    .catch(reason => console.log('test','通知发送失败!',JSON.stringify(reason)))
    //定义一个定时任务 处理progressValue进度累加
    let vs = setInterval(()=> {
      //判断如果  progressValue 小于 100  则  将它+1
      if(this.progressValue < 100) {
        this.progressValue ++;
      }else{
        //否则 关闭定时任务
        clearInterval(vs)
      }
    },1000)
  }

  build() {
    Column({space: 30}) {
      Button("发送进度条通知").onClick(() => this.SendNotifications())
    }
    .width('100%')
    .height('100%')
  }
}

首先 我们导入了通知模块 @ohos.notificationManager
这个模块是 harmonyos 内置的 不需要安装 直接导入即可
然后 逻辑代码 我都写在了 SendNotifications 函数中
页面就给了个按钮 点击调用 SendNotifications
进度条通知 比较特殊 需要先通过 isSupportTemplate 获取 看拿不拿得到 如果拿不到 表示当前设备并不支持 那么 就直接 return出去就好了 后面的逻辑也执行不了

否则 继续template 是进度条通知的模板 name属性和isSupportTemplate传的一直就好了
data中 progressMaxValue是进度条总共多少 到多少加载完成 progressValue代表当前进度 我们定义了一个 progressValue记录当前的进度

然后 request 配置通知请求参数
然后 publish request 做参数 返送请求

下面定义了 setInterval 一个定时任务 一直把我们的 progressValue 加加 让进度一直往前走

我们真机运行
点击按钮
在这里插入图片描述
进度通知时来了 但你会发现 进度是不会动的
在这里插入图片描述

这里 就要科普一下 进度条通知是静态的 它并不是动态的 它自己不会变
所以 我们要这样改

import notificationManager from "@ohos.notificationManager"
@Entry
@Component
struct Dom {

  @State progressValue:number = 0;

  SendNotifications(){
    //定义一个定时任务 处理progressValue进度累加
    let vs = setInterval(async ()=> {
      //判断如果  progressValue 小于 100  则  将它+1
      if(this.progressValue < 100) {
        this.progressValue ++;
      }else{
        //否则 关闭定时任务
        clearInterval(vs)
      }
      // 获取判断条件 判断当前设备是否支持进度条模板
      let isSupport = await notificationManager.isSupportTemplate('downloadTemplate')
      // 如果不支持
      if(!isSupport) {
        // 直接返回 终止函数运行
        return
      }
      // 通知模板
      let template = {
        // 模板名称  与 isSupportTemplate参数一直即可
        name: "downloadTemplate",
        data: {
          // 当前进度的数值
          progressValue: this.progressValue,
          // 最大值  进度到达多少满
          progressMaxValue: 100
        }
      }

      let request: notificationManager.NotificationRequest = {
        id: 999,
        template: template,
        content:{
          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
          normal: {
            title: "文件正在下载",
            text:"",
            additionalText: this.progressValue+"%"
          }
        }
      }
      //发送通知 传入 我们处理好的 request
      notificationManager.publish(request)
        .then(()=>console.log('test','通知发送成功'))
        .catch(reason => console.log('test','通知发送失败!',JSON.stringify(reason)))
    },1000)
  }

  build() {
    Column({space: 30}) {
      Button("发送进度条通知").onClick(() => this.SendNotifications())
    }
    .width('100%')
    .height('100%')
  }
}

简单说 将发送通知的代码 放到定时任务里面 进度变一点 发一条
因为 你发通知 要保证一直是同一个通知 所以 就用固定id 去覆盖之前的 这样 就一直是同一个通知 只是进度不同
进行覆盖 用户看着 就好像是 一个通知 一直在进度向前

重新真机运行 我们进度就会实时变化了
在这里插入图片描述

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值