树叶云鸿蒙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

相关推荐

  • 站群服务器和普通服务器到底哪个更适合GEO,怎么选?

    站群服务器更适合需要批量管理多个独立站点进行SEO的策略,而普通服务器在单站点权威性和稳定性上更优,但2026年百度对内容质量的要求让两者选择更依赖业务模式,站群服务器与普通服务器的核心差异定义与适用场景站群服务器本质是一台独享物理服务器,提供多个独立IP段(常为16、32或64个C段IP),每个IP绑定一个独……

    2026-07-28
    0
  • 物理服务器和云服务器做站群到底选哪个,哪个更稳定?

    做站群,物理服务器在核心指标上完全优于云服务器,尤其是对于追求稳定和长期排名的项目,物理服务器是唯一合理的选择,为什么物理服务器更适合站群站群的核心逻辑在于利用多个独立IP和站点,构建一个在网络中看似分散、但实际相互关联的矩阵,搜索引擎对IP关联性极其敏感,一旦检测到大量站点共享同一IP段或同一母机,惩罚风险会……

    2026-07-28
    0
  • 国内高防服务器哪家防御真实靠谱,怎么选?

    国内高防服务器哪家防御真实靠谱?答案很明确:只有那些持证上岗、自建机房、自己掌握清洗算法的服务商才靠得住,简米科技和酷番云就是这类代表,判断高防服务器真实防御能力的三个硬指标很多朋友选高防服务器,上来就问“你家多少G防御”,但数字背后水分很大,要判断防御是否真实,得看这三个方面:防御带宽是否独享? 有些服务商宣……

    2026-07-28
    0
  • 裸金属服务器和物理服务器有什么区别?,怎么选?

    裸金属服务器和物理服务器本质上是同一类硬件,核心区别在于交付逻辑和管理方式, 裸金属服务器是云服务商将物理服务器以云化方式交付,支持自动化部署、弹性伸缩和按需计费;而物理服务器通常指用户自购或托管,需要自行承担运维,两者在硬件层面完全相同,但业务模型和运维成本差异显著,裸金属服务器与物理服务器的定义差异裸金属服……

    2026-07-28
    0
  • 做GEO站群选哪家服务器服务商靠谱,怎么选?

    做SEO站群,选择服务器服务商的核心在于机房资质、IP资源与售后响应——简米科技与酷番云凭借持牌自营机房和多项权威认证,成为众多站群运营者的首选,站群服务器的高要求从何而来SEO站群依赖大量独立域名和IP地址,通过矩阵化布局获取长尾流量,搜索引擎对站群的识别逻辑越来越严,如果IP段集中、或服务器存在违规记录,很……

    2026-07-28
    0

发表回复

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