HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建

发布于 1周前 作者 eggper 来自 鸿蒙OS

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

3 回复

场景一:创建拉起UIAbility的WantAgent的WantAgentInfo信息。

–这里主要是点击通知后进入对应app的功能,用于拉起应用的场景

场景二:创建发布公共事件的WantAgent的WantAgentInfo信息。

–这里主要是发送事件通知的场景,如果该应用订阅了对应的通知事件,则会执行订阅事件的回调

更多关于HarmonyOS鸿蒙Next中创建的状态栏通知点击事件两种场景如何区分创建的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)Next中,状态栏通知的点击事件可以通过两种场景进行区分创建:一种是点击通知后直接打开应用,另一种是点击通知后执行特定操作。这两种场景可以通过设置NotificationRequest中的WantAgent来实现。

  1. 直接打开应用:当用户点击通知后,系统会启动应用的主界面。可以通过设置WantAgentoperationTypeWantAgent.OperationType.START_ABILITY,并指定应用的主Ability。示例代码如下:

    let wantAgentInfo = {
        wants: [
            {
                bundleName: "com.example.myapp",
                abilityName: "MainAbility"
            }
        ],
        operationType: WantAgent.OperationType.START_ABILITY
    };
    let wantAgent = await WantAgent.getWantAgent(wantAgentInfo);
  2. 执行特定操作:当用户点击通知后,系统会执行特定的操作,例如跳转到应用的某个特定页面或执行某个功能。可以通过设置WantAgentoperationTypeWantAgent.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对象传递给NotificationRequestwantAgent属性即可实现不同的点击事件处理。

在HarmonyOS鸿蒙Next中,创建状态栏通知的点击事件可以通过两种场景进行区分:

  1. 直接打开应用:通过设置PendingIntent,当用户点击通知时,直接启动应用的主界面或指定Activity。使用Intent指定目标Activity,并通过PendingIntent.getActivity()创建PendingIntent

  2. 执行特定操作:通过设置PendingIntent执行特定操作,如启动Service或发送Broadcast。使用Intent指定目标Service或BroadcastReceiver,并通过PendingIntent.getService()PendingIntent.getBroadcast()创建PendingIntent

根据需求选择合适的PendingIntent类型,确保点击通知后执行预期的操作。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!