Spaces:
Sleeping
Sleeping
MisbahKhan0009
feat: Add script for sending test push notifications and Firebase configuration
632173a | /** | |
| * Quick script to send a test push notification directly via Firebase Admin SDK. | |
| * | |
| * Usage: | |
| * 1. Set FIREBASE_SERVICE_ACCOUNT_PATH or FIREBASE_SERVICE_ACCOUNT_JSON in your .env | |
| * (or export GOOGLE_APPLICATION_CREDENTIALS=/path/to/serviceAccount.json) | |
| * 2. Run: | |
| * node backend/scripts/send_test_notification.js <FCM_TOKEN> "Title" "Body" | |
| * | |
| * Example: | |
| * node backend/scripts/send_test_notification.js "eA1b2c3..." "Hello" "Push is working!" | |
| */ | |
| const path = require('path'); | |
| const admin = require('firebase-admin'); | |
| const dotenv = require('dotenv'); | |
| dotenv.config({ path: path.resolve(__dirname, '..', '.env') }); | |
| const token = process.argv[2]; | |
| const title = process.argv[3] || 'Care People'; | |
| const body = process.argv[4] || 'Push notification is working!'; | |
| if (!token) { | |
| console.error('Usage: node send_test_notification.js <FCM_TOKEN> [title] [body]'); | |
| process.exit(1); | |
| } | |
| function initFirebase() { | |
| if (process.env.FIREBASE_SERVICE_ACCOUNT_JSON) { | |
| const serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT_JSON); | |
| admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); | |
| return; | |
| } | |
| if (process.env.FIREBASE_SERVICE_ACCOUNT_PATH) { | |
| const fullPath = path.resolve(__dirname, '..', process.env.FIREBASE_SERVICE_ACCOUNT_PATH); | |
| const serviceAccount = require(fullPath); | |
| admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); | |
| return; | |
| } | |
| // Falls back to GOOGLE_APPLICATION_CREDENTIALS env var | |
| admin.initializeApp({ credential: admin.credential.applicationDefault() }); | |
| } | |
| async function sendNotification() { | |
| initFirebase(); | |
| const message = { | |
| token, | |
| notification: { title, body }, | |
| android: { | |
| priority: 'high', | |
| notification: { | |
| channelId: 'care_people_general', | |
| clickAction: 'FLUTTER_NOTIFICATION_CLICK', | |
| }, | |
| }, | |
| }; | |
| try { | |
| const response = await admin.messaging().send(message); | |
| console.log('✅ Notification sent successfully!'); | |
| console.log('Message ID:', response); | |
| } catch (err) { | |
| console.error('❌ Failed to send notification:', err.message); | |
| process.exit(1); | |
| } | |
| } | |
| sendNotification(); | |