Skip to content

Mark Notification as Read (Admin)

Mark a specific notification as read.

Endpoint

POST /v1/chat/admin/notifications/read/

Authentication

Required: Admin authentication token

Authorization: Token <admin_token>

Request

Headers

Content-Type: application/json
Authorization: Token <admin_token>

Body

{
  "id": 45
}

Request Fields

Field Type Required Description
id integer Yes ID of the notification to mark as read

Response

Success (200 OK)

{
  "success": true,
  "message": "Notification marked as read"
}

Error (400 Bad Request)

Missing id:

{
  "success": false,
  "message": "id is required"
}

Invalid id:

{
  "success": false,
  "message": "Invalid notification id"
}

Error (404 Not Found)

Notification doesn't exist or doesn't belong to you:

{
  "success": false,
  "message": "Notification not found"
}

Error (401 Unauthorized)

{
  "success": false,
  "message": "Unauthorized"
}

Error (403 Forbidden)

{
  "success": false,
  "message": "Access denied. Admin role required."
}

Example Usage

JavaScript/Fetch

async function markNotificationRead(notificationId) {
  const response = await fetch('/v1/chat/admin/notifications/read/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Token ${adminToken}`
    },
    body: JSON.stringify({
      id: notificationId
    })
  });

  const data = await response.json();

  if (data.success) {
    console.log('Notification marked as read');
    // Update UI - decrement badge count
    updateNotificationBadge();
  }
}

Axios

import axios from 'axios';

const markNotificationRead = async (notificationId) => {
  try {
    await axios.post('/v1/chat/admin/notifications/read/', 
      { id: notificationId },
      {
        headers: {
          'Authorization': `Token ${adminToken}`
        }
      }
    );

    console.log('Notification marked as read');
  } catch (error) {
    console.error('Failed to mark notification as read:', error);
  }
};

cURL

curl -X POST "http://localhost:8000/v1/chat/admin/notifications/read/" \
  -H "Content-Type: application/json" \
  -H "Authorization: Token your_admin_token_here" \
  -d '{
    "id": 45
  }'

Usage in UI

React Component

import { useState } from 'react';
import axios from 'axios';

function NotificationItem({ notification, onRead }) {
  const [isRead, setIsRead] = useState(notification.is_read);

  const handleClick = async () => {
    // Navigate to chat session
    navigate(`/dashboard/messages?session=${notification.session_id}`);

    // Mark as read
    if (!isRead) {
      try {
        await axios.post('/v1/chat/admin/notifications/read/', 
          { id: notification.id },
          { headers: { 'Authorization': `Token ${adminToken}` } }
        );

        setIsRead(true);
        onRead(notification.id); // Update parent component
      } catch (error) {
        console.error('Failed to mark as read:', error);
      }
    }
  };

  return (
    <div 
      className={`notification ${isRead ? 'read' : 'unread'}`}
      onClick={handleClick}
    >
      <img src={notification.store_logo} alt={notification.store_name} />
      <div>
        <strong>{notification.store_name}</strong>
        <p>{notification.content}</p>
        <span>{formatTime(notification.created_at)}</span>
      </div>
    </div>
  );
}

Batch Mark as Read

async function markMultipleNotificationsRead(notificationIds) {
  const promises = notificationIds.map(id =>
    axios.post('/v1/chat/admin/notifications/read/', 
      { id },
      { headers: { 'Authorization': `Token ${adminToken}` } }
    )
  );

  try {
    await Promise.all(promises);
    console.log('All notifications marked as read');
  } catch (error) {
    console.error('Some notifications failed to mark as read:', error);
  }
}

Notes

  • This endpoint marks a single notification as read
  • The notification's is_read field is set to true
  • The notification remains in the database but won't count towards unread count
  • You can only mark your own notifications as read (checked by admin_id)
  • To mark all notifications as read at once, use Mark All Notifications Read

Typical Workflow

  1. User clicks on a notification
  2. Navigate to the chat session
  3. Call this endpoint to mark the notification as read
  4. Update the notification badge count
  5. Remove the unread indicator from the notification UI

When to Call This Endpoint

  • When user clicks on a notification
  • When user views the chat session from a notification
  • When notification is displayed in a focused state

Performance Considerations

  • For marking many notifications, consider using Mark All Notifications Read
  • Batch requests when marking multiple notifications (use Promise.all)
  • Update UI optimistically (don't wait for server response)