Bỏ qua, đến nội dung

Tham chiếu API — Notification Manager ​

Gửi thông báo ​

messages:send ​

Gửi thông báo qua kênh đã cấu hình.

POST /api/messages:send

Body:

TrườngKiểuBắt buộcMô tả
channelNamestringCóTên kênh thông báo (đã cấu hình trong Settings)
tostring[]CóDanh sách người nhận (email hoặc userId tuỳ kênh)
subjectstringKhôngTiêu đề (dùng cho email)
contentstring / objectCóNội dung thông báo

Ví dụ gửi email ​

typescript
await api.resource('messages').send({
  values: {
    channelName: 'default-email',
    to: ['user@example.com', 'admin@example.com'],
    subject: 'Thông báo quan trọng',
    content: '<h1>Xin chào</h1><p>Nội dung thông báo HTML...</p>',
  },
});

Ví dụ gửi in-app ​

typescript
await api.resource('messages').send({
  values: {
    channelName: 'in-app-message',
    to: ['userId-123', 'userId-456'],
    title: 'Đơn hàng mới',
    content: {
      text: 'Bạn có đơn hàng mới #1234',
      link: '/orders/1234',
    },
  },
});

Gửi từ server-side code ​

typescript
const notificationManager = this.app.pm.get(
  PluginNotificationManagerServer
) as PluginNotificationManagerServer;

await notificationManager.send({
  channelName: 'default-email',
  to: ['user@example.com'],
  subject: 'Thông báo từ server',
  content: 'Nội dung...',
});

Gửi cho nhiều user (sendToUsers) ​

typescript
await notificationManager.sendToUsers({
  channelName: 'in-app-message',
  userIds: [1, 2, 3],
  title: 'Thông báo chung',
  content: 'Nội dung...',
});

Quản lý kênh thông báo ​

Lấy danh sách kênh ​

typescript
const channels = await api.resource('notificationChannels').list();

Response:

json
{
  "data": [
    {
      "id": 1,
      "name": "default-email",
      "channelType": "email",
      "options": { "host": "smtp.gmail.com", "port": 587 },
      "enabled": true
    }
  ]
}

Tạo kênh mới ​

typescript
await api.resource('notificationChannels').create({
  values: {
    name: 'support-email',
    channelType: 'email',
    options: {
      host: 'smtp.gmail.com',
      port: 587,
      secure: false,
      account: 'support@company.com',
      password: 'app-password',
      from: 'Support <support@company.com>',
    },
    enabled: true,
  },
});

Cập nhật kênh ​

typescript
await api.resource('notificationChannels').update({
  filterByTk: channelId,
  values: {
    enabled: false,
  },
});

Xoá kênh ​

typescript
await api.resource('notificationChannels').destroy({
  filterByTk: channelId,
});

Xem lịch sử gửi (Send Logs) ​

Lấy danh sách log ​

typescript
const logs = await api.resource('notificationSendLogs').list({
  filter: {
    status: 'failed',
  },
  sort: ['-createdAt'],
  pageSize: 50,
});

Cấu trúc Send Log ​

TrườngKiểuMô tả
idbigintID log
channelIdbigintID kênh đã gửi
receiverstringNgười nhận
statusstringTrạng thái: success, failed, pending
contentjsonNội dung đã gửi
errorMessagetextChi tiết lỗi (nếu thất bại)
createdAtdatetimeThời gian gửi

Đăng ký Channel Type (dành cho Plugin Developer) ​

Interface Channel ​

Một channel type cần implement interface sau:

typescript
interface NotificationChannel {
  send(options: SendOptions): Promise<void>;
}

Đăng ký vào Manager ​

typescript
class MyPlugin extends Plugin {
  async load() {
    const nm = this.pm.get(
      PluginNotificationManagerServer
    ) as PluginNotificationManagerServer;

    nm.registerChannelType({
      type: 'sms',
      Channel: MySMSChannel,
    });
  }
}

Mã lỗi (Error Codes) ​

MãMô tả
CHANNEL_NOT_FOUNDKênh với tên channelName không tồn tại
CHANNEL_DISABLEDKênh đã bị tắt (enabled: false)
SEND_FAILEDGửi thất bại — xem errorMessage để biết chi tiết
INVALID_RECEIVERNgười nhận không hợp lệ (email sai format, userId không tồn tại)