0

I want to send custom notifications to users in Salesforce when certain conditions are met. My background is not technical but I know how to use APIs (using API clients), and I've seen that I can send the notifications created with the Notification Builder Platform through API Calls, which suits me perfectly.

But what about the other types of events that can also generate notifications in Salesforce, such as Generic Events, Platform Events, PushTopic Events? I've seen the developer documentation for these and they have a more technical implementation. What do they allow me to do that I can't with notifications from the Notifications Builder Platform?

I'm also interested in Salesforce Users not having to subscribe to an event in order to receive a notification. In the case of notifications created in the Notification Builder Platform, users don't have to subscribe to anything in order to receive them, right?

Thanks very much!

1 Answer 1

1

A very interesting found.

Though internally both should use same streaming channel, but Custom Notification might be using some internal channel to communicate which does not get counted in org streaming limit. But Events such as Generic Events, Platform Events, PushTopic Events uses the same streaming mechanism but these gets counted in the limit.

The main difference between them are below.

enter image description here

I have debugged the Custom Notification from Chrome browser and found out that you can actually subscribe to this event from a LWC Component and use it for custom functionality too.

export default class HelloWorld extends LightningElement {
    channelName = '/s/notifications/notification';
    isSubscribeDisabled = false;
    isUnsubscribeDisabled = !this.isSubscribeDisabled;

    subscription = {};

    handleChannelName(event) {
        this.channelName = event.target.value;
    }

    connectedCallback() {
        this.registerErrorListener();
    }

    // Handles subscribe button click
    handleSubscribe() {
        const messageCallback = function(response) {
            console.log('New message received: ', JSON.stringify(response));
        };

        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then(response => {
            console.log('Subscription request sent to: ', JSON.stringify(response.channel));
            this.subscription = response;
            this.toggleSubscribeButton(true);
        });
    }

    // Handles unsubscribe button click
    handleUnsubscribe() {
        this.toggleSubscribeButton(false);

        // Invoke unsubscribe method of empApi
        unsubscribe(this.subscription, response => {
            console.log('unsubscribe() response: ', JSON.stringify(response));
            // Response is true for successful unsubscribe
        });
    }

    toggleSubscribeButton(enableSubscribe) {
        this.isSubscribeDisabled = enableSubscribe;
        this.isUnsubscribeDisabled = !enableSubscribe;
    }

    registerErrorListener() {
        // Invoke onError empApi method
        onError(error => {
            console.log('Received error from server: ', JSON.stringify(error));
            // Error contains the server-side error
        });
    }
}

Messaging.CustomNotification customNotification = new Messaging.CustomNotification();
customNotification.setNotificationTypeId([SELECT Id FROM CustomNotificationType].Id);

customNotification.setTargetId([SELECT Id FROM Account LIMIT 1].Id);
customNotification.setTitle('here is my more data');
customNotification.setBody('Here is my data Here is my data ');
customNotification.send(new Set<String> {String.valueOf(UserInfo.getUserId())});

Check the event name : /s/notifications/notification

And on the UI you get a message like this.

enter image description here

You can use this mechanism to refresh components or page on demand. No need to subscribing to Platform or any streaming events.

ADVANTAGE -> Does not gets counted under concurrent subscriber. Does not gets counted in 50K message limit of streaming channel.

2
  • Hello, thanks for your answer. What do you mean that Notification can't be used for integration purpose, can you give me an example? At the moment I am working in a packaged app to be installed in Salesforce, I've seen that custom notifications can be included inside a packaged app. Can this be a good way to show notifications in an org which will have my packaged app installed?
    – bonzi
    Jul 23, 2021 at 14:53
  • Notifications cant be used for integration means, in an integration you send an event to external system with a payload, so for that purpose Custom notification cannot be used. Yes you can show a notification using Custom Notification in a packaged app. Jul 23, 2021 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .