Danh Sách Thông Báo
API lấy danh sách thông báo của khách hàng đã đăng nhập.
Hỗ trợ lọc theo loại thông báo, trạng thái đọc, mức độ ưu tiên, phân trang và đếm số thông báo chưa đọc.
GET/POST https://api.socdo.vn/mini-app/v1/get-list-notification
Request Headers
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
Content-Type: application/json
Query Parameters (GET) / Request Body (POST)
| Tham số |
Loại |
Bắt buộc |
Mô tả |
Authorization |
header |
✓ |
JWT token từ API login (Bearer token) |
page |
integer |
- |
Số trang (mặc định: 1) |
limit |
integer |
- |
Số lượng thông báo mỗi trang (mặc định: 20, tối đa: 100) |
type |
string |
- |
Lọc theo loại thông báo: 'order', 'voucher_new', 'voucher_expiring', 'affiliate_order', 'deposit', 'withdrawal' |
is_read |
string |
- |
Lọc theo trạng thái đọc: '0' (chưa đọc), '1' (đã đọc), '' (tất cả) |
priority |
string |
- |
Lọc theo mức độ ưu tiên: 'low', 'medium', 'high' |
Loại Thông Báo (Type)
| Type |
Mô tả |
order | Thông báo về đơn hàng (đặt hàng thành công, hủy đơn, giao hàng, etc.) |
voucher_new | Thông báo voucher mới |
voucher_expiring | Thông báo voucher sắp hết hạn |
affiliate_order | Thông báo đơn hàng affiliate |
deposit | Thông báo nạp tiền |
withdrawal | Thông báo rút tiền |
Response thành công (200)
{
"success": true,
"message": "Lấy danh sách thông báo thành công",
"data": {
"notifications": [
{
"id": 582,
"user_id": 262,
"type": "order",
"title": "Đơn hàng đã được đặt thành công",
"content": "Đơn hàng \"Nước Giặt Xả 6 Trong 1 SPY Plus...\" của bạn đã được đặt thành công. Mã đơn hàng: 239332930278198. Chúng tôi sẽ xử lý đơn hàng của bạn trong thời gian sớm nhất.",
"data": {
"order_id": 1251,
"order_code": "239332930278198",
"product_title": "Nước Giặt Xả 6 Trong 1 SPY Plus Siêu Đậm Đặc Hương Tím, Sạch Sâu, Thơm Lâu, Can 5.2kg",
"product_image": "/uploads/minh-hoa/nuoc-giat-xa-6-trong-1-spy-plus-sieu-dam-dac-huong-tim-sach-sau-thom-lau-can-5-2kg-1755418087.png",
"product_price": 239000,
"status": 0,
"total_amount": 239000
},
"related_id": 1251,
"related_type": "order",
"priority": "medium",
"is_read": false,
"push_sent": true,
"read_at": null,
"created_at": 1762930403,
"updated_at": null
},
{
"id": 581,
"user_id": 37379,
"type": "order",
"title": "Yêu cầu hủy đơn hàng",
"content": "Đơn hàng \"Nước Giặt Xả 6 Trong 1 SPY Plus...\" đã có yêu cầu hủy đơn. Chúng tôi đang xử lý yêu cầu của bạn và sẽ thông báo kết quả sớm nhất.",
"data": {
"order_id": 1251,
"order_code": "239332930278198",
"product_title": "Nước Giặt Xả 6 Trong 1 SPY Plus Siêu Đậm Đặc Hương Tím, Sạch Sâu, Thơm Lâu, Can 5.2kg",
"product_image": "/uploads/minh-hoa/nuoc-giat-xa-6-trong-1-spy-plus-sieu-dam-dac-huong-tim-sach-sau-thom-lau-can-5-2kg-1755418087.png",
"product_price": 239000,
"old_status": 0,
"new_status": 3,
"total_amount": 239000
},
"related_id": 1251,
"related_type": "order",
"priority": "high",
"is_read": false,
"push_sent": true,
"read_at": null,
"created_at": 1762930403,
"updated_at": 1762930404
}
],
"unread_count": 5,
"pagination": {
"current_page": 1,
"total_pages": 3,
"total_records": 50,
"per_page": 20,
"has_next": true,
"has_prev": false
},
"filters": {
"type": "",
"is_read": "",
"priority": ""
}
}
}
Response lỗi (401 - Token không hợp lệ)
{
"success": false,
"message": "Token không hợp lệ - không có user_id"
}
Response lỗi (500 - Lỗi hệ thống)
{
"success": false,
"message": "Lỗi xử lý lấy danh sách thông báo",
"error": "Error message details"
}
Ví dụ JavaScript
// Lấy tất cả thông báo
async function getAllNotifications() {
try {
const authToken = localStorage.getItem('auth_token');
if (!authToken) {
throw new Error('Chưa đăng nhập');
}
const response = await fetch('https://api.socdo.vn/mini-app/v1/get-list-notification', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
});
const result = await response.json();
if (result.success) {
console.log('Danh sách thông báo:', result.data.notifications);
console.log('Số thông báo chưa đọc:', result.data.unread_count);
return result;
} else {
console.error('Lỗi:', result.message);
return result;
}
} catch (error) {
console.error('Lỗi kết nối:', error);
return { success: false, message: 'Lỗi kết nối mạng' };
}
}
// Lấy thông báo chưa đọc
async function getUnreadNotifications() {
try {
const authToken = localStorage.getItem('auth_token');
const response = await fetch('https://api.socdo.vn/mini-app/v1/get-list-notification?is_read=0', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
});
const result = await response.json();
if (result.success) {
console.log('Thông báo chưa đọc:', result.data.notifications);
return result;
}
} catch (error) {
console.error('Lỗi:', error);
}
}
// Lấy thông báo theo loại (ví dụ: order)
async function getNotificationsByType(type) {
try {
const authToken = localStorage.getItem('auth_token');
const response = await fetch(`https://api.socdo.vn/mini-app/v1/get-list-notification?type=${type}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
});
const result = await response.json();
if (result.success) {
console.log(`Thông báo loại ${type}:`, result.data.notifications);
return result;
}
} catch (error) {
console.error('Lỗi:', error);
}
}
// Lấy thông báo với phân trang
async function getNotificationsWithPagination(page = 1, limit = 20) {
try {
const authToken = localStorage.getItem('auth_token');
const response = await fetch(`https://api.socdo.vn/mini-app/v1/get-list-notification?page=${page}&limit=${limit}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
});
const result = await response.json();
if (result.success) {
console.log('Thông báo trang', page, ':', result.data.notifications);
console.log('Tổng số trang:', result.data.pagination.total_pages);
return result;
}
} catch (error) {
console.error('Lỗi:', error);
}
}
// Sử dụng POST method
async function getNotificationsPost() {
try {
const authToken = localStorage.getItem('auth_token');
const response = await fetch('https://api.socdo.vn/mini-app/v1/get-list-notification', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
},
body: JSON.stringify({
page: 1,
limit: 20,
type: 'order',
is_read: '0',
priority: 'high'
})
});
const result = await response.json();
if (result.success) {
console.log('Thông báo:', result.data.notifications);
return result;
}
} catch (error) {
console.error('Lỗi:', error);
}
}
// Hiển thị thông báo lên UI
function displayNotifications(notifications) {
const container = document.getElementById('notifications-container');
container.innerHTML = '';
notifications.forEach(notification => {
const notificationElement = document.createElement('div');
notificationElement.className = `notification-item ${notification.is_read ? 'read' : 'unread'}`;
notificationElement.innerHTML = `
${notification.content}
${formatTimestamp(notification.created_at)}
${!notification.is_read ? 'Mới' : ''}
`;
// Xử lý click để xem chi tiết
notificationElement.addEventListener('click', () => {
handleNotificationClick(notification);
});
container.appendChild(notificationElement);
});
}
// Định dạng timestamp
function formatTimestamp(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleString('vi-VN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
// Xử lý click vào thông báo
function handleNotificationClick(notification) {
// Nếu là thông báo đơn hàng, chuyển đến trang chi tiết đơn hàng
if (notification.type === 'order' && notification.data && notification.data.order_id) {
window.location.href = `/order-detail?id=${notification.data.order_id}`;
}
// Có thể thêm logic đánh dấu đã đọc ở đây
// markAsRead(notification.id);
}
// Gọi khi trang được tải
window.addEventListener('load', function() {
getAllNotifications().then(result => {
if (result.success) {
displayNotifications(result.data.notifications);
// Hiển thị số thông báo chưa đọc
const unreadBadge = document.getElementById('unread-badge');
if (unreadBadge) {
unreadBadge.textContent = result.data.unread_count;
unreadBadge.style.display = result.data.unread_count > 0 ? 'block' : 'none';
}
}
});
});
Lưu ý quan trọng
- User Token Required: API yêu cầu user token từ login (Bearer token trong Authorization header)
- Chỉ lấy thông báo của user: API tự động lọc theo user_id từ token
- Sắp xếp mặc định: Thông báo được sắp xếp theo thời gian tạo (mới nhất trước)
- Unread Count: Số thông báo chưa đọc được tính cho tất cả thông báo, không phụ thuộc vào filter
- Data JSON: Trường
data là JSON object chứa thông tin chi tiết của thông báo (order_id, product_title, etc.)
- Pagination: Hỗ trợ phân trang với page và limit (tối đa 100 mỗi trang)
- Multiple Filters: Có thể kết hợp nhiều filter cùng lúc (type, is_read, priority)
- GET và POST: API hỗ trợ cả GET (query parameters) và POST (request body)
Cấu trúc dữ liệu trả về
| Trường |
Loại |
Mô tả |
id |
integer |
ID duy nhất của thông báo |
user_id |
integer |
ID người dùng nhận thông báo |
type |
string |
Loại thông báo (order, voucher_new, etc.) |
title |
string |
Tiêu đề thông báo |
content |
string |
Nội dung thông báo |
data |
object |
Dữ liệu bổ sung (JSON object) - có thể null |
related_id |
integer/null |
ID đối tượng liên quan (order_id, voucher_id, etc.) |
related_type |
string |
Loại đối tượng liên quan (order, coupon, etc.) |
priority |
string |
Mức độ ưu tiên (low, medium, high) |
is_read |
boolean |
Trạng thái đọc (true: đã đọc, false: chưa đọc) |
push_sent |
boolean |
Đã gửi push notification (true/false) |
read_at |
integer/null |
Timestamp khi đọc thông báo (null nếu chưa đọc) |
created_at |
integer |
Timestamp khi tạo thông báo |
updated_at |
integer/null |
Timestamp khi cập nhật thông báo (null nếu chưa cập nhật) |
Bảo mật
- JWT Verification: Sử dụng JWT để xác thực token
- User Isolation: API chỉ trả về thông báo của user đang đăng nhập (tự động filter theo user_id từ token)
- SQL Injection Protection: Sử dụng prepared statement và mysqli_real_escape_string để bảo vệ
- CORS Headers: Cấu hình CORS để bảo mật cross-origin requests
- Input Validation: Validate và sanitize tất cả input parameters
API Liên Quan
- Login API - Đăng nhập để lấy JWT token
- List Order API - Lấy danh sách đơn hàng
- Get Profile API - Lấy thông tin khách hàng
- Checkout API - Tạo đơn hàng (tự động tạo thông báo)
- Cancel Order API - Hủy đơn hàng (tự động tạo thông báo)