|
| 1 | +# API |
| 2 | + |
| 3 | +## Server Side |
| 4 | + |
| 5 | +### `BaseNotificationChannel` |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +```ts |
| 10 | +export abstract class BaseNotificationChannel<Message = any> { |
| 11 | + constructor(protected app: Application) {} |
| 12 | + abstract send(params: { |
| 13 | + channel: ChannelOptions; |
| 14 | + message: Message; |
| 15 | + }): Promise<{ message: Message; status: 'success' | 'fail'; reason?: string }>; |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +### `PluginNotificationManagerServer` |
| 20 | + |
| 21 | +This server-side plugin serves as a notification management tool, providing methods for registering notification channel types and sending notifications. |
| 22 | + |
| 23 | +#### `registerChannelType()` |
| 24 | + |
| 25 | +This method registers a new channel type on the server side. Example usage is provided below. |
| 26 | + |
| 27 | +```ts |
| 28 | +import PluginNotificationManagerServer from '@nocobase/plugin-notification-manager'; |
| 29 | +import { Plugin } from '@nocobase/server'; |
| 30 | +import { ExampleServer } from './example-server'; |
| 31 | +export class PluginNotificationExampleServer extends Plugin { |
| 32 | + async load() { |
| 33 | + const notificationServer = this.pm.get(PluginNotificationManagerServer) as PluginNotificationManagerServer; |
| 34 | + notificationServer.registerChannelType({ type: 'example-sms', Channel: ExampleServer }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export default PluginNotificationExampleServer; |
| 39 | +``` |
| 40 | + |
| 41 | +##### Signature |
| 42 | + |
| 43 | +`registerChannelType({ type, Channel }: {type: string, Channel: BaseNotificationChannel })` |
| 44 | + |
| 45 | +#### `send()` |
| 46 | + |
| 47 | +The `send` method is used to dispatch notifications via a specified channel. |
| 48 | + |
| 49 | +```ts |
| 50 | +send('in-app-message', |
| 51 | + message: [ |
| 52 | + receivers: [1, 2, 3], |
| 53 | + receiverType: 'userId', |
| 54 | + content: 'In-app message test', |
| 55 | + title: 'In-app message test title' |
| 56 | + ], |
| 57 | + triggerFrom: 'workflow') |
| 58 | + |
| 59 | +send('email', |
| 60 | + message: [ |
| 61 | + |
| 62 | + receiverType: 'email', |
| 63 | + content: 'Email test', |
| 64 | + title: 'Email test title' |
| 65 | + ], |
| 66 | + triggerFrom: 'workflow') |
| 67 | +``` |
| 68 | + |
| 69 | +##### Signature |
| 70 | + |
| 71 | +`send(sendConfig: {channelName: String, message: Object, receivers: ReceiversType, triggerFrom: String })` |
| 72 | + |
| 73 | +The `receivers` field currently supports two formats: NocoBase user IDs`userId` or custom channel configurations`channel-self-defined`. |
| 74 | + |
| 75 | +```ts |
| 76 | +type ReceiversType = |
| 77 | + | { value: number[]; type: 'userId' } |
| 78 | + | { value: any; type: 'channel-self-defined'; channelType: string }; |
| 79 | +``` |
| 80 | + |
| 81 | +##### Detailed Information |
| 82 | + |
| 83 | +`sendConfig` |
| 84 | + |
| 85 | +| Property | Type | Description | |
| 86 | +| -------------- | ---------------- | ------------------------ | |
| 87 | +| `channelName` | `string` | Channel identifier | |
| 88 | +| `message` | `object` | Message object | |
| 89 | +| `receivers` | `ReceiversType` | Recipients | |
| 90 | +| `triggerFrom` | `string` | Source of trigger | |
| 91 | + |
| 92 | +## Client Side |
| 93 | + |
| 94 | +### `PluginNotificationManagerClient` |
| 95 | + |
| 96 | +#### `channelTypes` |
| 97 | + |
| 98 | +The library of registered channel types. |
| 99 | + |
| 100 | +##### Signature |
| 101 | + |
| 102 | +`channelTypes: Registry<registerTypeOptions>` |
| 103 | + |
| 104 | +#### `registerChannelType()` |
| 105 | + |
| 106 | +Registers a client-side channel type. |
| 107 | + |
| 108 | +##### Signature |
| 109 | + |
| 110 | +`registerChannelType(params: registerTypeOptions)` |
| 111 | + |
| 112 | +##### Type |
| 113 | + |
| 114 | +```ts |
| 115 | +type registerTypeOptions = { |
| 116 | + title: string; // Display title for the channel |
| 117 | + type: string; // Channel identifier |
| 118 | + components: { |
| 119 | + ChannelConfigForm?: ComponentType // Channel configuration form component; |
| 120 | + MessageConfigForm?: ComponentType<{ variableOptions: any }> // Message configuration form component; |
| 121 | + ContentConfigForm?: ComponentType<{ variableOptions: any }> // Content configuration form component (for message content only, excluding recipient configuration); |
| 122 | + }; |
| 123 | + meta?: { // Metadata for channel configuration |
| 124 | + createable?: boolean // Whether new channels can be added; |
| 125 | + editable?: boolean // Whether channel configuration is editable; |
| 126 | + deletable?: boolean // Whether channel configuration is deletable; |
| 127 | + }; |
| 128 | +}; |
| 129 | + |
| 130 | +type RegisterChannelType = (params: ChannelType) => void |
| 131 | +``` |
0 commit comments