Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
mudeerapi.abasa.com
/
nodetest
/
Filename :
prompt.md
back
Copy
# Firebase Push Notifications Integration Guide ## Overview This guide explains how to integrate Firebase Cloud Messaging (FCM) push notifications with your Node.js backend. The system automatically sends login notifications to admins/super_admins when employees/department_heads log in, and provides manual notification sending capabilities. ## Backend Setup Complete ✅ The following backend components have been implemented: ### 1. Database Schema Updates - **User Model**: Added `fcmToken` field to store device FCM tokens - **Notification Templates**: Created reusable templates for different notification types ### 2. Firebase Service (`/src/services/firebase.js`) Core functions available: ```javascript sendNotification(fcmToken, title, body, data) sendNotificationToMultipleUsers(fcmTokens, title, body, data) sendLoginNotificationToAdmins(employeeName, role, department, loginTime) ``` ### 3. API Routes Available #### User Routes (`/user/...`) - **PUT** `/user/fcm-token` - Update user's FCM token - **Auth**: Required (Bearer token) - **Body**: `{ fcmToken: string }` - **Response**: `{ message: "FCM token updated successfully", user: {...} }` #### Notification Routes (`/notification/...`) - **POST** `/notification/send` - Send notification to single user - **Auth**: Admin/Super Admin/Department Head only - **Body**: `{ userId, title, body, data? }` - **Response**: `{ message: "Notification sent successfully", result: {...} }` - **POST** `/notification/send-multiple` - Send to multiple users - **Auth**: Admin/Super Admin only - **Body**: `{ userIds: [string], title, body, data? }` - **Response**: `{ message: "Notifications sent successfully", sentTo: number }` - **POST** `/notification/send-department` - Send to entire department - **Auth**: Admin/Super Admin/Department Head only (dept heads can only send to their own dept) - **Body**: `{ departmentId, title, body, data? }` - **Response**: `{ message: "Department notifications sent successfully", sentTo: number }` ### 4. Automatic Login Notifications - **Trigger**: When employees/department_heads login successfully - **Target**: All admins/super_admins with registered FCM tokens - **Data Structure**: ```javascript { title: "Employee Login Alert" | "Department Head Login Alert", body: "{Name} from {Department} has logged in at {Time}", data: { type: 'login_notification', employeeName: string, employeeRole: 'employee' | 'department_head', department: string | null, loginTime: string, timestamp: ISO string } } ``` ### 5. Environment Configuration Required ```env FIREBASE_SERVICE_ACCOUNT_KEY={"type":"service_account","project_id":"...","private_key":"..."} ``` --- ## Frontend Integration Requirements ### Step 1: Firebase Setup You need to: 1. Install Firebase SDK for your platform (React/Vue/React Native/Flutter) 2. Configure Firebase with your project credentials 3. Set up service worker for background notifications (web) 4. Request notification permissions from users ### Step 2: FCM Token Management **On user login/app initialization:** 1. Request notification permission from user 2. Get FCM token from Firebase SDK 3. Send token to backend via `PUT /user/fcm-token` endpoint 4. Store token locally for future reference ### Step 3: Real-time Notification Listening **For Admins/Super Admins Dashboard:** You should implement a real-time listener that: 1. Listens for incoming FCM messages when app is in foreground 2. Filters for `login_notification` type messages 3. Displays login activities in a dedicated panel/sidebar 4. Shows employee name, role, department, and login time 5. Maintains a list of recent login activities (last 10-20) 6. Provides visual/audio alerts for new logins **Expected UI Components:** - **Login Activity Panel**: Shows recent employee logins - **Live Notification Toast**: Popup notification for immediate alerts - **Notification Badge**: Counter for unread notifications - **Notification History**: Expandable list of past notifications ### Step 4: Manual Notification Sending Interface **For Admins/Super Admins:** Create UI components that allow: #### Send to Individual User - User selection dropdown/autocomplete - Title and message input fields - Send button that calls `POST /notification/send` #### Send to Department - Department selection dropdown - Title and message input fields - Send button that calls `POST /notification/send-department` - **Note**: Department heads can only send to their own department #### Send to Multiple Users - Multi-select user interface (checkboxes/tags) - Bulk selection options (all employees, all dept heads, etc.) - Title and message input fields - Send button that calls `POST /notification/send-multiple` ### Step 5: Background Notification Handling Implement background message handling for: - Notifications when app is closed/minimized - Badge updates on app icon - Notification click handling to open relevant app sections - Sound/vibration alerts based on notification priority ### Step 6: Permission Levels Implementation Ensure your frontend respects role-based access: **Super Admin & Admin:** - Can access all manual notification sending features - Can see login notifications from all employees/dept heads - Can send to any user/department **Department Head:** - Can see login notifications from all employees/dept heads - Can only send manual notifications to their own department - Limited access to user selection (only their department) **Employee:** - Cannot send manual notifications - Cannot see login activity dashboard - Only receives notifications targeted to them ### Step 7: Error Handling Handle various scenarios: - FCM token registration failures - Network errors during notification sending - Permission denied by user - Invalid user/department selections - Rate limiting from Firebase ### Step 8: UI/UX Considerations **Login Activity Display:** - Real-time updates without page refresh - Clean, readable list format - Color coding for different roles (employee vs dept head) - Time formatting (relative time: "2 minutes ago") - Department badges/tags - Search/filter functionality for large lists **Manual Notification Interface:** - Form validation for required fields - Character limits for title/message - Preview of notification before sending - Confirmation dialogs for bulk sends - Success/error feedback messages - Send history/logs **Notification Preferences:** - Allow users to customize notification types - Sound/vibration settings - Do not disturb hours - Notification frequency limits ## Expected User Flows ### Admin Login Activity Monitoring: 1. Admin logs into dashboard 2. FCM token automatically registered with backend 3. Real-time listener activated for login notifications 4. When employee logs in → Admin receives instant notification 5. Notification appears as toast + added to activity panel 6. Admin can view detailed login history and patterns ### Manual Notification Sending: 1. Admin navigates to notification sending interface 2. Selects target (individual/department/multiple users) 3. Composes notification title and message 4. Reviews and sends notification 5. Receives confirmation of successful delivery 6. Target users receive notification on their devices This implementation provides a complete notification system that enhances real-time monitoring capabilities while maintaining proper access control and user experience standards.