How to get unread notification / topic count via API?

Using the API.

The endpoint https://example.com/notifications?username=Falco which can be discovered using Reverse engineer the Discourse API and navigating to a user profile Notification page.

You will need to pass an Admin API Key to be able to query notifications from any user.

Depends on your app.

A good default is:

  • When you are rendering a new page in your main app, you will know your current user.

  • Translate your app user id to a Discourse user id (and cache this for a long period)

  • Query the notifications endpoint in Discourse (cache for a short period)

  • Render your app page with the Discourse Notifications

For Dismissing, a click to dismiss will need to hit your backend and then you issue a PUT to https:/example.com/notifications/mark-read. That needs to clear your app notifications cache for the user.

Advanced stuff would include listening to new notifications in a Discourse plugin (or a PR to core that adds a Notification webhook. That would allow your main app to keep an websocket or server-sent events and update notifications in real time.

4 Likes