学习鸿蒙,我学到的东西与浅显理解(2-5、通知、提醒)

268 阅读4分钟

通知

  • 显示接收到的短消息、即时消息等。
  • 显示应用的推送消息,如广告、版本更新等。
  • 显示当前正在进行的事件,如下载等。

image.png

  1. 通知小图标:表示通知的功能与类型。
  2. 通知名称:应用名称或功能名称。
  3. 时间:发送通知的时间,系统默认显示。
  4. 展开箭头:点击标题区,展开被折叠的内容和按钮
  5. 内容标题:描述简明概要。
  6. 内容详情:描述具体内容或详情。

基础类型

import { notificationManager } from '@kit.NotificationKit'

 // 定义通知实例
const notificationRequest: notificationManager.NotificationRequest = {
  // 通知 ID,用于更新或取消通知
  id: 1,
  largeIcon: '', // 图标,image.PixelMap 类型
  content: {
    // 通知类型,这里是文本类型里的普通文本通知
    notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    // 通知内容
    normal: {
      title: '通知的标题', 
      text: '通知的详情' 
    } 
  } 
} 

// 发布通知 
notificationManager.publish(notificationRequest).then(() => { 
  console.info('publish success')
}).catch((err: Error) => { 
  console.error(`publish failed,message is ${err}`); 
})

进度类型

// 先查询是否支持进度条通知
const isSupport = await notificationManager.isSupportTemplate('downloadTemplate')

const template: notificationManager.NotificationTemplate = { 
  name: 'downloadTemplate', 
  data: { 
    progressValue: progress, // 当前进度值 
    progressMaxValue: 100 // 最大进度值 
  }
}

// 构造通知的信息
const notificationRequest: notificationManager.NotificationRequest = { 
  id: 1, 
  template: template,
  content: { 
    notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 
    normal: { 
      title: '文件下载:music.mp4', 
      text: 'senTemplate', 
      additionalText: '60%' 
    } 
  } 
}

// 发布通知 
notificationManager.publish(notificationRequest).then(() => { 
  console.info(`publish success`); 
}).catch((err: Error) => { 
  console.error(`publish failed,message is ${err}`); 
})

更新通知

  • 在发出通知后,使用您之前使用的相同通知 ID
  • 再次调用 notificationManager.publish 来实现通知的更新
  • 如果之前的通知是关闭的,将会创建新通知

取消通知

// 通过通知ID和通知标签取消已发布的通知。
notificationManager.cancel(notificationId)

// 取消所有已发布的通知。
notificationManager.cancelAll()

通知表现形式

  • 通知通道,可以让通知有不同的表现形式
  • 比如社交类型的通知是横幅显示的,并且有提示音
  • 而一般的通知则不会横幅显示
// 社交类型,状态栏中显示通知图标,有横幅和提示音
slotType: SlotType.SOCIAL_COMMUNICATION

// 服务类型,状态栏中显示通知图标,没有横幅但有提示音
slotType: SlotType.SERVICE_INFORMATION

// 内容类型,状态栏中显示通知图标,但没有横幅或提示音
slotType: SlotType.CONTENT_INFORMATION

// 其它类型,状态栏中不显示通知图标,且没有横幅或提示音
slotType: SlotType.OTHER_TYPES


// 设置
notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION)

通知组

  • 将不同类型的通知分为不同的组,以便用户可以更好的管理他们
  • 当同组的通知有多条的时候,会自动折叠起来
  • 避免通知比较多的时候,界面比较杂乱
const notifyId = 0;
 
const chatRequest: notificationManager.NotificationRequest = {  
  id: notifyId++, 
  groupName: 'ChatGroup', 
  content: { 
    //... 
   } 
 }
 
const productRequest: notificationManager.NotificationRequest = {  
  id: notifyId++, 
  groupName: 'ProductGroup', 
  content: { 
    //... 
   } 
 }

通知的点击事件

import { notificationManager } from '@kit.NotificationKit'
import { wantAgent, WantAgent } from '@kit.AbilityKit'

// 第一步:创建 WantAgentInfo 信息
// 场景一:拉起 UIAbilit
const wantAgentInfo = {
  wants: [ 
    { 
      bundleName: "com.example.notification", 
      abilityName: "EntryAbility" 
    } 
  ], 
  operationType: wantAgent.OperationType.START_ABILITY, 
  requestCode: 100 
}

// 场景二:发布公共事件
const wantAgentInfo = { 
  wants: [ 
    { 
      action: 'event_name', // 设置事件名 
      parameters: {}, 
    } 
  ], 
  operationType: wantAgent.OperationType.SEND_COMMON_EVENT, 
  requestCode: 100, 
  wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 
}


// 第二步:创建 WantAgent 对象
const wantAgentObj = await wantAgent.getWantAgent(wantAgentInfo) 

  
// 第三步:构造 NotificationRequest 对象
const notificationRequest: notificationManager.NotificationRequest = {
  id: 1, 
  wantAgent: wantAgentObj,
  content: { 
    notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 
    normal: { 
      title: "通知标题", 
      text: "通知内容" 
    } 
  }
}

// 第四步:发布 WantAgent 通知
notificationManager.publish(notificationRequest)

提醒

  • 倒计时类:适用于短时的计时提醒业务
  • 日历类:适用于较长时间的提醒业务
  • 闹钟类:适用于指定时刻的提醒业务

申请权限

"module": {
  // ...
  "requestPermissions": [
    {
     "name": "ohos.permission.PUBLISH_AGENT_REMINDER"
    }
  ]
}

使用示例

import { reminderAgentManager } from '@kit.BackgroundTasksKit'

// 注意:这个 item 的参数值,官方课程里没有,是我查了 ChatGPT 后写的,仅供参考
const item = {
  remindType: 'alarm', // 提醒类型,可选值如 'alarm' (闹钟), 'event' (事件), 'reminder' (提醒) 等
  hour: 8,             // 提醒时间的小时部分 (24小时制)
  minute: 30,          // 提醒时间的分钟部分
  repeatDays: [1, 3, 5], // 重复提醒的星期几,以数组形式表示,0代表星期日,1-6分别代表星期一到星期六
  name: 'Morning Exercise', // 提醒名称
  duration: 5,         // 提醒持续时间,单位是分钟
  intervalTimes: 3,    // 重复提醒次数 (例如贪睡次数)
  intervalMinute: 10,  // 每次提醒之间的间隔时间,单位是分钟
  notificationId: 'reminder_12345' // 提醒的唯一标识符,用于管理或取消提醒
}

// 定义提醒实例
const reminder = {
  reminderType: item.remindType,
  hour: item.hour,
  minute: item.minute,
  daysOfWeek: item.repeatDays,
  title: item.name,
  ringDuration: item.duration * CommonConstants.DEFAULT_TOTAL_MINUTE,
  snoozeTimes: item.intervalTimes,
  timeInterval: item.intervalMinute * CommonConstants.DEFAULT_TOTAL_MINUTE,
  notificationId: item.notificationId,
  actionButton: [
    {
      title: '关闭',
      type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
    },
  ],
  wantAgent: {
    pkgName: CommonConstants.BUNDLE_NAME,
    abilityName: CommonConstants.ABILITY_NAME
  },
}

// 发布提醒
reminderAgentManager.publishReminder(reminder, (err, reminderId) => {
  if (callback != null) {
    callback(reminderId);
  }
})

// 删除提醒
reminderAgentManager.cancelReminder(reminderId)

// 修改提醒
// 没有修改,只能删除后,重新添加

系列文章


参考资料


写在最后

  • 不是教程,只是学习记录
  • 包含了一些自己的理解,一边学一边写的,难免有不对的地方
  • 写出来希望能与大家探讨,看到有错误的地方,望大家指正~