The notification API offers information about user notifications in your app, letting you build custom activity UI indicators such as unread badge counts
This API is currently in BETA mode, and its default design and options are subject to change.
Through this API you can observe a summary of the notifications for the current user.
Information available through the notification summary API | ||
---|---|---|
unread
|
number
|
the number of notifications that the current user hasn't seen yet |
You can access the notifications summary using the useCordNotificationSummary
hook.
Initially this hook will return undefined
while the data is loading from our API. Your component will re-render once the summary data becomes available, then every time the numbers change (a new notification is created, a user marks a notification as read).
import { beta } from "@cord-sdk/react";
function App() {
const summary = beta.useCordNotificationSummary();
return (
<div>
{!summary && "Loading..."}
{summary && <p>Unread notifications: {summary.unread}</p>}
</div>
);
}
If you don’t use React, you can still access this information through the JavaScript API available on window.CordSDK.activity
.
To start observing the current user’s notifications, use the observeNotificationSummary
function, providing a callback
function. The callback
will be called once the with the current notification summary and subsequently every time the data updates.
// start observing notifications summary for the current user
const ref = window.CordSDK.beta.notification.observeNotificationSummary(
(summary) => {
console.log("Unread", summary.unread);
}
);
// stop observing
window.CordSDK.beta.notification.unobserveNotificationSummary(ref);
type NotificationSummary = {
unread: number;
};
type NotificationSummaryUpdateCallback = (
summary: NotificationSummary
) => unknown;
interface ICordNotificationSDK {
observeNotificationSummary(
callback: NotificationSummaryUpdateCallback
): ListenerRef;
unobserveNotificationSummary(ref: ListenerRef): boolean;
}
function useCordNotificationSummary(): NotificationSummary | null;