Observe notification summary information

How to use the notification API to observe the number of unread notifications


Overview #

This method allows you to observe the count of unread notifications for the current user, including live updates.
React:
import { notification } from '@cord-sdk/react';
const counts = notification.useNotificationCounts({
      filter: {
          metadata: { flavor: 'minty' },
          location: { page: 'bookmarks' },
          groupID: 'group123',
       },
    });

return (
  <div>
     {!counts && "Loading..."}
     {counts && (
       <p>Unread notifications: {counts.unread}</p>
      )}
  </div>
 );
Vanilla JavaScript:
const ref = window.CordSDK.notification.observeNotificationCounts(
  (counts) => {
       console.log("Unread notifications", counts.unread);
  },
  { filter: {
        metadata: { flavor: 'minty' },
        location: { page: 'bookmarks' },
        groupID: 'group123',
   }}
);
import { notification } from '@cord-sdk/react';
const counts = notification.useNotificationCounts({
      filter: {
          metadata: { flavor: 'minty' },
          location: { page: 'bookmarks' },
          groupID: 'group123',
       },
    });

return (
  <div>
     {!counts && "Loading..."}
     {counts && (
       <p>Unread notifications: {counts.unread}</p>
      )}
  </div>
 );
Copy

Available Data #

Graphic showing example uses of the Cord Notification Counts 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 #

A reference number which can be passed to unobserveSummary to stop observing notification summary information.

Arguments this function takes #


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 filter only notifications with the metadata key of "category" set to "sales", set the filter to { metadata: { category: "sales" } }.

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

This property can be one of the following:

  • Location
  • LocationFilterOptions

    This is an object with the following fields:


    value
    Location
    The Location of the threads.

    partialMatch
    boolean
    If true, perform partial matching on the specified location. If false, fetch information for only exactly the location specified.

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

Ask Cordy