实现一个发送进度条通知的方法

实现一个发送进度条通知的方法

HarmonyOS
2024-08-07 10:19:15
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
一意孤行的
// NotificationUtil.ets
import { notificationManager } from '@kit.NotificationKit';

// 获取应用的Want信息
export function createWantAgent(bundleName: string, abilityName: string): Promise<object> {
  let wantAgentInfo = {
    wants: [
      {
        bundleName: bundleName,
        abilityName: abilityName
      }
    ],
    operationType: wantAgent.OperationType.START_ABILITY,
    requestCode: 0,
    wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
  } as wantAgent.WantAgentInfo;
  return wantAgent.getWantAgent(wantAgentInfo);
}

// 发布通知
export function publishNotification(progress: number, title: string, wantAgentObj: object) {
  // 构造通知模板对象
  let template:notificationManager.NotificationTemplate = {
    name: 'downloadTemplate',
    data: {
      progressValue: progress,
      progressMaxValue: CommonConstants.PROGRESS_TOTAL,
      isProgressIndeterminate: false
    }
  };
  // 构造NotificationRequest对象
  let notificationRequest: notificationManager.NotificationRequest = {
    id: CommonConstants.NOTIFICATION_ID,
    notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION,
    // 模板对象
    template: template,
    content: {
      notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
      normal: {
        title: `${title}:${CommonConstants.DOWNLOAD_FILE}`,
        text: ' ',
        additionalText: `${progress}%`
      }
    },
    // 点击拉起应用的Want信息
    wantAgent: wantAgentObj
  };
  // 发送通知
  notificationManager.publish(notificationRequest).catch((err: Error) => {
    Logger.error(`[ANS] publish failed,message is ${err}`);
  });
}
分享
微博
QQ
微信
回复
2024-08-07 15:11:57
相关问题
如何实现一个月食样式进度条
128浏览 • 1回复 待解决
弧形进度条实现,有人知道方法吗?
483浏览 • 1回复 待解决
如何实现带刻度进度条
240浏览 • 1回复 待解决
如何实现带图片进度条
498浏览 • 1回复 待解决
基于Progress组件进度条
238浏览 • 1回复 待解决
Progress进度条如何实现渐变色?
255浏览 • 1回复 待解决
服务卡片进度条如何停止动画
8548浏览 • 1回复 待解决
app切换到后台时进度条处理问题
2381浏览 • 0回复 待解决
怎么在进度条更新时候刷新页面?
4424浏览 • 1回复 待解决
如何设置一个通知按钮
127浏览 • 1回复 待解决
视频进度滑动三种实现方式
721浏览 • 1回复 待解决
如何实现一个监听网络变化方法
195浏览 • 1回复 待解决
需要一个获取当前省份方法
134浏览 • 1回复 待解决
销毁一个子窗口方法
126浏览 • 1回复 待解决
实现一个虚线边框组件。
241浏览 • 1回复 待解决