public class NotificationHelper extends Object
The methods provided by this class are all static and can be called without creating an object of this class.
Generally, only system applications have permissions on notification subscription and unsubscription.
You can specify the content of a notification to be published and the content is carried by
NotificationRequest. A notification ID is unique in an application and must be specified
when using NotificationRequest to carry the notification content. If a notification
with this ID has been published and you need to use this ID to publish another notification,
the original notification will be updated. In addition, the notification ID can be used to cancel
a notification by calling the cancelNotification(int) method.
NotificationRequestSystemCapability.Notification#ANS| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static void |
addNotificationSlot(NotificationSlot slot) |
Creates a notification slot.
|
static void |
addNotificationSlotGroup(NotificationSlotGroup slotGroup) |
Creates a notification slot group to which a
NotificationSlot object can be bound by calling
NotificationSlot#setSlotGroup(String). |
static void |
addNotificationSlotGroups(List<NotificationSlotGroup> slotGroups) |
Creates multiple notification slot groups.
|
static void |
addNotificationSlots(List<NotificationSlot> slots) |
Creates multiple notification slots.
|
static boolean |
areNotificationsSuspended() |
Checks whether this application is in the suspended state.
|
static void |
cancelAllNotifications() |
Cancels all the published notifications.
|
static void |
cancelNotification(int notificationId) |
Cancels a published notification.
|
static void |
cancelNotification(String label,
int notificationId) |
Cancels a published notification matching the specified
label and notificationId. |
static boolean |
canPublishNotificationAsBundle(String representativeBundle) |
Checks whether your application has permission to publish notifications by calling
publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) in the name of another application indicated
by the given representativeBundle. |
static int |
getActiveNotificationNums() |
Obtains the number of active notifications of the current application in the system.
|
static Set<NotificationRequest> |
getActiveNotifications() |
Obtains active notifications of the current application in the system.
|
static int |
getBundleImportance() |
Obtains the importance level of this application.
|
static NotificationSortingMap |
getCurrentAppSorting() |
Obtains the map for sorting notifications of the current application.
|
static String |
getNotificationAgent() |
Obtains the name of the application bundle that can publish notifications in the name of your application.
|
static NotificationSlot |
getNotificationSlot(String slotId) |
Queries a created notification slot.
|
static NotificationSlotGroup |
getNotificationSlotGroup(String slotGroupId) |
Obtains a created notification slot group.
|
static List<NotificationSlotGroup> |
getNotificationSlotGroups() |
Obtains a list of created notification slot groups.
|
static List<NotificationSlot> |
getNotificationSlots() |
Obtains all notification slots of this application.
|
static boolean |
hasNotificationPolicyAccessPermission() |
Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy.
|
static boolean |
isAllowedNotify() |
Checks whether this application has permission to publish notifications.
|
static void |
publishNotification(String label,
NotificationRequest request) |
Publishes a notification with a specified label.
|
static void |
publishNotification(NotificationRequest request) |
Publishes a notification.
|
static void |
publishNotification(NotificationRequest request,
String deviceId) |
Publishes a notification on a specified remote device.
|
static void |
publishNotificationAsBundle(String representativeBundle,
NotificationRequest request) |
Publishes a notification in the name of a specified application bundle.
|
static void |
removeNotificationSlot(String slotId) |
Deletes a created notification slot based on the slot ID.
|
static void |
removeNotificationSlotGroup(String slotGroupId) |
Deletes a created notification slot group based on the slot group ID.
|
static void |
setNotificationAgent(String agent) |
Allows another application to act as an agent to publish notifications in the name of your application bundle.
|
static void |
setNotificationBadgeNum() |
Sets the number of active notifications of the current application as the number to be displayed
on the notification badge.
|
static void |
setNotificationBadgeNum(int num) |
Sets the number to be displayed on the notification badge of the application.
|
public static void publishNotification(NotificationRequest request) throws RemoteException
If a notification with the same ID has been published by the current application and has not been deleted, this method will update the notification.
request - Indicates the NotificationRequest object for setting the notification content.
This parameter must be specified.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if request is null.public static void publishNotification(String label, NotificationRequest request) throws RemoteException
If a notification with the same ID has been published by the current application and has not been deleted, this method will update the notification.
label - Indicates the label of the notification to publish.request - Indicates the NotificationRequest object for setting the notification content.
This parameter must be specified.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if request is null.public static void publishNotification(NotificationRequest request, String deviceId) throws RemoteException
If a notification with the same ID has been published by the current application and has not been deleted, this method will update the notification.
request - Indicates the NotificationRequest object for setting the notification content.
This parameter must be specified.deviceId - Indicates the ID of the remote device. If this parameter is null or an empty string,
the notification will be published on the local device.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if request is null.public static void cancelNotification(int notificationId)
throws RemoteException
notificationId - Indicates the unique notification ID in the application. The value must be the ID
of a published notification. Otherwise, this method does not take effect.RemoteException - Throws this exception if an IPC exception occurs.public static void cancelAllNotifications()
throws RemoteException
cancelNotification(int).RemoteException - Throws this exception if an IPC exception occurs.public static void addNotificationSlot(NotificationSlot slot) throws RemoteException
NotificationRequest#setSlotId(String) method
to bind the slot for publishing.
A NotificationSlot instance cannot be used directly after being initialized. Instead,
you have to call this method to create a notification slot and bind the slot ID to
a NotificationRequest object so that the notification published can have
all the characteristics set in the NotificationSlot.
After a notification slot is created by using this method, only the name and description of the notification slot can be changed. Changes to the other attributes, such as the vibration status and notification tone, will no longer take effect.
slot - Indicates the notification slot to be created, which is set by NotificationSlot.
This parameter must be specified.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if slot is null.public static void removeNotificationSlot(String slotId) throws RemoteException
slotId - Indicates the ID of the slot, which is created by
addNotificationSlot(ohos.event.notification.NotificationSlot). This parameter must be specified.RemoteException - Throws this exception if slotId is null or an empty string,
or if an IPC exception occurs.public static NotificationSlot getNotificationSlot(String slotId) throws RemoteException
slotId - Indicates the ID of the slot, which is created by
addNotificationSlot(ohos.event.notification.NotificationSlot). This parameter must be specified.NotificationSlot.RemoteException - Throws this exception if slotId is null or an empty string,
or if an IPC exception occurs.public static Set<NotificationRequest> getActiveNotifications() throws RemoteException
NotificationRequest objects of the current application.RemoteException - Throws this exception if an IPC exception occurs.public static int getActiveNotificationNums()
throws RemoteException
RemoteException - Throws this exception if an IPC exception occurs.public static void setNotificationBadgeNum(int num)
throws RemoteException
num - Indicates the number to display. A negative number indicates that the badge setting
remains unchanged. The value 0 indicates that no badge is displayed on the application icon.
If the value is greater than 99, 99+ will be displayed.RemoteException - Throws this exception if an IPC exception occurs.public static void setNotificationBadgeNum()
throws RemoteException
RemoteException - Throws this exception if an IPC exception occurs.public static NotificationSortingMap getCurrentAppSorting() throws RemoteException
NotificationSortingMap object for the current application.RemoteException - Throws this exception if an IPC exception occurs.public static void addNotificationSlotGroup(NotificationSlotGroup slotGroup) throws RemoteException
NotificationSlot object can be bound by calling
NotificationSlot#setSlotGroup(String).
A NotificationSlotGroup instance cannot be used directly after being initialized. Instead, you
have to call this method to create a notification slot group so that you can bind NotificationSlot
objects to it.
slotGroup - Indicates the notification slot group to be created, which is set by
NotificationSlotGroup. This parameter must be specified.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if slotGroup is null.public static void addNotificationSlotGroups(List<NotificationSlotGroup> slotGroups) throws RemoteException
The precautions for using this method are similar to those for
addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup).
slotGroups - Indicates a list of NotificationSlotGroup objects to create.
This parameter cannot be null.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if slotGroups is null.public static void removeNotificationSlotGroup(String slotGroupId) throws RemoteException
slotGroupId - Indicates the ID of the notification slot group, which is created by
addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup). This parameter must be specified.RemoteException - Throws this exception if slotGroupId is null or an empty string,
or if an IPC exception occurs.public static NotificationSlotGroup getNotificationSlotGroup(String slotGroupId) throws RemoteException
slotGroupId - Indicates the ID of the notification slot group, which is created by
addNotificationSlotGroup(ohos.event.notification.NotificationSlotGroup). This parameter must be specified.NotificationSlotGroup.RemoteException - Throws this exception if slotGroupId is null or an empty string,
or if an IPC exception occurs.public static boolean hasNotificationPolicyAccessPermission()
throws RemoteException
true if this application has the permission; returns false otherwise.RemoteException - Throws this exception if an IPC exception occurs.public static void addNotificationSlots(List<NotificationSlot> slots) throws RemoteException
slots - Indicates the notification slots to create.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if slots is null.public static List<NotificationSlot> getNotificationSlots() throws RemoteException
RemoteException - Throws this exception if an IPC exception occurs.public static void publishNotificationAsBundle(String representativeBundle, NotificationRequest request) throws RemoteException
representativeBundle - Indicates the name of the application bundle that allows your application
to publish notifications for it by calling setNotificationAgent(java.lang.String).request - Indicates the NotificationRequest object to be published.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if request or representativeBundle
is null or if representativeBundle is an empty string.public static void setNotificationAgent(String agent) throws RemoteException
agent - Indicates the name of the application bundle that can publish notifications for your application.RemoteException - Throws this exception if an IPC exception occurs.public static String getNotificationAgent() throws RemoteException
null otherwise.RemoteException - Throws this exception if an IPC exception occurs.public static boolean canPublishNotificationAsBundle(String representativeBundle) throws RemoteException
publishNotificationAsBundle(java.lang.String,ohos.event.notification.NotificationRequest) in the name of another application indicated
by the given representativeBundle.representativeBundle - Indicates the name of application bundle your application is representing.true if your application can publish notifications for
the given representativeBundle; returns false otherwise.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if representativeBundle is null.public static List<NotificationSlotGroup> getNotificationSlotGroups() throws RemoteException
RemoteException - Throws this exception if an IPC exception occurs.public static boolean isAllowedNotify()
throws RemoteException
true if this application has the permission; returns false otherwise.RemoteException - Throws this exception if an IPC exception occurs.public static boolean areNotificationsSuspended()
throws RemoteException
true if this application is suspended; returns false otherwise.RemoteException - Throws this exception if an IPC exception occurs.public static int getBundleImportance()
throws RemoteException
NotificationSlot#LEVEL_NONE, NotificationSlot#LEVEL_MIN,
NotificationSlot#LEVEL_LOW, NotificationSlot#LEVEL_DEFAULT,
NotificationSlot#LEVEL_HIGH, or NotificationSlot#LEVEL_UNDEFINED.RemoteException - Throws this exception if an IPC exception occurs.public static void cancelNotification(String label, int notificationId) throws RemoteException
label and notificationId.label - Indicates the label of the notification to cancel.notificationId - Indicates the ID of the notification to cancel.RemoteException - Throws this exception if an IPC exception occurs.