8

Is there a way to fetch the unread bell icon notifications of a user in Apex?

We have a community built on the Build Your Own (LWR) template where we need to implement an LWC component that displays unread notifications of the community user.

I tried using the Notifications endpoint from Connect REST API. But it only retrieves custom notifications. Standard ones like mentions and comments on followed records are skipped. enter image description here

The documentation says:-

If a connected app makes a GET request, the API returns only custom notifications for the types that the connected app subscribes to with org-level settings applied.

But I can only see Custom Notification types under the Active Notification Subscriptions of the connected app. enter image description here

Is it possible to fetch both standard and custom notifications of the user?

1

1 Answer 1

8
+50

Unfortunately, no. The endpoint you're using seems exactly what you want/need - but, as you noted, will only give you custom notifications.

You can see this older question that mentions the lack of ability to get Chatter notifications in API/Apex. You can also see the following idea that was created and archived for low activity.

A workaround would involve a heavy lift (in addition to using the endpoint you mentioned) as it wouldn't really retrieve "notifications", but the underlying feed items. Below I'll detail some methods and possibilities - but, it's likely not a good enough solution to use depending on what you're replicating/doing with notifications.


With the ConnectApi Namespace, you do have several options for pulling the feed for a context user with various parameters. You can specify a ConnectApi.FeedType and the good news is that ConnectApi.FeedType.News should contain what you're interested in

Contains all updates for people the context user follows, groups the user is a member of, and files and records the user is following. Contains all updates for records whose parent is the context user.

The issue will be limiting the data returned to what you're interested in. You'd need one or both of the following:

  • Ability to see what feed elements are unread
  • Ability to only return elements from a certain time period (to limit processing)

There is a isReadByMe property within the model of information returned. The ability to filter at the request level and only return elements that are "unread" is done through ConnectApi.FeedFilter and that type of filter is limited in its application.

Unread — Feed elements that are created in the past 30 days and aren’t marked as read for the context user. This value is valid only for the Record feed of a group.

There does seem to be an updatedSince parameter that is returned in certain calls (or you can build) that can be passed to other methods to retrieve only items since a date.

updatedSince shows the following for right now:

  • 4:1641914920468
    • To the right of the colon is the date in milliseconds
    • The left of the colon, I believe, corresponds to the FeedSortOrder enum - 4, this case, refers to LastModifiedDateDesc

You could do an initial query/retrieval with getFeedElementsFromFeed. You'd have to utilize pageParam and pageSize as you might be querying a lot at this point. This would provide the following:

After having a updatesToken, you could then pass that in subsequent calls/retrievals to ConnectApi.ChatterFeeds.getFeedElementsUpdatedSince that returns the same things as the above method, but limits based on updated time:

Get a page of feed elements from the Files, Groups, News, People, and Record feeds. Include only feed elements that have been updated since the time specified in the updatedSince parameter.

Of course, you could skip the first initial query and just rely on the updatedSince parameter if you construct your own updatedSince token using milliseconds.

DateTime prevDay = DateTime.now().addDays(-1);
String sinceYesterday = '4:' + String.valueOf(prevDay.millisecond());

ConnectApi.FeedElementPage myNews = ConnectApi.ChatterFeeds.getFeedElementsUpdatedSince(
    null, 
    ConnectApi.FeedType.News, 
    'me', 
    3, 
    ConnectApi.FeedDensity.AllUpdates, 
    null, 
    25, 
    sinceYesterday
);

//see if item has not been read
for(ConnectApi.FeedElement feedElem : myNews){
    if(!feedElem.capabilities.readBy.isReadByMe){
       //...
    }
}
3
  • Thank you for the detailed answer. Even though it is not possible to get the 'notifications' directly, this will help in building a custom solution using ConnectApi.
    – dennisjsk
    Commented Jan 12, 2022 at 5:56
  • I am trying to build something similar on lightning community and I wanted to know is there a way to read custom notification in Apex (Soql) ? Other than making a call out to notifications api.
    – AJM
    Commented Sep 12, 2022 at 19:59
  • No, I don't believe any of the relevant objects are exposed through that manner - just the endpoints for it. Commented Sep 16, 2022 at 16:32

You must log in to answer this question.

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