Newer Version Available
CustomNotification Class
Namespace
Usage
- Create an instance with the default constructor, and then set notification attributes using the various setter methods.
- Create an instance and configure notification parameters at the same time using the parameterized constructor.
Notification Target
The notification target is used by the receiving client application to navigate to an appropriate record or page when a user responds to a notification. For example, when a user is notified that a record was updated, responding to the notification can open the relevant record.
You must specify a target for a notification. The target can be specified using either the targetID or the targetPageRef attribute. Neither attribute is required, but if both are omitted, send() throws an exception. If there’s no natural target for a notification, set the targetID to a dummy value, such as 000000000000000AAA. A dummy value prevents the exception, and also prevents automatic navigation when responding to the notification in the client app.
Execution Context and Notification Permissions
By default Apex code executes in system mode, and doesn’t require user permissions to send notifications with CustomNotification. However, if your Apex code runs in a user context—for example, by executing anonymous Apex in the Developer Console—the Send Custom Notifications user permission is checked, and send() fails if you don’t have the required permission.
Example
This example Apex class provides a static method for sending a custom notification to a recipient list. Call this method from a trigger, flow, or wherever you want to send a custom notification from Apex.
1public without sharing class CustomNotificationFromApex {
2
3 public static void notifyUsers(Set<String> recipientsIds, String targetId) {
4
5 // Get the Id for our custom notification type
6 CustomNotificationType notificationType =
7 [SELECT Id, DeveloperName
8 FROM CustomNotificationType
9 WHERE DeveloperName='Custom_Notification'];
10
11 // Create a new custom notification
12 Messaging.CustomNotification notification = new Messaging.CustomNotification();
13
14 // Set the contents for the notification
15 notification.setTitle('Apex Custom Notification');
16 notification.setBody('The notifications are coming from INSIDE the Apex!');
17
18 // Set the notification type and target
19 notification.setNotificationTypeId(notificationType.Id);
20 notification.setTargetId(targetId);
21
22 // Actually send the notification
23 try {
24 notification.send(recipientsIds);
25 }
26 catch (Exception e) {
27 System.debug('Problem sending notification: ' + e.getMessage());
28 }
29 }
30}CustomNotification Constructors
The following are constructors for CustomNotification.
CustomNotification(typeId, sender, title, body, targetId, targetPageRef)
Signature
public CustomNotification(String typeId, String sender, String title, String body, String targetId, String targetPageRef)
Parameters
- typeId
- Type: String
- The ID of the Custom Notification Type being used for the notification.
- sender
- Type: String
- The User ID of the sender of the notification.
- title
- Type: String
- The title of the notification, as it will be seen by recipients. Maximum characters: 250.
- body
- Type: String
- The body of the notification, as it will be seen by recipients. Maximum characters: 750.
- targetId
- Type: String
- The Record ID for the target record of the notification.
You must specify either a targetID or a targetPageRef. See Custom Notification Usage.
- targetPageRef
- Type: String
- The PageReference for the navigation target of the
notification.
You must specify either a targetID or a targetPageRef. See Custom Notification Usage.
CustomNotification Methods
The following are methods for CustomNotification.
send(users)
Signature
public void send(Set<String> users)
Parameters
- users
- Type: Set<String>
- Required. A set of recipient IDs. Each recipient ID corresponds to a recipient or recipient type
that the notification should be sent to. Valid recipient or recipient type values are:
- UserId — The notification is sent to this user, if this user is active.
- AccountId — The notification is sent to all active users who are members of this account’s Account Team.
- OpportunityId — The notification is sent to all active users who are members of this opportunity’s Opportunity Team.
- GroupId — The notification is sent to all active users who are members of this group.
- QueueId — The notification is sent to all active users who are members of this queue.
Return Value
Type: void
Example
See the Custom Notification Example.
setNotificationTypeId(id)
Signature
public void setNotificationTypeId(String id)
Parameters
- id
- Type: String
- The ID of the Custom Notification Type being used for the notification.
- A notification type is required to send a custom notification. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.
setTitle(title)
Signature
public void setTitle(String title)
Parameters
- title
- Type: String
- The title of the notification, as it will be seen by recipients. Maximum characters: 250.
- A title is required to send a custom notification. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.
setBody(body)
Signature
public void setBody(String body)
Parameters
- body
- Type: String
- The body of the notification, as it will be seen by recipients. Maximum characters: 750.
- A body is required to send a custom notification. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.
setSenderId(id)
Signature
public void setSenderId(String id)
Parameters
- id
- Type: String
- The User ID of the sender of the notification.
- Setting a sender is optional. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.
setTargetId(targetId)
Signature
public void setTargetId(String targetId)
Parameters
- targetId
- Type: String
- The Record ID for the target record of the notification.
- Either a targetID or a targetPageRef is required to send a custom notification. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.
setTargetPageRef(pageRef)
Signature
public void setTargetPageRef(String pageRef)
Parameters
- pageRef
- Type: String
- The PageReference for the navigation target of the notification.
- Either a targetID or a targetPageRef is required to send a custom notification. See Custom Notification Usage.
Return Value
Type: void
Example
See the Custom Notification Example.