HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建
HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建
创建状态栏通知代码如下:
let notificationRequest: notificationManager.NotificationRequest = {
id: 1,
notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
normal: { title: '大众新闻', text: getuiPushEntity.newsTitle, }
}
};
notificationManager.publish(notificationRequest, (err: Base.BusinessError) => {
if (err) {
console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Succeeded in publishing notification.');
});
更多关于HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
场景一:创建拉起UIAbility的WantAgent的WantAgentInfo信息。
–这里主要是点击通知后进入对应app的功能,用于拉起应用的场景
场景二:创建发布公共事件的WantAgent的WantAgentInfo信息。
–这里主要是发送事件通知的场景,如果该应用订阅了对应的通知事件,则会执行订阅事件的回调
更多关于HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)Next中,状态栏通知的点击事件可以通过两种场景进行区分创建:一种是点击通知后直接打开应用,另一种是点击通知后执行特定操作。这两种场景可以通过设置NotificationRequest
中的WantAgent
来实现。
-
直接打开应用:当用户点击通知后,系统会启动应用的主界面。可以通过设置
WantAgent
的operationType
为WantAgent.OperationType.START_ABILITY
,并指定应用的主Ability。示例代码如下:let wantAgentInfo = { wants: [ { bundleName: "com.example.myapp", abilityName: "MainAbility" } ], operationType: WantAgent.OperationType.START_ABILITY }; let wantAgent = await WantAgent.getWantAgent(wantAgentInfo);
-
执行特定操作:当用户点击通知后,系统会执行特定的操作,例如跳转到应用的某个特定页面或执行某个功能。可以通过设置
WantAgent
的operationType
为WantAgent.OperationType.START_ABILITY
,并指定对应的Ability或Action。示例代码如下:let wantAgentInfo = { wants: [ { bundleName: "com.example.myapp", abilityName: "DetailAbility", parameters: { key: "value" } } ], operationType: WantAgent.OperationType.START_ABILITY }; let wantAgent = await WantAgent.getWantAgent(wantAgentInfo); ``
在创建NotificationRequest
时,将WantAgent
对象传递给NotificationRequest
的wantAgent
属性即可实现不同的点击事件处理。
在HarmonyOS鸿蒙Next中,创建状态栏通知的点击事件可以通过两种场景进行区分:
-
直接打开应用:通过设置
PendingIntent
,当用户点击通知时,直接启动应用的主界面或指定Activity。使用Intent
指定目标Activity,并通过PendingIntent.getActivity()
创建PendingIntent
。 -
执行特定操作:通过设置
PendingIntent
执行特定操作,如启动Service或发送Broadcast。使用Intent
指定目标Service或BroadcastReceiver,并通过PendingIntent.getService()
或PendingIntent.getBroadcast()
创建PendingIntent
。
根据需求选择合适的PendingIntent
类型,确保点击通知后执行预期的操作。