![Send custom bell notification using Salesforce Rest Api from APEX](https://forceblogs.com/wp-content/uploads/2020/07/Send-custom-bell-notification-using-Salesforce-Rest-Api-from-APEX.jpg)
Hello there! In my previous post, I had told you about the How to use Notification Builder in Flows . Today, Let’s us discuss about how to send the bell notification to the User from the apex.
We have a use case where many times that we need to notify the users about the completion of asynchronous jobs or failure notification from the async jobs like Batch class, Schedule class or Queuable class.
So let’s start.
Firstly, you need to create a Custom Notification Types. You can create a new or use any old ones, you only need to get the Id of the Custom Notification Type.
Now, let’s come to the code, this class receives two parameter, one is message, the body of the Custom Notification & another is Title, the Notification title.
public without sharing class QueueBellNotification implements Queueable, Database.AllowsCallouts {
public String title;
public String message;
public String customNotifTypeId;
public BatchBellNotification(String message, String title)
{
this.message = message;
this.title = title;
this.customNotifTypeId = [SELECT Id, DeveloperName FROM CustomNotificationType Where DeveloperName='<Your Custom Notification Name>'].Id;
}
public void execute(QueueableContext context)
{
notifyCurrentUser(message);
}
public void notifyCurrentUser(String message)
{
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm()
+ '/services/data/v46.0/actions/standard/customNotificationAction');
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
CustomNotificationActionInput input = new CustomNotificationActionInput();
input.customNotifTypeId = customNotifTypeId;
input.recipientIds = new List<String> {UserInfo.getUserId()};
input.title = title;
input.body = message;
input.targetId = customNotifTypeId;
CustomNotificationAction action = new CustomNotificationAction();
action.inputs = new List<CustomNotificationActionInput> {input};
req.setBody(JSON.serialize(action));
HttpResponse res = h.send(req);
System.debug(res.getBody());
}
public class CustomNotificationAction
{
public List<CustomNotificationActionInput> inputs { get; set; }
}
public class CustomNotificationActionInput
{
public String customNotifTypeId { get; set; }
public List<String> recipientIds { get; set; }
public String title { get; set; }
public String body { get; set; }
public String targetId { get; set; }
}
}
To send the notification to the user, you need to enqueue the class.
public void notifyUser(){
String title = 'Forceblogs Custom Notification';
String message = 'Yay! you just learned from forceblogs.com'
System.enqueueJob(new QueueBellNotification(message,title));
}
Call the notifyUser() method and you are good to go.
As you can see in the screenshot, you will receive the notification asap.
Salesforce Rest API Custom Notification Actions Documentation.
Let us know in comment section if this blog helps you out.
If you have any issue with the implementation, let me know.