Candy咱看雪去吧鸿蒙通知Notification如何自定义view
sun开发者你好,你还需要在layout的布局文件中加上ohos:remote="true"的属性。
附上我调试的代码供参考使用:
---MainAbilitySlice
public class MainAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL = new HiLogLabel(3, 0xD000F00, "MailDemo");
private static final String ONGOING_CARD_SLOT_ID = "test_slot";
private static final String ONGOING_CARD_NAME = "test_card";
private static final String ONGOING_CARD_SLOT_DESCRIPTION = "test_description";
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
NotificationSlot slot = new NotificationSlot(ONGOING_CARD_SLOT_ID, ONGOING_CARD_NAME, NotificationSlot.LEVEL_DEFAULT);
slot.setDescription(ONGOING_CARD_SLOT_DESCRIPTION);
try {
NotificationHelper.addNotificationSlot(slot);
} catch (RemoteException ex) {
HiLog.error(LABEL, "Add ongoing card slot exception");
}
int notificationId = 1;
NotificationRequest request = new NotificationRequest(this, notificationId);
request.setSlotId(slot.getId());
String title = "title";
String text = "There is an ongoing notification content.";
NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
content.setTitle(title).setText(text);
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
request.setContent(notificationContent);
ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_ability_main, this); // 创建ComponentProvider对象
componentProvider.setString(ResourceTable.Id_text_helloworld, "setText", "TextContent"); // 设置布局中的文本内容
request.setCustomView(componentProvider);
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException ex) {
HiLog.error(LABEL, "Exception occurred during publishNotification invocation.");
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
---ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:alignment="center"
ohos:height="200px"
ohos:orientation="vertical"
ohos:remote="true"
ohos:width="match_parent">
<Text
ohos:background_element="#8888"
ohos:height="match_content"
ohos:id="$+id:text_helloworld"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:mainability_HelloWorld"
ohos:text_size="16vp"
ohos:width="match_content" />
</DirectionalLayout>
sun开发者你好,你可以参考官方指导文档中的内容,尝试一下哦
Candy咱看雪去吧试了,但还是没有效果,没有通知,代码基本就是搬的文档里面的:
收起自动换行深色代码主题复制NotificationSlot slot = new NotificationSlot("1", "nihao", NotificationSlot.LEVEL_HIGH); slot.setDescription("ONGOING_CARD_SLOT_DESCRIPTION"); try { NotificationHelper.addNotificationSlot(slot); } catch (RemoteException ex) { ex.printStackTrace(); } NotificationRequest request = new NotificationRequest(getContext(), 1); request.setSlotId(slot.getId()); String title = "title"; String text = "There is an ongoing notification content."; NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent(); content.setTitle(title).setText(text); NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content); request.setContent(notificationContent); ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_custom_pic, getContext()); // 创建ComponentProvider对象 componentProvider.setString(ResourceTable.Id_ongoing_card_text, "setText", "TextContent"); // 设置布局中的文本内容 request.setCustomView(componentProvider);2021-07-28 02:55 未知归属地
五维空间幻影猫- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
HarmonyOS 如何在非自定义组件中获取当前屏幕顶部显示的组件的UniqueId (API12+)
HarmonyOS 组件view可以转换成image吗? (API12+)
HarmonyOS 自定义键盘与底部安全区重合(API12+)
自定义弹窗保存文档后内容丢失如何解决
HarmonyOS builder自定义构建函数中无法实现hover效果 (API12+)
如何在HarmonyOS中为自定义View进行手势监听?(API12+)
HarmonyOS openCustomDialog如何使用全局Builder构建自定义弹窗?(API12+)
HarmonyOS Refresh组件下拉刷新自定义区域闪动的案例分享(API12+)




































苏公安网备 32011402010933号