Cloud Functions in Firebase Cloud Functions in Firebase

Firebase Cloud Functions Documentation with Node.js v22

Firebase Cloud Functions allow you to run backend code in response to events triggered by Firebase features (e.g., Firestore, Auth) or HTTP requests. This documentation provides a complete guide on:

Node.js Version Support in Firebase

Firebase supports the following Node.js versions for Cloud Functions:

Node js version          Status
16.x    Supported
18.x  Recommended
20.x Supported
22.xExperimental (as of 2024)

Node.js v22 is not officially GA (Generally Available) in Firebase as of May 2025. Ensure you are using the latest Firebase CLI and check release notes for updates.

Benefits of Node.js v22

  • Native fetch() API
  • Built-in Web APIs (e.g., URL, Request, Response)
  • Faster cold starts
  • Enhanced ESM support
  • Better async performance

Creating and Deploying a Notification Cloud Function

1.Initialize Firebase Project

firebase init functions

  • Choose JavaScript or TypeScript
  • Choose Node.js v22 if available (use CLI version >= v13)

2.Directory Structure

functions/

├── index.js

├── package.json

└── firebase.json

3. package.json

{
  "name": "notification-function",
  "type": "module",
  "engines": {
    "node": "22"
  },
  "dependencies": {
    "firebase-admin": "^11.10.0",
    "firebase-functions": "^4.0.0"
  }
}

4. index.js

import { initializeApp } from 'firebase-admin/app';
import { getMessaging } from 'firebase-admin/messaging';
import { onRequest } from 'firebase-functions/v2/https';

initializeApp();

export const sendNotification = onRequest(async (req, res) => {
  try {
    const { token, title, body } = req.body;

    if (!token || !title || !body) {
      return res.status(400).json({ error: 'Missing fields' });
    }

    const message = {
      token,
      notification: {
        title,
        body,
      },
    };

    const response = await getMessaging().send(message);
    console.log('Notification sent:', response);
    res.status(200).json({ success: true, response });
  } catch (error) {
    console.error('Error sending notification:', error);
    res.status(500).json({ success: false, error: error.message });
  }
});

5. Deploy to Firebase

firebase deploy –only functions

Sending Notification via Postman

POST https://<REGION>-<PROJECT_ID>.cloudfunctions.net/sendNotification

Headers:

Content-Type: application/json

Body:

{
  "token": "DEVICE_FCM_TOKEN",
  "title": "Hello User!",
  "body": "You have a new message."
}

Response:

{
  "success": true,
  "response": "projects/myproject/messages/1234567890"
}

How to Verify Notification Delivery

Response from Firebase:

  • A successful send() returns a message ID string.
  • If the token is invalid, Firebase returns an error like messaging/invalid-recipient.

In-App Confirmation (Client Side):

  • Implement onMessage() handler in your web/mobile app to confirm receipt.

FCM Diagnostics:

  • Use Firebase Console > Cloud Messaging > Delivery Reports to track delivery statistics.

 Viewing Logs in Google Cloud

Option 1: Firebase CLI

firebase functions:log

Option 2: Google Cloud Console

  • 1.Visit Logs Explorer
  • 2.Filter by:
    resource.type=”cloud_function”
    resource.labels.function_name=”sendNotification”
    Use console.log, console.error, and structured logging for better observability.

Conclusion

Deploying Firebase Cloud Functions with Node.js v22 enables:

  • Modern syntax with ES Modules
  • Native web API usage
  • Faster performance

However, check compatibility and Firebase release notes before using v22 in production. Cloud Functions are ideal for scalable and event-driven backends like sending real-time notifications.

For more details, refer to:

  • Firebase Docs
  • Node.js v22 Release Notes
  • Google Cloud Logs Explorer

Recent Blogs


Posted

in

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.