Skip to content

sannlynnhtun-coding/nextjs-firebase-push-notification

Repository files navigation

Next.js Firebase Push Notification Demo

A Next.js application demonstrating Firebase Cloud Messaging (FCM) push notifications with browser-based configuration.

Features

  • βœ… Browser-based Firebase Configuration - Configure Firebase settings directly from the browser UI
  • βœ… Firebase v10+ Support - Uses the latest Firebase modular SDK
  • βœ… Next.js 14 - Upgraded to the latest Next.js version
  • βœ… Push Notifications - Receive and display push notifications from Firebase Console
  • βœ… Service Worker Support - Background notifications even when the app is closed
  • βœ… Token Management - Easy FCM token copying for testing

Getting Started

Installation

First, install the dependencies:

npm install
# or
yarn install

Running the Development Server

npm run dev
# or
yarn dev

Open http://localhost:3000 with your browser to see the result.

Firebase Configuration

Step 1: Get Firebase Configuration

  1. Go to Firebase Console
  2. Create a new project or select an existing one
  3. Navigate to Project Settings β†’ General tab
  4. Scroll down to Your apps section
  5. Click on the Web app icon (</>) to add a web app if you haven't already
  6. Copy the Firebase configuration values:
    • API Key
    • Auth Domain
    • Project ID
    • Storage Bucket
    • Messaging Sender ID
    • App ID
    • Measurement ID (optional)

Step 2: Get VAPID Key

  1. In Firebase Console, go to Project Settings β†’ Cloud Messaging tab
  2. Scroll down to Web Push certificates section
  3. If no key exists, click Generate key pair
  4. Copy the Key pair value (this is your VAPID key)

Step 3: Configure in the App

  1. Navigate to the Settings page (/settings) in the app
  2. Fill in all the Firebase configuration fields
  3. Click Save Configuration
  4. The app will reload and initialize Firebase with your configuration

Testing Push Notifications

Method 1: Using Firebase Console

  1. After configuring Firebase, copy your FCM token from the home page
  2. Go to Firebase Console β†’ Cloud Messaging
  3. Click Send test message
  4. Paste your FCM token in the FCM registration token field
  5. Enter a notification title and message
  6. Click Test
  7. You should receive the notification in your browser!

Method 2: Using Firebase Admin SDK

You can also send notifications programmatically using the Firebase Admin SDK from your backend.

Project Structure

β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ FirebaseConfigForm.js    # Firebase configuration form component
β”‚   └── PushNotificationLayout.js # Push notification handler wrapper
β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ index.js                 # Home page
β”‚   β”œβ”€β”€ settings.js              # Firebase configuration page
β”‚   β”œβ”€β”€ offers.js                 # Example page with notifications
β”‚   └── _app.js                  # App wrapper
β”œβ”€β”€ public/
β”‚   └── firebase-messaging-sw.js # Service worker for background notifications
β”œβ”€β”€ utils/
β”‚   └── firebase.js              # Firebase initialization utilities
└── next.config.js               # Next.js configuration

Technologies Used

  • Next.js 14 - React framework
  • Firebase 10.7+ - Firebase Cloud Messaging
  • React Toastify - Toast notifications
  • LocalForage - Local storage for configuration and tokens
  • next-offline - Service worker support

Browser Support

This app requires a browser that supports:

  • Service Workers
  • Push Notifications API
  • IndexedDB (for LocalForage)

Modern browsers (Chrome, Firefox, Edge, Safari 16+) are supported.

Troubleshooting

Notifications not working?

  1. Make sure you've granted notification permissions in your browser
  2. Verify your Firebase configuration is correct
  3. Check that your VAPID key is correct
  4. Ensure the service worker is registered (check browser DevTools β†’ Application β†’ Service Workers)
  5. Check the browser console for any errors

Service Worker not registering?

  1. Make sure you're accessing the app via HTTPS (or localhost for development)
  2. Clear your browser cache and reload
  3. Check browser DevTools β†’ Application β†’ Service Workers for errors

Learn More

License

MIT

About

πŸš€ Next.js 14 + Firebase Cloud Messaging Demo. Configure Firebase settings directly in the browser, handle background push notifications with Service Workers, and easily manage FCM tokens!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors