Send custom bell notification using Salesforce Rest Api from APEX

Send custom bell notification using Salesforce Rest Api from APEX
Send custom bell notification using Salesforce Rest Api from APEX

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.

Create Custom Notification Type
Create 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)
    public void notifyCurrentUser(String message)
            Http h = new Http();
            HttpRequest req = new HttpRequest();
        +             '/services/data/v46.0/actions/standard/customNotificationAction');
            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};
            HttpResponse res = h.send(req);

   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'
    System.enqueueJob(new QueueBellNotification(message,title));

Call the notifyUser() method and you are good to go.

Send custom bell notification using Salesforce Rest Api from APEX
Send custom bell notification using Salesforce Rest Api from APEX

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.

Popular Post:

Atul Singh

I'm a Salesforce Lightning Champion, Certified Developer & Consultant with 2.5yrs. of experience. I love to code, and so, I have high experience in working with APEX and Custom LEX. During my free time, I explore things realted to Salesforce and new technologies like RPA.

You may also like...