Firebase FCM Notification Node.js સાથે કેવી રીતે સેટઅપ કરવું? (ગુજરાતી માર્ગદર્શિકા)

🔥 Firebase FCM Notification Node.js Setup Gujarati

આ બ્લોગમાં આપણે Firebase Cloud Messaging (FCM) ને Node.js સાથે કેવી રીતે સેટઅપ કરવું તે શીખીશું. FCM દ્વારા તમે તમારા Mobile App અથવા Web App પર Push Notifications મોકલી શકો છો.


Firebase FCM Notification Node.js

1️⃣ Firebase Console માં પ્રોજેક્ટ બનાવો

  1. Firebase Console ખોલો.
  2. Add Project પર ક્લિક કરો.
  3. પ્રોજેક્ટનું નામ નાખો → Continue.
  4. Google Analytics Disable કરી શકો (જરૂર ન હોય તો).
  5. Create Project પર ક્લિક કરો.

2️⃣ FCM Service Enable કરો

  1. Project SettingsCloud Messaging ખોલો.
  2. ત્યાંથી Server Key અને Sender ID સેવ કરો.

3️⃣ Service Account Key ડાઉનલોડ કરો

  1. Project SettingsService Accounts પર જાઓ.
  2. Generate new private key પર ક્લિક કરો.
  3. JSON ફાઇલ ડાઉનલોડ કરો અને પ્રોજેક્ટ ડિરેક્ટરીમાં firebase-service-account.json નામથી સેવ કરો.

4️⃣ Node.js પ્રોજેક્ટ સેટઅપ

Step 1: નવો પ્રોજેક્ટ બનાવો

mkdir fcm-nodejs
cd fcm-nodejs
npm init -y

Step 2: Firebase Admin SDK ઇન્સ્ટોલ કરો

npm install firebase-admin

5️⃣ Push Notification મોકલવાનું કોડ

index.js ફાઇલ બનાવો:

const admin = require("firebase-admin");
const serviceAccount = require("./firebase-service-account.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});

const message = {
notification: {
title: "નવું અપડેટ 🚀",
body: "તમારા માટે નવો મેસેજ આવ્યો છે!"
},
token: "YOUR_DEVICE_FCM_TOKEN"
};

admin.messaging().send(message)
.then((response) => {
console.log("✅ નોટિફિકેશન મોકલાઈ ગઈ:", response);
})
.catch((error) => {
console.error("❌ ભૂલ આવી:", error);
});

6️⃣ Device FCM Token મેળવવો

  • Web App માટે: Firebase SDK દ્વારા બ્રાઉઝરમાંથી ટોકન મેળવો.
  • Mobile App માટે: Android/iOS Firebase SDK દ્વારા ટોકન કલેક્શન API બનાવો.

7️⃣ Test કરો

Editnode index.js

જો બધું ઠીક હશે તો તમારા ડિવાઇસ પર નોટિફિકેશન આવશે ✅


📌 Pro Tips for Firebase FCM

  • sendMulticast() થી એક સાથે ઘણા ડિવાઇસ પર નોટિફિકેશન મોકલો.
  • data payload મોકલી કસ્ટમ એક્શન હેન્ડલ કરો.
  • Production માં error handling અને retry logic ઉમેરો.

🔍 Conclusion

Firebase FCM તમને તમારા એપ યૂઝર્સ સાથે real-time માં કનેક્ટ થવા દે છે. Node.js સાથે તેનો ઉપયોગ કરવો ખૂબ સરળ છે. આ સ્ટેપ્સ ફોલો કરીને તમે તમારા પ્રોજેક્ટમાં Push Notifications સરળતાથી ઉમેરશો.

Leave a Reply

Your email address will not be published. Required fields are marked *