DEV Community

Cover image for App Inbox Notification Feed in Next.js
Nik L. for SuprSend

Posted on

App Inbox Notification Feed in Next.js

A notification center in a Next.js app acts as a central hub where users can access all their notifications anytime. This feature enhances user engagement and ensures no important updates are missed. In this tutorial, we'll guide you through creating a comprehensive notification center in a Next.js application, including backend setup and frontend development for a user-friendly notification feed.

By following this guide, you'll develop a fully functional notification system in your Next.js app with real-time updates. This implementation will enhance your app's user experience and provide a scalable solution for managing notifications.

Key topics include:

  • Setting up a Next.js project for your notification center.
  • Building a backend with Node.js and Express to handle notifications.
  • Creating and integrating a frontend notification widget with the backend.
  • Implementing real-time updates for an up-to-date notification feed.
  • Styling the notification UI for responsiveness and user-friendliness.

No matter if you call it an in-app inbox, notification widget, or alert center, the principles and steps remain consistent. Let's get started on building your Next.js notification center!

Prerequisites

  • Knowledge of Next.js
  • SuprSend account with a secret key and token
  • A Next.js application

Where will we build our app inbox in Next.js?

For this tutorial, we've created a demo Next.js project called b2b2c-freight_company available on GitHub. The application is also deployed on Vercel, allowing you to see the real-time notification feed here: Freight B2B2C App.

Building a real-time notification center in Next.js

Step 1: Creating an account on SuprSend

Start by signing up on SuprSend. After onboarding, navigate to Developers → API Keys in the side panel to find your Workspace Key and Workspace Secret.

Image description

Step 2: Finding the right place for your app inbox

Typically, the app inbox is located in the header with other menu options. Alternatively, it can be an option in the side panel for a full-page notification center, similar to LinkedIn. In our case, we'll implement the notification center in the navigation header. Relevant code is available here.

Step 3: Installing and integrating SuprSendInbox in Next.js

  1. Install the SuprSend App Inbox Package:
   npm install @suprsend/react-inbox
Enter fullscreen mode Exit fullscreen mode
  1. Import Necessary Packages:
   import 'react-toastify/dist/ReactToastify.css';
Enter fullscreen mode Exit fullscreen mode
  1. Disable Server-Side Rendering for SuprSend:
   import dynamic from 'next/dynamic';

   const SuprSendInbox = dynamic(() => import('@suprsend/react-inbox'), { ssr: false });
Enter fullscreen mode Exit fullscreen mode
  1. Place the SuprSendInbox Component:
   const workspaceKey = process.env.NEXT_PUBLIC_SUPRSEND_WORKSPACE_KEY || '';
   const subscriberId = process.env.NEXT_PUBLIC_SUPRSEND_SUBSCRIBER_ID || '';
   const distinctId = process.env.NEXT_PUBLIC_SUPRSEND_DISTINCT_ID || '';

   export default function HomePage() {
     return (
       <div className="flex min-h-screen bg-gray-900 text-white">
         <div className="fixed top-0 right-0 m-4 z-50">
           <SuprSendInbox
             workspaceKey={workspaceKey}
             subscriberId={subscriberId}
             distinctId={distinctId}
             themeType="dark"
           />
         </div>
       </div>
     );
   }
Enter fullscreen mode Exit fullscreen mode
  1. Styling the App Inbox Component: If using Tailwind CSS, integrate the inbox into your navigation panel and customize it:
   <div className="fixed top-0 right-0 m-4 z-50">
     <SuprSendInbox
       workspaceKey={workspaceKey}
       subscriberId={subscriberId}
       distinctId={distinctId}
       themeType="dark"
     />
   </div>
Enter fullscreen mode Exit fullscreen mode

Customize using props like workspaceKey, subscriberId, distinctId, and themeType. More details are in the SuprSend documentation.

Step 4: Setting environment variables

Ensure you set the following in your .env file:

NEXT_PUBLIC_SUPRSEND_WORKSPACE_KEY=your_workspace_key
NEXT_PUBLIC_SUPRSEND_SUBSCRIBER_ID=your_subscriber_id
NEXT_PUBLIC_SUPRSEND_DISTINCT_ID=your_distinct_id
Enter fullscreen mode Exit fullscreen mode

Step 5: Setting triggers for notifications

With your app inbox set up and visible, configure triggers for sending notifications using any backend SDK. We will use the Node SDK. Documentation for sending and triggering events is available here.

Image description

Key Points

  • Disable server-side rendering, as the app inbox operates only on the client side.
  • For generating the subscriber key, perform HMAC authentication using any backend SDK.

Integrating a notification center in your Next.js app significantly boosts user engagement and ensures crucial messages are never missed. By following these steps, you can effectively implement the SuprSend app inbox, optimizing performance and functionality with dynamic import and client-side rendering.

This approach provides extensive customization options, fitting seamlessly within your existing design. With this robust notification system, you can focus on building more features, knowing your users are always informed and engaged. Happy coding!

Top comments (0)