公共事件和通知

 公共事件

事件(Event)是指对由系统、组件、应用程序等对对象发起操作的封装。事件的发布者通常情况下回维持自己的状态不变,如果收到了某些交互操作所产生的信号而改变了它的状态,就会将消息发布给时间接收者。事件接收者可以是订阅用户或者相关的应用程序。

公共事件(Common Event)是指通过广播的形式发出的事件,即一个事件发布者会将消息发送给多个事件接收者。

公共事件服务

HarmonyOS中使用公共事件服务(Common Event Service,CES)为应用程序提供发布、订阅及退订公共事件的能力,系统和应用程序都可以发布公共事件,接收者可以使应用程序自身或者其他应用程序。根据事件发布者的不同,公共事件分为系统公共事件和自定义公共事件。

系统公共事件是指系统将收集到的事件息,根据系统策略发送给订阅该事件的用户的应用程序。系统公共事件的发布者是系统。常见的系统公共事件包括终端用户能感知到的亮灭屏事件,还有由系统关键服务发布的公共事件,如USB插拔、网络连接、HAP安装与卸载等事件。

自定义公共事件是指由应用程序定义的期望特定订阅者可以接收的公共事件。这些公共事件往往和应用程序的业务逻辑相关,通过调用系统接口自定义一些公共事件,从而实现跨应用的事件通信能力。

公共事件发布者可以是系统、应用程序自身或者其他应用程序。每个应用程序都可以是事件接收者。首先应用程序会根据需要订阅某个公共事件,当该公共事件发布时,系统会把该事件发送给已经订阅成功的应用程序。

公共事件处理接口

公共事件的处理接口是由commonEventManager模块提供的,使用前需要导入相应的模块

commonEventManager模块

commonEventManager模块提供的接口包括发布公共事件、创建订阅者、这月公共事件和取消订阅公共事件等。

发布公共事件接口(commonEventManager.publish)

该接口用于发布公共事件,并在发布后执行相应的回调函数

接口结构:

publish(event: string, callback: AsyncCallback<void>): void

指定发布信息并发布公共事件接口(commonEventManager.publish)

发布公共事件接口publish有一个可选的options参数,如果需要指定发布信息,则可以携带options参数。options主要用来指定发布信息的属性,具体内容类型为公共事件发布数据类型(CommonEventPublishData类),该类中所有参数都是可选的。

接口结构:

publish(event: string, options: CommonEventPublishData, callback: AsyncCallback<void>): void

创建订阅者接口(commonEventManager.createSubscriber) 

创建订阅者可以通过createSubscriber函数实现,其主要参数subscribeInfo表示订阅信息,对应类型为公共时间订阅信息类型,即CommonEventSubscribeInfo类。

接口结构:

该接口可以通过callback和promise两种回调方法来进行书写

createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventSubscriber>): void

/

createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber>

创建订阅者同步接口(commonEventManager.createSubscriberSync)

该接口为createSubscriber的同步接口,功能与其一致

接口结构:

createSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber

订阅公共事件接口(commonEventManager.subscribe) 

该接口用于以回调的形式订阅公共事件。

接口结构:

subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback<CommonEventData>): void


取消订阅公共事件接口(commonEventManager.unsubscribe)

该接口用于以回调形式取消订阅公共事件。

接口结构:

实际开发

导包

开发公共事件模块相关程序需要导入commonEventManager包,如下:

import { commonEventManager } from '@kit.BasicServicesKit';

发布公共事件

这里我将发布公共事件接口封装在一个自定义方法里面,通过this.msg的不同变化来模拟发布公共事件的成功与失败时所对应的的业务逻辑

publish(): void {
    commonEventManager.publish('textEvent',(err)=>{
      if(err){
        this.msg = `publish Error,${err}`
      } else {
      this.msg = `publish Success`
      }
    })
  }

创建订阅者

这里我将创建订阅者接口封装在一个自定义方法里面,通过this.msg的不同变化来模拟订阅者创建的成功与失败的情况以及已经有订阅者被创建出来后再调用该方法的情况对应的业务逻辑

  createSubscriber(): void {
    let subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
      events:['textEvent']
    }
    if(this.suber){
      this.msg = `subscriber is already there`
    } else {
      commonEventManager.createSubscriber(subscribeInfo,(err,sub) => {
        if (err) {
          this.msg = `createSuber Error ${err}}`
        } else {
          this.msg = `createSuber success`;
          this.suber = sub
        }
      })
    }
  }

订阅公共事件

这里我将订阅公共事件接口封装在一个自定义方法里面,通过this.msg的不同变化来模拟公共事件订阅的的成功与失败的情况对应的的业务逻辑

subscribe(): void {
    if(this.suber){
      commonEventManager.subscribe(this.suber,(err,data) => {
        if(err){
          this.msg = `sub false ${err}`
        } else {
        this.msg = `sub success! ${data} `
        }
      })
    } else {
        this.msg = 'create subscriber first!'
    }
  }

取消订阅公共事件

这里我将取消订阅公共事件接口封装在一个自定义方法里面,通过this.msg的不同变化来模拟取消公共事件订阅的的成功与失败的情况对应的的业务逻辑

unsubscribe(): void {
    commonEventManager.unsubscribe(this.suber,(err) => {
      if(err){
        `unsub Error ${err}`
      } else {
      this.msg = 'unsub succes!'
      }
    })
  }

通知

通知

导包

import { notificationManager } from '@kit.NotificationKit';

通知接口 

notificationManager.publish

该接口用于发布通知,由callback和promise两种异步回调方式

接口结构:

publish(request: NotificationRequest, callback: AsyncCallback<void>): void

/

publish(request: NotificationRequest): Promise<void>

后台代理提醒

除了应用程序可以发布通知,后台系统服务也可以代理发布提醒类通知。HarmonyOS中有倒计时Timer、日历Calendar、闹钟Alarm共三种提醒类型,一般与事件相关的定时提醒,用户可以在通知栏收到提醒,也可以关闭提醒。当用户在应用程序中放设置了定时提醒时,该应用可以被冻结或退出,由后台系统服务来代理计时功能和弹出提醒。

后台代理接口

后台代理提醒接口主要包括reminderAgentManger模块中的各种接口,其中ReminderRequest类用来创建代理提醒对象,用于设置提醒类型、响铃时长等具体信息

reminderAgentManger模块

reminderAgentManger模块中有各种用于处理后台大力提醒接口的API,每种API都可以采用callback和promise两种方式实现异步回调,下面对每一个API的介绍都以callback回调作为展开

reminderAgentManager.publishReminder

该接口用于发布后台代理提醒

接口结构:

publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void

reminderAgentManager.cancelReminder

该接口用于取消指定Id的代理提醒

接口结构:

cancelReminder(reminderId: number, callback: AsyncCallback<void>): void

reminderAgentManager.getValidReminders

该接口用于获取当前应用设置的所有有效(未过期)的代理提醒。

接口结构:

getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void

 reminderAgentManager.cancelAllReminders

该接口用于取消的当前应设置的所有代理提醒。

接口结构:

reminderAgentManager.addNotificationSlot

该接口用于添加NotifacationSlot(通知槽)。

接口结构:

 reminderAgentManager.removeNotificationSlot

该接口用于删除目标NoticaficationSlot(通知槽)

接口结构:

removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback<void>): void

API12+最新接口

以下接口均只提供了一种异步回调接口

 reminderAgentManager.getAllValidReminders

该接口用于获取当前应用设置的所有有效(未过期)的代理提醒。该方法使用promise异步回调

说明

当到达设置的提醒时间点时,通知中心会弹出相应提醒的通知卡片(通知栏消息)。若未点击通知卡片上的关闭/CLOSE按钮,则代理提醒是有效/未过期的;若点击了关闭/CLOSE按钮,则代理提醒过期。

当代理提醒类型是闹钟时,若设置每天提醒,无论是否点击关闭/CLOSE按钮,代理提醒都是有效的。

接口结构:

getAllValidReminders(): Promise<Array<ReminderInfo>>

 reminderAgentManager.addExcludeDate

该接口用于为指定id的重复日历添加不提醒日期,即在设定的日期范围内不提醒。使用Promise异步回调。

接口结构:

addExcludeDate(reminderId: number, date: Date): Promise<void>

 reminderAgentManager.deleteExcludeDates

该接口用于删除重复日历设置的所有不提醒日期,通过指定Id指定具体重复日历。使用Promise异步回调。

接口结构:

deleteExcludeDates(reminderId: number): Promise<void>

 reminderAgentManager.getExcludeDates

该接口用于查询重复日历设置的所有不提醒日期,通过Id指定具体重复日历。使用Promise异步回调。

接口结构:

getExcludeDates(reminderId: number): Promise<Array<Date>>

实际开发

声明使用权限

使用后台代理提醒需要在配置文件module.json5文件中声明需要此权限:

        "name": "ohos.permission.PUBLISH_AGENT_REMINDER"
导包 
编写后台代理通知需要导入如下两个资源包
import { BusinessError } from '@kit.BasicServicesKit';
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
定义一个倒计时提醒实例ReminderRequestTimer

    let timer: reminderAgentManager.ReminderRequestTimer = {
      reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
      triggerTimeInSeconds: 10,
      title: "登陆界面后台代理倒计时通知"
    }
发布代理
    reminderAgentManager.publishReminder(timer, (err: BusinessError, reminderId: number) => {
      if (err.code) {
        console.error("callback err code:" + err.code + " message:" + err.message);
      } else {
        console.log("callback, reminderId = " + reminderId);
      }
    });
运行程序并查看通知是否发送成功

代理发送成功!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值