接口介绍
通知相关基础类包含NotificationSlot、NotificationRequest和NotificationHelper。详细的接口信息请参考通知开发指导。基础类之间的关系如下所示:
NotificationSlot可以对提示音、振动和重要级别等进行设置。一个应用可以创建一个或多个NotificationSlot,在发送通知时,通过绑定不同的NotificationSlot,实现不同用途。
NotificationSlot需要先通过NotificationHelper的addNotificationSlot(NotificationSlot)方法发布后,通知才能绑定使用;所有绑定该NotificationSlot的通知在发布后都具备相应的特性,对象在创建后,将无法更改其设置属性,对于是否启动相应设置,用户有最终控制权。
不指定NotificationSlot时,当前通知会使用默认的NotificationSlot,默认的NotificationSlot优先级为LEVEL_DEFAULT,声音为系统默认提示音。
LEVEL_DEFAULT/LEVEL_HIGH:表示通知发布后可在通知中心显示,自动弹出,触发提示音。
LEVEL_LOW:表示通知发布后可在通知中心显示,不自动弹出,触发提示音。
LEVEL_MIN:表示通知发布后可在通知中心显示,不自动弹出,不触发提示音。
NotificationRequest用于设置具体的通知对象,包括设置通知的属性,如:通知的小图标、自动删除等参数,以及设置具体的通知类型,如普通文本、长文本等。
标识说明:为通过NotificationRequest.setLittleIcon(PixelMap)设置的小图标。
从通知启动Ability:点击通知栏的通知,可以通过启动Ability,触发新的事件。
通知设置NotificationRequest的setIntentAgent(IntentAgent)后,点击通知栏上发布的通知,将触发通知中的IntentAgent承载的事件。IntentAgent的设置请参考IntentAgent开发指导。
通知设置ActionButton:通过点击通知按钮,可以触发按钮承载的事件。
标识说明:、
为两个通过NotificationRequest.addActionButton(NotificationActionButton)设置的通知附加按钮,点击按钮后可以触发相关的事件,具体事件内容如何设置需要参考NotificationActionButton。
标识说明:为通知的标题,通过NotificationNormalContent.setTitle(String)设置。
为通知的内容,通过NotificationNormalContent.setText(String)设置。通知标题和内容至少要设置一个。
长文本通知样式(NotificationLongTextContent)
标识说明:为通知的长文本,通过NotificationLongTextContent.setLongText(String)设置,文本长度最大支持1024个字符。
图片通知样式(NotificationPictureContent)
标识说明: 为图片通知样式的图片,通过NotificationPictureContent.setBigPicture(PixelMap bigPicture)设置。
通知发布后,通知的设置不可修改。如果下次发布通知使用相同的ID,就会更新之前发布的通知。
NotificationHelper封装了发布、更新、订阅、删除通知等静态方法。订阅通知、退订通知和查询系统中所有处于活跃状态的通知,有权限要求需为系统应用或具有订阅者权限。
开发步骤
通知的开发指导分为创建NotificationSlot、发布通知和取消通知等开发场景。
创建NotificationSlot
// 创建notificationSlot对象
NotificationSlot slot = new NotificationSlot(“slot_001”,“slot_default”, NotificationSlot.LEVEL_DEFAULT);
slot.setDescription(“NotificationSlotDescription”);
try {
NotificationHelper.addNotificationSlot(slot);
} catch (RemoteException ex) {
HiLog.warn(LABEL, “addNotificationSlot occur exception.”);
}
发布通知
构建NotificationRequest对象,应用发布通知前,通过NotificationRequet的setSlot()方法与NotificationSlot绑定,使该通知在发布后都具备该对象的特征。
int notification_id = 1;
NotificationRequest request = new NotificationRequest(notification_id);
request.setSlotId(slot.getId());
调用setContent()设置通知的内容。
String title = “title”;
String text = “There is a normal notification content.”;
NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
content.setTitle(title)
.setText(text);
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
// 设置通知的小图标
request.setLittleIcon(PixelMap);
// 设置通知的内容
request.setContent(notificationContent);
调用setIntentAgent()设置通知可以触发的事件。
// 指定要启动的ability属性
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId(“”)
.withBundleName(“com.example.testintentagent”)
.withAbilityName(“com.example.testintentagent.IntentAgentAbility”)
.build();
intent.setOperation(operation);
List<Intent> intentList = new ArrayList<>();
intentList.add(intent);
// 指定启动一个有页面的ability
IntentAgentInfo intenAgentinfo = new IntentAgentInfo(request.getNotificationId(), IntentAgentConstant.OperationType.START_ABILITY, IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG, intentList, null);
// 获取IntentAgent实例
IntentAgent intentAgent = IntentAgentHelper.getIntentAgent(mContext, intenAgentinfo);
request.setIntentAgent(intentAgent);
request.setTapDismissed(true);
调用publishNotification()发送通知。
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException ex) {
HiLog.warn(LABEL, “publishNotification occur exception.”);
}
取消通知
取消通知分为取消指定单条通知和取消所有通知,应用只能取消自己发布的通知。
调用cancelNotification()取消指定的单条通知。
int notification_id = 1;
try {
NotificationHelper.cancelNotification(notification_id);
} catch (RemoteException ex) {
HiLog.warn(LABEL, “cancelNotification occur exception.”);
}