介绍系统通知的使用方法,为应用提供通知功能。
本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。
介绍系统通知的使用方法,为应用提供通知功能。
本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。
在鸿蒙系统中使用系统通知,主要需使用Notification API及相关服务。以下是如何为应用提供通知功能的详细步骤:
确保项目中已引入必要的模块,通过SDK和API直接支持Notification功能。
使用Notification类或其子类创建通知,并设置通知属性,如标题、内容、图标等。例如:
import { notificationManager } from '@kit.NotificationKit';
let notificationRequest: notificationManager.NotificationRequest = {
id: 1, // 通知ID
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
normal: {
title: '通知内容标题',
text: '通知内容详情'
}
}
};
若应用目标版本要求类似Android的通知渠道机制,则需先注册通知渠道。
使用NotificationManager或类似服务发送通知。例如:
notificationManager.publish(notificationRequest).then(() => {
console.info('publish success');
}).catch((err: Error) => {
console.error(`publish failed, message is ${err}`);
});
若通知可交互(如包含按钮或可展开的内容),还需实现相应的回调或广播接收器处理交互。
鸿蒙系统提供了Push Kit推送服务,可建立从云端到终端的消息推送通道,实现实时推送消息。开发者需集成Push Kit,并申请Push Token,以便根据Token推送消息。
完成通知功能的集成后,需进行充分测试以确保通知正常显示和交互。
notificationManager.publish
来更新已发布的通知。notificationManager.cancelAll()
取消所有已发布的通知。notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION).then(() => {
console.info("addSlot success");
}).catch((err: Base.BusinessError) => {
console.error(`addSlot fail: ${JSON.stringify(err)}`);
});
如果应用首次被拒绝了消息通知权限,可以引导用户拉起系统设置应用来设置允许通知。例如:
let want: Want = {
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: "systemui_notification_settings",
parameters: {
pushParams: {
bundleName: context.abilityInfo.bundleName
}
}
};
context.startAbility(want);
并在打开页面时获取通知管理中按钮状态,使用Notification.requestEnableNotification()
请求发送通知的许可。
通过以上步骤,你可以在鸿蒙系统中为应用提供丰富的通知功能,提升用户体验。
鸿蒙系统(HarmonyOS)的通知系统与Android系统在许多方面相似,但也有一些特定的差异。以下是在鸿蒙系统中使用系统通知的步骤:
{
"module": {
"reqPermissions": [
{
"name": "ohos.permission.NOTIFICATION_CONTROLLER"
}
]
}
}
创建通知渠道(HarmonyOS 3.0及以上版本需要)
if (ohos.systemversion.Build.VERSION.SDK_INT >= ohos.systemversion.Build.VERSION_CODES.S) {
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationSlotType.OTHER_TYPES);
notificationHelper.createNotificationChannel(channel);
}
NotificationRequest request = new NotificationRequest.Builder()
.setContentTitle("通知标题")
.setContentText("通知内容")
.setSmallIcon(ResourceTable.Media_ic_notification)
.setAutoDeletedTime(10000) // 设置通知自动删除时间,单位为毫秒
.setSlotType(NotificationSlotType.OTHER_TYPES)
.build();
显示通知
创建NotificationHelper实例,并使用它来发送通知。
NotificationHelper notificationHelper = new NotificationHelper(context);
notificationHelper.notify(100, request);
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.example.app")
.withAbilityName("com.example.app.MainAbility")
.build();
intent.setOperation(operation);
// 将Intent转换为PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationRequest.Builder builder = new NotificationRequest.Builder()
.setContentIntent(pendingIntent)
// ... 其他设置
.build();
处理通知的点击事件
在你的Ability中重写onStart方法,以处理通知点击事件。
@Override
public void onStart(Intent intent) {
super.onStart(intent);
if (intent != null) {
String action = intent.getAction();
if (action != null && action.equals("action.notification.click")) {
// 处理通知点击事件
}
}
}
本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。
1 回答470 阅读✓ 已解决
1 回答487 阅读
1 回答415 阅读
451 阅读
442 阅读
432 阅读
398 阅读
Notification
API 创建通知。ohos.permission.NOTIFICATION
.