如何创建带按钮通知?

如何创建带按钮通知

阅读 415
1 个回答

解决措施

可通过设置NotificationRequest中actionButtons设置通知按钮,最多可设置三个按钮。

示例代码

import { BusinessError } from '@kit.BasicServicesKit'; 
import { notificationManager } from '@kit.NotificationKit'; 
import { wantAgent , WantAgent } from '@kit.AbilityKit'; 
 
//publish回调 
let publishCallback = (err: BusinessError): void => { 
  if (err) { 
    console.error(`publish failed, code is ${err.code}, message is ${err.message}`); 
  } else { 
    console.info("publish success"); 
  } 
} 
let wantAgentObj:WantAgent; // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。 
 
// 通过WantAgentInfo的operationType设置动作类型 
let wantAgentInfo:wantAgent.WantAgentInfo = { 
  wants: [ 
    { 
      action: 'event_name', // 设置事件名 
      parameters: {}, 
    } 
  ], 
  operationType: wantAgent.OperationType.SEND_COMMON_EVENT, 
  requestCode: 0, 
  wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 
}; 
//通知Request对象 
let notificationRequest: notificationManager.NotificationRequest = { 
  id: 1, 
  content: { 
    contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 
    normal: { 
      title: "test_title", 
      text: "test_text", 
      additionalText: "test_additionalText" 
    } 
  }, 
  actionButtons: [ 
    { 
      title: '回复', 
      wantAgent: wantAgentObj 
    } 
  ] 
}; 
notificationManager.publish(notificationRequest, publishCallback);

参考链接

自定义通知

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进