Send notifications across different channels (SMS, Email, Chat, Push) and enable real-time In-App notifications with the help of a rich and customizable Notification Center.

Requirements

To follow the steps in this quickstart, you’ll need:

  • A Novu account. Sign up for free if you don’t have one yet.
  • A working React development environment.

You can find the code for this project here

Install Novu React Notification Center Package in your React app

The Novu React package provides a React component library that you can use to add a fully functioning notification center to your React application.

To use it, first, install the React Notification Center package by running the following command in your terminal:

npm install @novu/notification-center

Before you can use Novu in your React app, you’ll need two things:

  1. Create a workflow to use for sending notifications, and
  2. Create a subscriber - recipient of notifications.

Create a workflow

Before triggering a notification, we need to create a workflow. A workflow is like a blueprint that all the notifications are supposed to follow.

The recipients of a triggered notification are called subscribers.

The workflow includes the following:

  • Workflow name and Identifier
  • Channel-tailored content:
ChannelContent Style
Email1. Custom Code (HTML) with option to use custom variables via the handlebars , , syntax.
2. Click and place UI items with the visual workflow editor.
SMSText with the option to use handlebars syntax, to inject custom variables.
ChatText with the option to use handlebars syntax, to inject custom variables.
In-AppText

These are the steps to create a workflow:

  1. Click Workflow on the left sidebar of your Novu dashboard.
  2. Click the Create Workflow button on the top right.
  3. The name of the new workflow is currently “Untitled”. Rename it to a more suitable title.
  4. Select In-App as the channel you want to add.

Select the in-app channel

  1. Click on the recently added “In-App” channel and configure it according to your preferences. Once you’re done, click “Update” to save your configuration.

Configure the in-app node as per your need

I’ll briefly explain the function of each label in the image above:

  • 1-Preview: This shows you a glimpse of how each notification item will look in the Notification Center UI.
  • 2-Avatar: If turned on, each notification item will show the avatar of the subscriber.
  • 3-Action: With this, you can add a primary and secondary call to action button to each notification item.
  • 4-Notification Feeds: This displays a stream of specific notifications. You can have multiple feeds to show specific notifications in multiple tabs.
  • 5-Redirect URL: This is the URL to which a subscriber can be directed when they click on a notification item.
  • 6-Filter: This feature allows you to configure the criteria for delivering notifications. For instance, you can apply a filter based on a subscriber’s online status to send them an email if they were online within the last hour. Read more about filters.
  • 7-Editor: You can add text that you want to be displayed in each notification item. Additionally, you can specify custom variables using {{ }}. This means you can inject variables from your code into a notification item’s text via a payload.

In our case, we’ll use the custom variables functionality, as shown below:

We're gonna use custom varaibles here

Feel free to add only text for now and rename the workflow to quickstart. It automatically creates a slug-like Identifier that will be needed in later steps to trigger a notification.

Renaming the workflow to 'quickstart'

Now, we’ll learn how to create subscribers on Novu - Recipients of Notifications!

Create a subscriber

If you click “Subscriber” on the left sidebar of the Novu dashboard, you’ll see the subscriber list. By default, there will only be one subscriber as you’re automatically added as a subscriber when you sign up for Novu:

One subscriber is automatically created when you sign up

Now, let’s create a subscriber on Novu. After creating a subscriber, we’ll trigger a notification to this subscriber. Subscribers are identified by a unique subscriberId.

With Novu, you can create a subscriber using any of its SDKs (Node.js, PHP, .NET, Go, Ruby, Python and Kotlin). The code to create a subscriber in Novu is:

import { Novu } from '@novu/node';

const novu = new Novu('<YOUR_NOVU_API_KEY>');

await novu.subscribers.identify('123', {
  firstName: 'Sumit',
  lastName: 'Saurabh',
});

You can get your API key from the Novu dashboard. Replace YOUR_NOVU_API_KEY_HERE with it. Now, if you’ll go to the Novu dashboard, you shall see the subscriber we created above with subscriberId of 123.

You can also update information about an already existing subscriber using the subscriber.update method as shown below:

import { Novu } from '@novu/node';

const novu = new Novu('<YOUR_NOVU_API_KEY>');

await novu.subscribers.update('123', {
  firstName: 'Saurabh', // new first name
  lastName: 'Sumit', // new last name
});

Using Novu in a React app

We have already installed the Novu notification center package above. To use it in an app, simply import it and add the component to your React app as follows:

import React, { useState } from 'react';
import {
  NovuProvider,
  PopoverNotificationCenter,
  NotificationBell,
} from '@novu/notification-center';

function App() {
  const [description, setDescription] = useState('');
  function onNotificationClick(message) {
    // your logic to handle the notification click
    if (message?.cta?.data?.url) {
window.location.href = message.cta.data.url;
    }
  }

  return (
    <>
      <NovuProvider
        subscriberId={'<YOUR_SUBSCRIBER_ID>'}
        applicationIdentifier={'<YOUR_NOVU_APPLICATION_IDENTIFIER>'}
        initialFetchingStrategy={{
          fetchNotifications: true,
          fetchUserPreferences: true,
        }}
      >
        <PopoverNotificationCenter
          onNotificationClick={onNotificationClick}
          listItem={(notification) => <div>{notification?.payload?.description}</div>}
        >
          {({ unseenCount }) => <NotificationBell unseenCount={unseenCount} />}
        </PopoverNotificationCenter>
      </NovuProvider>
    </>
  );
}

export default App;

You’ll notice two things used above - applicationIdentifier and subscriberID.

An application identifier is a public key used to identify your application. You can get your own application identifier from the Novu dashboard settings.

And subscribers are users to which notifications will be sent. They are identified by a subscriberID which you can also find in the Novu subscribers dashboard.

Trigger a notification

We can trigger a notification by simply running the code below with the correct credentials;

import { Novu } from '@novu/node'; 

const novu = new Novu('<API_KEY>');

novu.trigger('quickstart', {
    to: {
      subscriberId: '123'
    },
    payload: {
      description: 'Test notification'
    }
  });

This will take the workflow we created above with the identifier quickstart and send a notification to the subscriber with subscriberId of 123.

Make sure you’re executing this code with the correct credentials.

Conclusion

Great job! If you’ve reached this point, you should now have successfully set up the notification center, created a subscriber, a workflow, configured a channel provider and triggered a notification in your React application.