DEV Community

Cover image for How to add a notification banner or popup to your website
Kartik Grewal for Canonic Inc.

Posted on

How to add a notification banner or popup to your website

A product's ability to capture the user's attention is crucial, and dynamic prompts/notification is one such efficient way to engage users.โœจ

It allows you to communicate with users by directing their attention to the navigation bar and can be used for a variety of purposes, such as for announcing business news, showing off the best features, generating leads, and so on.

This article will guide you step by step to build your own Dynamic Prompts/Notifications bar. ๐Ÿ‘ฉโ€๐Ÿ’ป

We used React for the frontend, Canonic for the Part backend.

Let's begin! ๐Ÿ“Œ

๐Ÿ“Step 1: Create-react-app

Lets start with creating a new react project - Use create-react-app

npx create-react-app dynamic-notifications
Enter fullscreen mode Exit fullscreen mode

Next, to create a basic skeleton, just edit src/App.js

import React from "react";
import "./App.css";

function App() {
  return (
    <div className="app">
      <section className="app-body">
        <h3>Dynamic popups and notifications</h3>
        <p>These notifications come from the data stored on your Canonic app</p>
      </section>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

โœจStep 2: Add some styling

Once you're done with the basic skeleton, it's time to add some styling to it. To add the styling use - Edit src/App.css

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  font-family: sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h3 {
  font-size: calc(10px + 2vmin);
  margin-bottom: 8px;
}

.app {
  text-align: center;
}

.app-body {
  background-color: #020d57;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  overflow: hidden;
}

.app-body p {
  font-size: calc(2px + 2vmin);
  margin: 0;
  opacity: 0.5;
}
Enter fullscreen mode Exit fullscreen mode

After completing the steps above, run npm start and you should have something similar to this๐Ÿ‘‡

step2

๐Ÿ””Step 3: Add the Notification details

Now it's time to add the notification and some basic styles, add the HTML to src/App.js

...
<div className="app">
    **<header className="app-notification"> ๐Ÿš€ This is a notification </header>**
    <section className="app-body">
...
Enter fullscreen mode Exit fullscreen mode

โœจStep 4: Make it look nice!

Let's add some styling to app, edit src/App.css


...

.app-notification {
  position: absolute;
  background-color: #4760ff;
  padding: 8px;
  width: 100%;
  color: #fff;
}

.app-notification p {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
}

...
Enter fullscreen mode Exit fullscreen mode

Once done with the stated steps, this is how it should look: ๐Ÿ‘‡

Step 4

๐Ÿ‘ฉโ€๐Ÿ”งStep 5: Let's make it dynamic

Let's move ahead and make it dynamic.

Here, we want to show a different popup based on certain criteria. However, if we do it on the front end, the logic can easily be exposed. Thus, letโ€™s rather create an API where the logic is computed on the backend, using Canonic.

Clone this project and head on to the CMS to create a few notifications.

Step 5

๐Ÿš€Step 6: Create custom API

Once done, head on to the API section and create an API to return random notifications.

Step 6

Put the below code into the code section to return random notifications from the database.

module.exports = async function getRandom() {
  const count = await Notification.count();
  const random = Math.floor(Math.random() * count);
  return Notification.findOne().skip(random);
}
Enter fullscreen mode Exit fullscreen mode

Step 6

๐ŸงชStep 7: Let's test it!

In order to do the testing, navigate to the Docs section and select the newly created endpoint. Click Open In Playground and hit the play button. The data should now appear.

Step 7

โš’Step 8: Time to integrate backend

We can easily integrate it by modifying App.js so that we fetch the data.

We'll use Axios to make the API call, install it using -npm install axios.

Make sure to replace "YOUR_API_URL_HERE" with the URL for your endpoint. You should be able to get that from the docs section.

...
import axios from "axios"
...
function App() {
  const [notification, setNotification] = React.useState();

  const getNotification = React.useCallback(
    () =>
      axios("YOUR_API_URL_HERE").then(({ data }) => data.data),
    []
  );

  React.useEffect(() => {
    getNotification().then(setNotification);
  }, [getNotification, setNotification]);

    ...

    <div className="app">
        {notification && (
      <header
        className="app-notification"
        dangerouslySetInnerHTML={{ __html: notification.text }}
      />
    )}
    ...
Enter fullscreen mode Exit fullscreen mode

๐Ÿ””Step 9: Refresh to get random notification

After the successful implementation of all the above states steps, you will get a random notification each time you refresh.

Step 9

Step 9

๐ŸŽ‰ Voila!

You have successfully made your one dynamic prompt. ๐Ÿ’ƒ๐Ÿ•บ

Check out the live demo here and sample code on github here.


Conclusion

Hope this guide helps you. You can also check out our other guidesย here.

Join us on discord to discuss or share with our community. Write to us for any support requests atย support@canonic.dev. Check out ourย websiteย to know more about Canonic.


Latest comments (0)