RedwoodJS
Learn how to use Novu to quickly send multi-channel (SMS, Email, Chat, Push) notifications with RedwoodJS.
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 in a RedwoodJS app.
Explore the code for this quickstart on GitHub.
Explore the live demo.
Prerequisites
To follow the steps in this quickstart, you’ll need:
- A Novu account. If you don’t have one, sign up for free.
- Node.js installed on your development machine.
Create a RedwoodJS app
Clone the popular RedwoodJS blog tutorial app by running the following command in your terminal:
Install Novu
To install Novu’s In-App notification center, simply issue the following command:
Next, install Novu’s Node SDK to fire notifications from the api/backend:
Register The Notification Center Component
Head over to web/src/layouts/BlogLayout/BlogLayout.js
file of the recently cloned project and add the Novu In-App Notification center.
The file should look like this:
Add REDWOOD_ENV_APPLICATION_IDENTIFIER
and REDWOOD_ENV_SUBSCRIBER_ID
to
the .env
file.
The Subscriber ID
ideally should be the id
of the logged in user. When a user logs into your app, they’ll be able to see all their notifications in the notification center. For this to work successfully, the user should have been created as a subscriber on Novu.
The Application Identifier
is a public key used to identify your application.
Head over to your Novu dashboard settings, copy the application identifier and add it as the value of REDWOOD_ENV_APPLICATION_IDENTIFIER
.
Head over to the Subscribers page, grab a subscriber identifier and add it as the value of REDWOOD_ENV_SUBSCRIBER_ID
in the .env
file.
Now, check your app, you will see a notification bell button. Click on the button to reveal the notification center UI. Voila!
There are no notifications because none has been triggered yet. When notifications are sent to a subscriber, it will show up in the UI. Next, we’ll learn how to trigger notifications.
Create A Workflow
The first step to trigger notifications is to create a workflow. A workflow is like a map that holds the entire flow of messages sent to the subscriber.
The workflow includes the following:
- Workflow name and Identifier
- Channel tailored content:
Channel | Content Style | Custom Variables{{handlebars}} format |
---|---|---|
HTML | ✅ | |
Visual Editor | ✅ | |
SMS | Text | ✅ |
Chat | Text | ✅ |
In-App | Text | ✅ |
Push | Text | ✅ |
Please proceed to create a workflow.
- Click Workflows on the left sidebar of your Novu dashboard.
- Click the Add a workflow button on the top left.
- The name of the new workflow is currently “Untitled.” Rename it to a more suitable title.
- Select In-App as the channel you want to add.
- Click on the recently added In-App channel and configure it.
Configure it as per your requirements and then save it
I’ll briefly explain the function of each label in the image above.
- 1-Preview: Shows you a glimpse of how each notification item will look like 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.
In the In-App channel Editor
, add the following content and rename the step to Onboarding In App
:
{{ name }}
and {{ message }}
are payload values that will be passed on from
our code to the workflow template so it can appear in the notification sent to
subscribers.
Click on the Update
button to save the configuration successfully!
- In the workflow page, drag and drop the Email channel to the workflow canvas to be just below the
In-App
channel. - Click on the recently added Email channel and configure it:
Add Sender name
and Subject
. Ensure the Default Layout
is selected for the Email Layout.
Switch to the Custom Code
section and add the following:
Finally, give the workflow a name. We’ll need the workflow trigger identifier (which is the slug of the name) later.
Next, we’ll learn how to trigger notifications to subscribers.
Trigger A Notification
Open up api/src/services/contacts/contacts.js
, and add the following code into your app to trigger a notification:
Before running the code, make sure you understand the following:
- The value of
<workflow-trigger-id>
should be the workflow’s trigger ID/slug. - The value of
payload
is an array of the data that you want to be dynamically injected into the workflow content. - The value of
subscriberId
is the id of the subscriber on Novu. - Ensure
NOVU_API_KEY
is in the.env
file and with the appropriate value gotten from the dashboard.
One more thing
Head over to the integrations store on your Novu dashboard.
Select the Novu Email Test Provider, and mark as the primary provider for Emails. This ensures that all emails are sent via the Novu Email provider.
Test the app
Ensure your app is still running and head over the /contact
page in the app. Fill the form and click Save
.
The app will trigger two notifications:
- An In-App notification
- An Email notification
Conclusion
Great job! If you’ve reached this point, you should now have successfully created workflow, configured a channel provider, triggered a workflow, and sent an email and In-App notification.