logologo
Get Started
Guide
Development
Plugins
API
Home
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
Italiano
Türkçe
Українська
Tiếng Việt
Bahasa Indonesia
ไทย
Polski
Nederlands
Čeština
العربية
עברית
हिन्दी
Svenska
Get Started
Guide
Development
Plugins
API
Home
logologo
Overview

Notification channel

In-app message
Email

Development

Extend channel type
API
Previous PageExtend channel type

#API Reference

#Server Side

#BaseNotificationChannel

This abstract class represents a base for different types of notification channels, defining essential interfaces for channel implementation. To add a new notification channel, you must extend this class and implement its methods.

export abstract class BaseNotificationChannel<Message = any> {
  constructor(protected app: Application) {}
  abstract send(params: {
    channel: ChannelOptions;
    message: Message;
  }): Promise<{
    message: Message;
    status: 'success' | 'fail';
    reason?: string;
  }>;
}

#PluginNotificationManagerServer

This server-side plugin serves as a notification management tool, providing methods for registering notification channel types and sending notifications.

#registerChannelType()

This method registers a new channel type on the server side. Example usage is provided below.

import PluginNotificationManagerServer from '@nocobase/plugin-notification-manager';
import { Plugin } from '@nocobase/server';
import { ExampleServer } from './example-server';
export class PluginNotificationExampleServer extends Plugin {
  async load() {
    const notificationServer = this.pm.get(
      PluginNotificationManagerServer,
    ) as PluginNotificationManagerServer;
    notificationServer.registerChannelType({
      type: 'example-sms',
      Channel: ExampleServer,
    });
  }
}

export default PluginNotificationExampleServer;
#Signature

registerChannelType({ type, Channel }: {type: string, Channel: BaseNotificationChannel })

#send()

The send method is used to dispatch notifications via a specified channel.

// In-app message
send({
  channelName: 'in-app-message',
  message: {
    title: 'In-app message test title',
    content: 'In-app message test'
  },
  receivers: {
    type: 'userId',
    value: [1, 2, 3]
  },
  triggerFrom: 'workflow'
});

// Email
send({
  channelName: 'email',
  message: {
    title: 'Email test title',
    content: 'Email test'
  },
  receivers: {
    type: 'channel-self-defined',
    channelType: 'email',
    value: ['a@example.com', 'b@example.com']
  },
  triggerFrom: 'workflow'
});
#Signature

send(sendConfig: {channelName: String, message: Object, receivers: ReceiversType, triggerFrom: String })

The receivers field currently supports two formats: NocoBase user IDs userId or custom channel configurations channel-self-defined.

type ReceiversType =
  | { value: number[]; type: 'userId' }
  | { value: any; type: 'channel-self-defined'; channelType: string };
#Detailed Information

sendConfig

PropertyTypeDescription
channelNamestringChannel identifier
messageobjectMessage object
receiversReceiversTypeRecipients
triggerFromstringSource of trigger

#Client Side

#PluginNotificationManagerClient

#channelTypes

The library of registered channel types.

#Signature

channelTypes: Registry<registerTypeOptions>

#registerChannelType()

Registers a client-side channel type.

#Signature

registerChannelType(params: registerTypeOptions)

#Type
type registerTypeOptions = {
  title: string; // Display title for the channel
  type: string; // Channel identifier
  components: {
    ChannelConfigForm?: ComponentType; // Channel configuration form component;
    MessageConfigForm?: ComponentType<{ variableOptions: any }>; // Message configuration form component;
    ContentConfigForm?: ComponentType<{ variableOptions: any }>; // Content configuration form component (for message content only, excluding recipient configuration);
  };
  meta?: {
    // Metadata for channel configuration
    createable?: boolean; // Whether new channels can be added;
    editable?: boolean; // Whether channel configuration is editable;
    deletable?: boolean; // Whether channel configuration is deletable;
  };
};

type RegisterChannelType = (params: ChannelType) => void;