Level Up Your JSS Next.js PWA: Unleashing the Power of Push Notifications

Priyanka Gulia
4 min readMay 28, 2024

--

In my previous blog, we explored the exciting world of Progressive Web Apps (PWAs) and how they can elevate your JSS Next.js app. We saw how PWAs offer an app-like experience, boosting engagement and SEO. But wait, there’s more! In this follow-up, we’ll dive into the world of Push Notifications, a powerful tool that further enhances user experience and keeps them coming back for more.

The Magic of Push Notifications

Imagine this: A user visits your JSS Next.js PWA and reads a captivating article. They leave, but then — a timely notification pops up on their phone, reminding them about a new blog post or an exciting offer. That’s the magic of Push Notifications! These are messages sent directly to a user’s device, even when they’re not actively browsing your app.

Why Push Notifications Are a Game Changer

  • Enhanced Engagement: Stay connected with users and re-engage them with relevant updates, driving repeat visits and boosting user loyalty.
  • Increased Conversions: Timely notifications can nudge users towards desired actions, like completing a purchase or signing up for a newsletter.
  • Improved User Experience: Provide valuable updates and reminders, keeping users informed and engaged with your PWA.

Adding Push Notifications to Your JSS Next.js PWA with Web-Push

The next-pwa library we explored in the previous blog offers built-in support for push notifications using Web-Push. Here's a step-by-step guide to get you started, following the pattern of our previous PWA adventures:

  • Enlisting Web-Push: Just like a trusty sidekick, we’ll need to enlist the web-push library to handle the heavy lifting of push notifications. Installation is a breeze using npm:
npm i web-push
  • Generating VAPID Keys: VAPID (Voluntary Application Server Identification) keys are like secret handshakes that ensure secure communication between your server and push notification services. To generate these keys, we’ll use a handy command-line tool provided by web-push:
npx web-push generate-vapid-keys
  • Securing Your Keys: Remember the hidden treasure map we kept in a secure vault in our previous blog (well, the .env file)? That's where we'll store our precious VAPID keys. Add the following code to your .env file at the root of your project, replacing the placeholders with your actual keys that you have generated in previous step:
WEB_PUSH_EMAIL=<YOUR_EMAIL>
WEB_PUSH_PRIVATE_KEY=<VAPID_PRIVATE_KEY>
NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY=<VAPID_PUBLIC_KEY >
  • Server-Side Hero: We’ll create a separate JavaScript file (often named notifications.js) to handle sending push notifications from your server. This file will utilize the web-push library and your VAPID keys. Here's an example:
notification.js
  • Service Worker Magic: Our service worker, the backbone of PWAs, plays a crucial role in push notifications. In your separate service worker file (usually index.js), you’ll need to implement logic to:
    Subscribe Users: Request permission from users to receive notifications and manage subscriptions.
    Handle Incoming Messages: Intercept incoming push notifications and display them as user-friendly alerts.
    The next-pwa library provides helper functions to simplify this process. Here’s a basic example to get you started:
Worker File
  • Client-Side Integration: The magic happens on the user’s screen too! In your JSS Next.js app’s frontend, you’ll need to integrate components provided by next-pwa to:
    Request Permission: Ask users for permission to receive push notifications.
    Handle Subscriptions: Manage user subscriptions and send them to your server for storage.
    This ensures a smooth user experience for opting in to push notifications.
Client Side Code

Experiment and Explore!

I’ve covered the core implementation of Push Notifications using Web-Push in our JSS Next.js PWA. For a complete demonstration of push notifications in action, check out the code in my GitHub repository.

How it works!

I encourage you to explore the next-pwa documentation https://github.com/topics/next-pwa for more details and advanced features.

PWAs: The Future of Progressive Apps

Push notifications are just one way to take your JSS Next.js PWA to the next level. Progressive Web Apps offer a vast range of features that can significantly enhance your user experience. The journey with PWAs is full of discoveries. I hope this blog post has equipped you with the knowledge to implement push notifications and elevate your JSS Next.js PWA. For more insights and inspiration, stay connected for future blog posts, or keep exploring the vast resources available online!

--

--

Priyanka Gulia

XM Cloud Certified || Senior Sitecore Developer || Crafting Web Experiences