[Salesforce] Use custom notification function

Salesforce uses custom notifications


In Salesforce, the small bell in the upper right corner of the page is a notification function on the platform. It is similar to a message push. When an event occurs, it will be pushed to the designated user, so that the user can pay attention to the first time when using it. To the event.
Insert picture description here

Salesforce also provides a custom notification function. Using Interface GUI + Apex a small amount of code, we can customize a custom notification to achieve platform-level push messages.

Platform settings

First, enter the settings page, the left side of the Quick Search box, enter a custom notification , click to enter the sub page.

Click on the right side of the New button in the pop-up box, enter the name and custom notification API name, the name of the follow-up we will use the API in Apex class.

The supported channels are desktop and mobile. Both ends of Salesforce are compatible. Click Save.

Apex class

Create a new Apex class as our working class for sending notifications. The custom notification class is Messaging.CustomNotification.

Messaging.CustomNotification

Attributes

Attribute name Types of meaning
typeId String Custom notification Id
sender String Sender id
title String title
body String text
targetId String Id of the target record
targetPageRef String Non-record page index

method

  • send(user)
  • setNotificationTypeId(id)
  • setTitle(title)
  • setBody(body)
  • setSenderId(id)
  • setTargetId(id)
  • setTargetPageRef(pageRef)

Basically property settermethods.

Code example

public without sharing class CustomNotificationFromApex {
    
    
 
    public static void notifyUsers(Set<String> recipientsIds, String targetId) {
    
    
 
        // 取得自定义通知的Id
        CustomNotificationType notificationType = 
            [SELECT Id, DeveloperName 
             FROM CustomNotificationType 
             WHERE DeveloperName='Custom_Notification'];
        
        // 新建一个自定义通知对象
        Messaging.CustomNotification notification = new Messaging.CustomNotification();
 
        // 设置标题、正文
        notification.setTitle('Apex Custom Notification');
        notification.setBody('The notifications are coming from INSIDE the Apex!');
 
        // 设置类型和目标记录Id
        notification.setNotificationTypeId(notificationType.Id);
        notification.setTargetId(targetId);
        
        // 发送通知
        try {
    
    
            notification.send(recipientsIds);
        }
        catch (Exception e) {
    
    
            System.debug('Problem sending notification: ' + e.getMessage());
        }
    }
}

Note that after setting the targetId, clicking on the notification push will automatically jump to the record page of the Id without setting more pageReferences.

Reference link

For more complete documentation, please refer to the official documentation

Guess you like

Origin blog.csdn.net/qq_35714301/article/details/114692607