树叶云鸿蒙OS教程:鸿蒙OS NotificationHelper

NotificationHelper

java.lang.Object

|—ohos.event.notification.NotificationHelper

public class NotificationHelper
extends Object

管理通知。

该类提供的方法都是静态的,无需创建该类的对象即可调用。

通常,只有系统应用程序才具有通知订阅和取消订阅的权限。 您可以指定要发布的通知的内容,该内容由 NotificationRequest 携带。 通知ID在应用程序中是唯一的,在使用NotificationRequest承载通知内容时必须指定。 如果具有此 ID 的通知已发布,并且您需要使用此 ID 发布另一个通知,则将更新原始通知。 此外,通知 ID 可用于通过调用 cancelNotification(int) 方法取消通知。

Since:

1

方法总结

修饰符和类型方法描述
static voidaddNotificationSlot(NotificationSlot slot)创建一个通知槽。
static voidaddNotificationSlotGroup(NotificationSlotGroup slotGroup)创建一个通知槽组,通过调用 NotificationSlot#setSlotGroup(String) 可以将 NotificationSlot 对象绑定到该组。
static voidaddNotificationSlotGroups(ListNotificationSlotGroup slotGroups)创建多个通知槽组。
static voidaddNotificationSlots(ListNotificationSlot slots)创建多个通知槽。
static booleanareNotificationsSuspended()检查此应用程序是否处于挂起状态。
static voidcancelAllNotifications()取消所有已发布的通知。
static voidcancelNotification(int notificationId)取消已发布的通知。
static voidcancelNotification(String label, int notificationId)取消与指定标签和 notificationId 匹配的已发布通知。
static booleancanPublishNotificationAsBundle(String representativeBundle)通过以给定代表包指示的另一个应用程序的名称调用 publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) 来检查您的应用程序是否有权发布通知。
static intgetActiveNotificationNums()获取系统中当前应用的活动通知数。
static SetNotificationRequestgetActiveNotifications()获取系统中当前应用程序的活动通知。
static intgetBundleImportance()获取此应用程序的重要性级别。
static NotificationSortingMapgetCurrentAppSorting()获取当前应用的通知排序 map。
static StringgetNotificationAgent()获取可以以您的应用程序名称发布通知的应用程序包的名称。
static NotificationSlotgetNotificationSlot(String slotId)查询已创建的通知槽。
static NotificationSlotGroupgetNotificationSlotGroup(String slotGroupId)获取创建的通知槽组。
static ListNotificationSlotGroupgetNotificationSlotGroups()获取创建的通知槽组列表。
static ListNotificationSlotgetNotificationSlots()获取该应用的所有通知槽。
static booleanhasNotificationPolicyAccessPermission()检查此应用程序是否有权修改请勿打扰 (DND) 通知策略。
static booleanisAllowedNotify()检查此应用程序是否有权发布通知。
static voidpublishNotification(String label, NotificationRequest request)发布带有指定标签的通知。
static voidpublishNotification(NotificationRequest request)发布通知。
static voidpublishNotification(NotificationRequest request, String deviceId)在指定的远程设备上发布通知。
static voidpublishNotificationAsBundle(String representativeBundle, NotificationRequest request)以指定应用程序包的名称发布通知。
static voidremoveNotificationSlot(String slotId)根据槽位 ID 删除创建的通知槽位。
static voidremoveNotificationSlotGroup(String slotGroupId)根据槽组 ID 删除创建的通知槽组。
static voidsetNotificationAgent(String agent)允许另一个应用程序充当代理以您的应用程序包的名称发布通知。
static voidsetNotificationBadgeNum()将当前应用程序的活动通知数量设置为要在通知徽章上显示的数量。
static voidsetNotificationBadgeNum(int num)设置要在应用程序的通知徽章上显示的数字。
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

方法详情

publishNotification

public static void publishNotification(NotificationRequest request) throws RemoteException

发布通知。

如果当前应用已经发布了相同 ID 的通知并且没有被删除,则该方法会更新通知。

参数:

参数名称参数描述
request表示用于设置通知内容的 NotificationRequest 对象。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果请求为空,则抛出此异常。

Since:

1

publishNotification

public static void publishNotification(String label, NotificationRequest request) throws RemoteException

发布带有指定标签的通知。

如果当前应用已经发布了相同 ID 的通知并且没有被删除,则该方法会更新通知。

参数:

参数名称参数描述
label指示要发布的通知的标签。
request表示用于设置通知内容的 NotificationRequest 对象。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果请求为空,则抛出此异常。

Since:

1

publishNotification

public static void publishNotification(NotificationRequest request, String deviceId) throws RemoteException

在指定的远程设备上发布通知。

如果当前应用已经发布了相同 ID 的通知并且没有被删除,则该方法会更新通知。

参数:

参数名称参数描述
request表示用于设置通知内容的 NotificationRequest 对象。 必须指定此参数。
deviceId表示对端设备的ID。 如果此参数为空或空字符串,则通知将在本地设备上发布。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果请求为空,则抛出此异常。

Since:

3

cancelNotification

public static void cancelNotification(int notificationId) throws RemoteException

取消已发布的通知。

参数:

参数名称参数描述
notificationId指示应用程序中的唯一通知 ID。 该值必须是已发布通知的 ID。 否则,此方法不生效。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

1

cancelAllNotifications

public static void cancelAllNotifications() throws RemoteException

取消所有已发布的通知。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

1

addNotificationSlot

public static void addNotificationSlot(NotificationSlot slot) throws RemoteException

创建一个通知槽。 您可以调用 NotificationRequest#setSlotId(String) 方法来绑定用于发布的槽。

NotificationSlot 实例初始化后不能直接使用。 相反,您必须调用此方法来创建通知槽并将槽 ID 绑定到 NotificationRequest 对象,以便发布的通知可以具有 NotificationSlot 中设置的所有特征。

使用该方法创建通知槽后,只能更改通知槽的名称和描述。 对其他属性的更改,例如振动状态和通知音,将不再生效。

参数:

参数名称参数描述
slot表示要创建的通知槽,由 NotificationSlot 设置。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果 slot 为空,则抛出此异常。

Since:

1

removeNotificationSlot

public static void removeNotificationSlot(String slotId) throws RemoteException

根据槽位 ID 删除创建的通知槽位。

参数:

参数名称参数描述
slotId槽的ID,由addNotificationSlot(ohos.event.notification.NotificationSlot)创建。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果 slotId 为 null 或空字符串,或者发生 IPC 异常,则引发此异常。

Since:

1

getNotificationSlot

public static NotificationSlot getNotificationSlot(String slotId) throws RemoteException

查询已创建的通知槽。

参数:

参数名称参数描述
slotId槽的ID,由addNotificationSlot(ohos.event.notification.NotificationSlot)创建。 必须指定此参数。

返回:

返回创建的 NotificationSlot。

Throws:

Throw名称Throw描述
RemoteException如果 slotId 为 null 或空字符串,或者发生 IPC 异常,则引发此异常。

Since:

1

getActiveNotifications

public static SetNotificationRequest getActiveNotifications() throws RemoteException

获取系统中当前应用程序的活动通知。

返回:

返回当前应用程序的活动 NotificationRequest 对象。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

1

getActiveNotificationNums

public static int getActiveNotificationNums() throws RemoteException

获取系统中当前应用的活动通知数。

返回:

返回当前应用程序的活动通知数。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

1

setNotificationBadgeNum

public static void setNotificationBadgeNum(int num) throws RemoteException

设置要在应用程序的通知徽章上显示的数字。

参数:

参数名称参数描述
num表示要显示的数字。 负数表示徽章设置保持不变。 值 0 表示应用程序图标上不显示任何标记。 如果值大于 99,将显示 99+。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

setNotificationBadgeNum

public static void setNotificationBadgeNum() throws RemoteException

将当前应用程序的活动通知数量设置为要在通知徽章上显示的数量。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

1

getCurrentAppSorting

public static NotificationSortingMap getCurrentAppSorting() throws RemoteException

获取当前应用的通知排序地图。

返回:

返回当前应用程序的 NotificationSortingMap 对象。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

2

addNotificationSlotGroup

public static void addNotificationSlotGroup(NotificationSlotGroup slotGroup) throws RemoteException

创建一个通知槽组,通过调用 NotificationSlot#setSlotGroup(String) 可以将 NotificationSlot 对象绑定到该组。

NotificationSlotGroup 实例初始化后不能直接使用。 相反,您必须调用此方法来创建通知槽组,以便您可以将 NotificationSlot 对象绑定到它。

参数:

参数名称参数描述
slotGroup表示要创建的通知槽组,由 NotificationSlotGroup 设置。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果 slotGroup 为 null,则引发此异常。

Since:

3

addNotificationSlotGroups

public static void addNotificationSlotGroups(ListNotificationSlotGroup slotGroups) throws RemoteException

创建多个通知槽组。

使用该方法的注意事项与 addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup) 的注意事项类似。

参数:

参数名称参数描述
slotGroups指示要创建的 NotificationSlotGroup 对象列表。 此参数不能为空。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果 slotGroups 为空,则引发此异常。

Since:

3

removeNotificationSlotGroup

public static void removeNotificationSlotGroup(String slotGroupId) throws RemoteException

根据槽组ID删除创建的通知槽组。

参数:

参数名称参数描述
slotGroupId通知槽组ID,由addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup)创建。 必须指定此参数。

Throws:

Throw名称Throw描述
RemoteException如果 slotGroupId 为 null 或空字符串,或者发生 IPC 异常,则引发此异常。

Since:

3

getNotificationSlotGroup

public static NotificationSlotGroup getNotificationSlotGroup(String slotGroupId) throws RemoteException

获取创建的通知槽组。

参数:

参数名称参数描述
slotGroupId通知槽组ID,由addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup)创建。 必须指定此参数。

返回:

返回创建的 NotificationSlotGroup。

Throws:

Throw名称Throw描述
RemoteException如果 slotGroupId 为 null 或空字符串,或者发生 IPC 异常,则引发此异常。

Since:

3

hasNotificationPolicyAccessPermission

public static boolean hasNotificationPolicyAccessPermission() throws RemoteException

检查此应用程序是否有权修改请勿打扰 (DND) 通知策略。

返回:

如果此应用程序具有权限,则返回 true; 否则返回 false。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

addNotificationSlots

public static void addNotificationSlots(ListNotificationSlot slots) throws RemoteException

创建多个通知槽。

参数:

参数名称参数描述
slots指示要创建的通知槽。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果 slot 为空,则引发此异常。

Since:

3

getNotificationSlots

public static ListNotificationSlot getNotificationSlots() throws RemoteException

获取该应用的所有通知槽。

返回:

返回此应用程序的所有通知槽。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

publishNotificationAsBundle

public static void publishNotificationAsBundle(String representativeBundle, NotificationRequest request) throws RemoteException

以指定应用程序包的名称发布通知。 如果要发布的通知与未取消的已发布通知具有相同的 ID,则现有通知将被新通知替换。

参数:

参数名称参数描述
representativeBundle指示允许您的应用程序通过调用 setNotificationAgent(java.lang.String) 为其发布通知的应用程序包的名称。
request指示要发布的 NotificationRequest 对象。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果request 或representativeBundle 为null 或representativeBundle 为空字符串,则引发此异常。

Since:

3

setNotificationAgent

public static void setNotificationAgent(String agent) throws RemoteException

允许另一个应用程序充当代理以您的应用程序包的名称发布通知。 您可以通过将 null 传递给此方法来撤销此授权。

参数:

参数名称参数描述
agent指示可以为您的应用程序发布通知的应用程序包的名称。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

getNotificationAgent

public static String getNotificationAgent() throws RemoteException

获取可以以您的应用程序名称发布通知的应用程序包的名称。

返回:

返回可以为您的应用程序发布通知的应用程序包的名称(如果有); 否则返回 null。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

canPublishNotificationAsBundle

public static boolean canPublishNotificationAsBundle(String representativeBundle) throws RemoteException

通过以给定代表包指示的另一个应用程序的名称调用 publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) 来检查您的应用程序是否有权发布通知。

参数:

参数名称参数描述
representativeBundle指示您的应用程序所代表的应用程序包的名称。

返回:

如果您的应用程序可以为给定的代表包发布通知,则返回 true; 否则返回 false。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。
IllegalArgumentException如果代表捆绑为空,则引发此异常。

Since:

3

getNotificationSlotGroups

public static ListNotificationSlotGroup getNotificationSlotGroups() throws RemoteException

获取创建的通知槽组列表。

返回:

返回已创建通知槽组的列表。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

isAllowedNotify

public static boolean isAllowedNotify() throws RemoteException

检查此应用程序是否有权发布通知。

返回:

如果此应用程序具有权限,则返回 true; 否则返回 false。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

areNotificationsSuspended

public static boolean areNotificationsSuspended() throws RemoteException

检查此应用程序是否处于挂起状态。 此状态下的应用程序无法发布通知。

返回:

如果此应用程序被挂起,则返回 true; 否则返回 false。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

getBundleImportance

public static int getBundleImportance() throws RemoteException

获取此应用程序的重要性级别。

返回:

返回此应用程序的重要性级别,可以是 NotificationSlot#LEVEL_NONE、NotificationSlot#LEVEL_MIN、NotificationSlot#LEVEL_LOW、NotificationSlot#LEVEL_DEFAULT、NotificationSlot#LEVEL_HIGH 或 NotificationSlot#LEVEL_UNDEFINED。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

cancelNotification

public static void cancelNotification(String label, int notificationId) throws RemoteException

取消与指定标签和 notificationId 匹配的已发布通知。

参数:

参数名称参数描述
label指示要取消的通知的标签。
notificationId指示要取消的通知的 ID。

Throws:

Throw名称Throw描述
RemoteException如果发生 IPC 异常,则抛出此异常。

Since:

3

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/173036.html<

(0)
运维的头像运维
上一篇2025-03-23 14:37
下一篇 2025-03-23 14:39

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注