Observe notification summary information

How to use the notification API to observe notification summary information


Overview #

This method allows you to observe summary information about the current user's notifications, including live updates.

React:
import { notification } from '@cord-sdk/react';
const summary = notification.useSummary();
Vanilla JavaScript:
const ref = window.CordSDK.notification.observeSummary(callback);
window.CordSDK.notification.unobserveSummary(ref);
import { notification } from '@cord-sdk/react';
const summary = notification.useSummary();
Copy

Usage #

React:
import { notification } from '@cord-sdk/react';
const summary = notification.useSummary();
return (
  <div>
    {!summary && "Loading..."}
    {summary && (
      <p>Unread notifications: {summary.unread}</p>
    )}
  </div>
);
Vanilla JavaScript:
const ref = window.CordSDK.notification.observeSummary(
  (summary) => {
    // Received an update!
    console.log("Unread notifications", summary.unread);
  });
);
// ... Later, when updates are no longer needed ...
window.CordSDK.notification.unobserveSummary(ref);
import { notification } from '@cord-sdk/react';
const summary = notification.useSummary();
return (
  <div>
    {!summary && "Loading..."}
    {summary && (
      <p>Unread notifications: {summary.unread}</p>
    )}
  </div>
);
Copy

Available Data #

Graphic showing example uses of the Cord Location Summary API

The API provides an object which has the following fields:


unread #

number
The number of notifications that the current user hasn't seen yet.

What this function returns #

The hook will initially return undefined while the data loads from our API. Once it has loaded, your component will re-render and the hook will return an object containing the fields described under "Available Data" above. The component will automatically re-render if any of the data changes, i.e., this data is always "live".


Arguments this function takes #


callback #

required
NotificationSummaryUpdateCallback
This callback will be called once with the current notification summary, and then again every time the data changes. The argument passed to the callback is an object which will contain the fields described under "Available Data" above..

options #

optional
ObserveNotificationSummaryOptions

This is an object with the following fields:


filter #

optional
NotificationListFilter
An object that can be used to filter the notifications returned.

This is an object with the following fields:


metadata
optional
EntityMetadata
An arbitrary JSON object specified when the notification is created. The value for a metadata entry should be an object representing the metadata key/value to filter on. For example, to show only notifications with the metadata key of "category" set to "sales", set the filter to { metadata: { category: "sales" } }.

location
optional
Location
The location where the notifications live. This will be the location of the thread containing the message which prompted the notification.

groupID
optional
string
The group to which the message that prompted the notification belongs.

Ask Cordy