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.
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.
Is it possible to fetch both standard and custom notifications of the user?
Best Answer
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 inThe issue will be limiting the data returned to what you're interested in. You'd need one or both of the following:
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.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
LastModifiedDateDesc
You could do an initial query/retrieval with getFeedElementsFromFeed. You'd have to utilize
pageParam
andpageSize
as you might be querying a lot at this point. This would provide the following:updatesToken
isReadByMe
.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:Of course, you could skip the first initial query and just rely on the
updatedSince
parameter if you construct your ownupdatedSince
token using milliseconds.