鴻蒙OS NotificationHelper

2022-10-11 11:27 更新

NotificationHelper

java.lang.Object

|---ohos.event.notification.NotificationHelper

public class NotificationHelper
extends Object

管理通知。

該類提供的方法都是靜態(tài)的,無需創(chuàng)建該類的對象即可調(diào)用。

通常,只有系統(tǒng)應(yīng)用程序才具有通知訂閱和取消訂閱的權(quán)限。 您可以指定要發(fā)布的通知的內(nèi)容,該內(nèi)容由 NotificationRequest 攜帶。 通知ID在應(yīng)用程序中是唯一的,在使用NotificationRequest承載通知內(nèi)容時必須指定。 如果具有此 ID 的通知已發(fā)布,并且您需要使用此 ID 發(fā)布另一個通知,則將更新原始通知。 此外,通知 ID 可用于通過調(diào)用 cancelNotification(int) 方法取消通知。

Since:

1

方法總結(jié)

修飾符和類型 方法 描述
static void addNotificationSlot(NotificationSlot slot) 創(chuàng)建一個通知槽。
static void addNotificationSlotGroup(NotificationSlotGroup slotGroup) 創(chuàng)建一個通知槽組,通過調(diào)用 NotificationSlot#setSlotGroup(String) 可以將 NotificationSlot 對象綁定到該組。
static void addNotificationSlotGroups(ListNotificationSlotGroup slotGroups) 創(chuàng)建多個通知槽組。
static void addNotificationSlots(ListNotificationSlot slots) 創(chuàng)建多個通知槽。
static boolean areNotificationsSuspended() 檢查此應(yīng)用程序是否處于掛起狀態(tài)。
static void cancelAllNotifications() 取消所有已發(fā)布的通知。
static void cancelNotification(int notificationId) 取消已發(fā)布的通知。
static void cancelNotification(String label, int notificationId) 取消與指定標(biāo)簽和 notificationId 匹配的已發(fā)布通知。
static boolean canPublishNotificationAsBundle(String representativeBundle) 通過以給定代表包指示的另一個應(yīng)用程序的名稱調(diào)用 publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) 來檢查您的應(yīng)用程序是否有權(quán)發(fā)布通知。
static int getActiveNotificationNums() 獲取系統(tǒng)中當(dāng)前應(yīng)用的活動通知數(shù)。
static SetNotificationRequest getActiveNotifications() 獲取系統(tǒng)中當(dāng)前應(yīng)用程序的活動通知。
static int getBundleImportance() 獲取此應(yīng)用程序的重要性級別。
static NotificationSortingMap getCurrentAppSorting() 獲取當(dāng)前應(yīng)用的通知排序 map。
static String getNotificationAgent() 獲取可以以您的應(yīng)用程序名稱發(fā)布通知的應(yīng)用程序包的名稱。
static NotificationSlot getNotificationSlot(String slotId) 查詢已創(chuàng)建的通知槽。
static NotificationSlotGroup getNotificationSlotGroup(String slotGroupId) 獲取創(chuàng)建的通知槽組。
static ListNotificationSlotGroup getNotificationSlotGroups() 獲取創(chuàng)建的通知槽組列表。
static ListNotificationSlot getNotificationSlots() 獲取該應(yīng)用的所有通知槽。
static boolean hasNotificationPolicyAccessPermission() 檢查此應(yīng)用程序是否有權(quán)修改請勿打擾 (DND) 通知策略。
static boolean isAllowedNotify() 檢查此應(yīng)用程序是否有權(quán)發(fā)布通知。
static void publishNotification(String label, NotificationRequest request) 發(fā)布帶有指定標(biāo)簽的通知。
static void publishNotification(NotificationRequest request) 發(fā)布通知。
static void publishNotification(NotificationRequest request, String deviceId) 在指定的遠(yuǎn)程設(shè)備上發(fā)布通知。
static void publishNotificationAsBundle(String representativeBundle, NotificationRequest request) 以指定應(yīng)用程序包的名稱發(fā)布通知。
static void removeNotificationSlot(String slotId) 根據(jù)槽位 ID 刪除創(chuàng)建的通知槽位。
static void removeNotificationSlotGroup(String slotGroupId) 根據(jù)槽組 ID 刪除創(chuàng)建的通知槽組。
static void setNotificationAgent(String agent) 允許另一個應(yīng)用程序充當(dāng)代理以您的應(yīng)用程序包的名稱發(fā)布通知。
static void setNotificationBadgeNum() 將當(dāng)前應(yīng)用程序的活動通知數(shù)量設(shè)置為要在通知徽章上顯示的數(shù)量。
static void setNotificationBadgeNum(int num) 設(shè)置要在應(yīng)用程序的通知徽章上顯示的數(shù)字。
從類 java.lang.Object 繼承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

方法詳情

publishNotification

public static void publishNotification(NotificationRequest request) throws RemoteException

發(fā)布通知。

如果當(dāng)前應(yīng)用已經(jīng)發(fā)布了相同 ID 的通知并且沒有被刪除,則該方法會更新通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
request 表示用于設(shè)置通知內(nèi)容的 NotificationRequest 對象。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果請求為空,則拋出此異常。

Since:

1

publishNotification

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

發(fā)布帶有指定標(biāo)簽的通知。

如果當(dāng)前應(yīng)用已經(jīng)發(fā)布了相同 ID 的通知并且沒有被刪除,則該方法會更新通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
label 指示要發(fā)布的通知的標(biāo)簽。
request 表示用于設(shè)置通知內(nèi)容的 NotificationRequest 對象。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果請求為空,則拋出此異常。

Since:

1

publishNotification

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

在指定的遠(yuǎn)程設(shè)備上發(fā)布通知。

如果當(dāng)前應(yīng)用已經(jīng)發(fā)布了相同 ID 的通知并且沒有被刪除,則該方法會更新通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
request 表示用于設(shè)置通知內(nèi)容的 NotificationRequest 對象。 必須指定此參數(shù)。
deviceId 表示對端設(shè)備的ID。 如果此參數(shù)為空或空字符串,則通知將在本地設(shè)備上發(fā)布。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果請求為空,則拋出此異常。

Since:

3

cancelNotification

public static void cancelNotification(int notificationId) throws RemoteException

取消已發(fā)布的通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
notificationId 指示應(yīng)用程序中的唯一通知 ID。 該值必須是已發(fā)布通知的 ID。 否則,此方法不生效。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

1

cancelAllNotifications

public static void cancelAllNotifications() throws RemoteException

取消所有已發(fā)布的通知。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

1

addNotificationSlot

public static void addNotificationSlot(NotificationSlot slot) throws RemoteException

創(chuàng)建一個通知槽。 您可以調(diào)用 NotificationRequest#setSlotId(String) 方法來綁定用于發(fā)布的槽。

NotificationSlot 實(shí)例初始化后不能直接使用。 相反,您必須調(diào)用此方法來創(chuàng)建通知槽并將槽 ID 綁定到 NotificationRequest 對象,以便發(fā)布的通知可以具有 NotificationSlot 中設(shè)置的所有特征。

使用該方法創(chuàng)建通知槽后,只能更改通知槽的名稱和描述。 對其他屬性的更改,例如振動狀態(tài)和通知音,將不再生效。

參數(shù):

參數(shù)名稱 參數(shù)描述
slot 表示要創(chuàng)建的通知槽,由 NotificationSlot 設(shè)置。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果 slot 為空,則拋出此異常。

Since:

1

removeNotificationSlot

public static void removeNotificationSlot(String slotId) throws RemoteException

根據(jù)槽位 ID 刪除創(chuàng)建的通知槽位。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotId 槽的ID,由addNotificationSlot(ohos.event.notification.NotificationSlot)創(chuàng)建。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果 slotId 為 null 或空字符串,或者發(fā)生 IPC 異常,則引發(fā)此異常。

Since:

1

getNotificationSlot

public static NotificationSlot getNotificationSlot(String slotId) throws RemoteException

查詢已創(chuàng)建的通知槽。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotId 槽的ID,由addNotificationSlot(ohos.event.notification.NotificationSlot)創(chuàng)建。 必須指定此參數(shù)。

返回:

返回創(chuàng)建的 NotificationSlot。

Throws:

Throw名稱 Throw描述
RemoteException 如果 slotId 為 null 或空字符串,或者發(fā)生 IPC 異常,則引發(fā)此異常。

Since:

1

getActiveNotifications

public static SetNotificationRequest getActiveNotifications() throws RemoteException

獲取系統(tǒng)中當(dāng)前應(yīng)用程序的活動通知。

返回:

返回當(dāng)前應(yīng)用程序的活動 NotificationRequest 對象。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

1

getActiveNotificationNums

public static int getActiveNotificationNums() throws RemoteException

獲取系統(tǒng)中當(dāng)前應(yīng)用的活動通知數(shù)。

返回:

返回當(dāng)前應(yīng)用程序的活動通知數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

1

setNotificationBadgeNum

public static void setNotificationBadgeNum(int num) throws RemoteException

設(shè)置要在應(yīng)用程序的通知徽章上顯示的數(shù)字。

參數(shù):

參數(shù)名稱 參數(shù)描述
num 表示要顯示的數(shù)字。 負(fù)數(shù)表示徽章設(shè)置保持不變。 值 0 表示應(yīng)用程序圖標(biāo)上不顯示任何標(biāo)記。 如果值大于 99,將顯示 99+。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

setNotificationBadgeNum

public static void setNotificationBadgeNum() throws RemoteException

將當(dāng)前應(yīng)用程序的活動通知數(shù)量設(shè)置為要在通知徽章上顯示的數(shù)量。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

1

getCurrentAppSorting

public static NotificationSortingMap getCurrentAppSorting() throws RemoteException

獲取當(dāng)前應(yīng)用的通知排序地圖。

返回:

返回當(dāng)前應(yīng)用程序的 NotificationSortingMap 對象。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

2

addNotificationSlotGroup

public static void addNotificationSlotGroup(NotificationSlotGroup slotGroup) throws RemoteException

創(chuàng)建一個通知槽組,通過調(diào)用 NotificationSlot#setSlotGroup(String) 可以將 NotificationSlot 對象綁定到該組。

NotificationSlotGroup 實(shí)例初始化后不能直接使用。 相反,您必須調(diào)用此方法來創(chuàng)建通知槽組,以便您可以將 NotificationSlot 對象綁定到它。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotGroup 表示要創(chuàng)建的通知槽組,由 NotificationSlotGroup 設(shè)置。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果 slotGroup 為 null,則引發(fā)此異常。

Since:

3

addNotificationSlotGroups

public static void addNotificationSlotGroups(ListNotificationSlotGroup slotGroups) throws RemoteException

創(chuàng)建多個通知槽組。

使用該方法的注意事項(xiàng)與 addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup) 的注意事項(xiàng)類似。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotGroups 指示要創(chuàng)建的 NotificationSlotGroup 對象列表。 此參數(shù)不能為空。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果 slotGroups 為空,則引發(fā)此異常。

Since:

3

removeNotificationSlotGroup

public static void removeNotificationSlotGroup(String slotGroupId) throws RemoteException

根據(jù)槽組ID刪除創(chuàng)建的通知槽組。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotGroupId 通知槽組ID,由addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup)創(chuàng)建。 必須指定此參數(shù)。

Throws:

Throw名稱 Throw描述
RemoteException 如果 slotGroupId 為 null 或空字符串,或者發(fā)生 IPC 異常,則引發(fā)此異常。

Since:

3

getNotificationSlotGroup

public static NotificationSlotGroup getNotificationSlotGroup(String slotGroupId) throws RemoteException

獲取創(chuàng)建的通知槽組。

參數(shù):

參數(shù)名稱 參數(shù)描述
slotGroupId 通知槽組ID,由addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup)創(chuàng)建。 必須指定此參數(shù)。

返回:

返回創(chuàng)建的 NotificationSlotGroup。

Throws:

Throw名稱 Throw描述
RemoteException 如果 slotGroupId 為 null 或空字符串,或者發(fā)生 IPC 異常,則引發(fā)此異常。

Since:

3

hasNotificationPolicyAccessPermission

public static boolean hasNotificationPolicyAccessPermission() throws RemoteException

檢查此應(yīng)用程序是否有權(quán)修改請勿打擾 (DND) 通知策略。

返回:

如果此應(yīng)用程序具有權(quán)限,則返回 true; 否則返回 false。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

addNotificationSlots

public static void addNotificationSlots(ListNotificationSlot slots) throws RemoteException

創(chuàng)建多個通知槽。

參數(shù):

參數(shù)名稱 參數(shù)描述
slots 指示要創(chuàng)建的通知槽。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果 slot 為空,則引發(fā)此異常。

Since:

3

getNotificationSlots

public static ListNotificationSlot getNotificationSlots() throws RemoteException

獲取該應(yīng)用的所有通知槽。

返回:

返回此應(yīng)用程序的所有通知槽。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

publishNotificationAsBundle

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

以指定應(yīng)用程序包的名稱發(fā)布通知。 如果要發(fā)布的通知與未取消的已發(fā)布通知具有相同的 ID,則現(xiàn)有通知將被新通知替換。

參數(shù):

參數(shù)名稱 參數(shù)描述
representativeBundle 指示允許您的應(yīng)用程序通過調(diào)用 setNotificationAgent(java.lang.String) 為其發(fā)布通知的應(yīng)用程序包的名稱。
request 指示要發(fā)布的 NotificationRequest 對象。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果request 或representativeBundle 為null 或representativeBundle 為空字符串,則引發(fā)此異常。

Since:

3

setNotificationAgent

public static void setNotificationAgent(String agent) throws RemoteException

允許另一個應(yīng)用程序充當(dāng)代理以您的應(yīng)用程序包的名稱發(fā)布通知。 您可以通過將 null 傳遞給此方法來撤銷此授權(quán)。

參數(shù):

參數(shù)名稱 參數(shù)描述
agent 指示可以為您的應(yīng)用程序發(fā)布通知的應(yīng)用程序包的名稱。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

getNotificationAgent

public static String getNotificationAgent() throws RemoteException

獲取可以以您的應(yīng)用程序名稱發(fā)布通知的應(yīng)用程序包的名稱。

返回:

返回可以為您的應(yīng)用程序發(fā)布通知的應(yīng)用程序包的名稱(如果有); 否則返回 null。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

canPublishNotificationAsBundle

public static boolean canPublishNotificationAsBundle(String representativeBundle) throws RemoteException

通過以給定代表包指示的另一個應(yīng)用程序的名稱調(diào)用 publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) 來檢查您的應(yīng)用程序是否有權(quán)發(fā)布通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
representativeBundle 指示您的應(yīng)用程序所代表的應(yīng)用程序包的名稱。

返回:

如果您的應(yīng)用程序可以為給定的代表包發(fā)布通知,則返回 true; 否則返回 false。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。
IllegalArgumentException 如果代表捆綁為空,則引發(fā)此異常。

Since:

3

getNotificationSlotGroups

public static ListNotificationSlotGroup getNotificationSlotGroups() throws RemoteException

獲取創(chuàng)建的通知槽組列表。

返回:

返回已創(chuàng)建通知槽組的列表。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

isAllowedNotify

public static boolean isAllowedNotify() throws RemoteException

檢查此應(yīng)用程序是否有權(quán)發(fā)布通知。

返回:

如果此應(yīng)用程序具有權(quán)限,則返回 true; 否則返回 false。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

areNotificationsSuspended

public static boolean areNotificationsSuspended() throws RemoteException

檢查此應(yīng)用程序是否處于掛起狀態(tài)。 此狀態(tài)下的應(yīng)用程序無法發(fā)布通知。

返回:

如果此應(yīng)用程序被掛起,則返回 true; 否則返回 false。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

getBundleImportance

public static int getBundleImportance() throws RemoteException

獲取此應(yīng)用程序的重要性級別。

返回:

返回此應(yīng)用程序的重要性級別,可以是 NotificationSlot#LEVEL_NONE、NotificationSlot#LEVEL_MIN、NotificationSlot#LEVEL_LOW、NotificationSlot#LEVEL_DEFAULT、NotificationSlot#LEVEL_HIGH 或 NotificationSlot#LEVEL_UNDEFINED。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

cancelNotification

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

取消與指定標(biāo)簽和 notificationId 匹配的已發(fā)布通知。

參數(shù):

參數(shù)名稱 參數(shù)描述
label 指示要取消的通知的標(biāo)簽。
notificationId 指示要取消的通知的 ID。

Throws:

Throw名稱 Throw描述
RemoteException 如果發(fā)生 IPC 異常,則拋出此異常。

Since:

3

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號