[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/notifications](../README) / Notification

# Abstract Class: Notification

Defined in: [notifications/src/Notification.ts:45](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L45)

Base class for all notifications.

Extend this class, declare which channels() to use, then implement
the corresponding to*() method for each declared channel.

Supported channels:
  'mail'     — implement toMail()     → returns a MailMessage
  'database' — implement toDatabase() → returns a plain object
  'slack'    — implement toSlack()    → returns a SlackMessage
  'sms'      — implement toSms()      → returns an SmsMessage

## Example

```ts
export class OrderShippedNotification extends Notification {
  constructor(private order: Order) { super(); }

  channels() { return ['mail', 'slack', 'database']; }

  toMail(notifiable: Notifiable): MailMessage {
    return new MailMessage()
      .subject(`Order #${this.order.id} shipped`)
      .line('Your order is on its way.')
      .action('Track', `https://app.test/orders/${this.order.id}`);
  }

  toSlack(_notifiable: Notifiable): SlackMessage | Promise<SlackMessage> {
    return {
      webhookUrl: 'https://hooks.slack.com/services/...',
      text:       `Order #${this.order.id} was shipped!`,
    };
  }

  toDatabase() {
    return { orderId: this.order.id, status: 'shipped' };
  }
}
```

## Constructors

### Constructor

> **new Notification**(): `Notification`

#### Returns

`Notification`

## Methods

### channels()

> `abstract` **channels**(`notifiable?`): `string`[]

Defined in: [notifications/src/Notification.ts:58](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L58)

Declare which channels to deliver on: `'mail'`, `'database'`, `'slack'`,
`'sms'`, `'broadcast'`, or any channel registered with `extend()`.

The recipient is passed in, so routing can follow their preferences.
Ignore the parameter when every recipient gets the same channels.

#### Parameters

##### notifiable?

`Notifiable`

#### Returns

`string`[]

#### Example

```ts
channels(user: Notifiable) {
  return user.wantsSms ? ["database", "sms"] : ["database", "mail"];
}
```

***

### toMail()

> **toMail**(`_notifiable`): [`MailMessage`](MailMessage) \| `Promise`\<[`MailMessage`](MailMessage)\>

Defined in: [notifications/src/Notification.ts:60](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L60)

#### Parameters

##### \_notifiable

`Notifiable`

#### Returns

[`MailMessage`](MailMessage) \| `Promise`\<[`MailMessage`](MailMessage)\>

***

### toDatabase()

> **toDatabase**(`_notifiable`): `Record`\<`string`, `unknown`\> \| `Promise`\<`Record`\<`string`, `unknown`\>\>

Defined in: [notifications/src/Notification.ts:64](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L64)

#### Parameters

##### \_notifiable

`Notifiable`

#### Returns

`Record`\<`string`, `unknown`\> \| `Promise`\<`Record`\<`string`, `unknown`\>\>

***

### toSlack()

> **toSlack**(`_notifiable`): [`SlackMessage`](../interfaces/SlackMessage) \| `Promise`\<[`SlackMessage`](../interfaces/SlackMessage)\>

Defined in: [notifications/src/Notification.ts:68](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L68)

#### Parameters

##### \_notifiable

`Notifiable`

#### Returns

[`SlackMessage`](../interfaces/SlackMessage) \| `Promise`\<[`SlackMessage`](../interfaces/SlackMessage)\>

***

### toSms()

> **toSms**(`_notifiable`): [`SmsMessage`](../interfaces/SmsMessage) \| `Promise`\<[`SmsMessage`](../interfaces/SmsMessage)\>

Defined in: [notifications/src/Notification.ts:72](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L72)

#### Parameters

##### \_notifiable

`Notifiable`

#### Returns

[`SmsMessage`](../interfaces/SmsMessage) \| `Promise`\<[`SmsMessage`](../interfaces/SmsMessage)\>

***

### toBroadcast()

> **toBroadcast**(`_notifiable`): `Record`\<`string`, `unknown`\> \| [`BroadcastMessage`](BroadcastMessage) \| `Promise`\<`Record`\<`string`, `unknown`\> \| [`BroadcastMessage`](BroadcastMessage)\>

Defined in: [notifications/src/Notification.ts:80](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L80)

The real-time representation for the 'broadcast' channel. Return a `BroadcastMessage`
(or a plain data object). Required when `channels()` includes `'broadcast'`.

#### Parameters

##### \_notifiable

`Notifiable`

#### Returns

`Record`\<`string`, `unknown`\> \| [`BroadcastMessage`](BroadcastMessage) \| `Promise`\<`Record`\<`string`, `unknown`\> \| [`BroadcastMessage`](BroadcastMessage)\>

***

### broadcastType()

> **broadcastType**(): `string`

Defined in: [notifications/src/Notification.ts:90](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L90)

The wire `type` of a broadcast notification (lets clients distinguish kinds). Default: class name.

#### Returns

`string`

***

### payload()

> **payload**(): `Record`\<`string`, `unknown`\>

Defined in: [notifications/src/Notification.ts:111](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/Notification.ts#L111)

Serialize this notification's state so it can be queued.

The default copies own enumerable fields, which covers the usual case of a
constructor assigning plain values. Override it when the notification holds
something JSON cannot carry — a model instance, a Date-keyed Map, a closure
— and pair the override with a matching `static fromPayload()`.

#### Returns

`Record`\<`string`, `unknown`\>

#### Example

```ts
override payload() {
  return { orderId: this.order.id };
}

static override async fromPayload(data: Record<string, unknown>) {
  return new OrderShipped(await Order.find(data.orderId as number));
}
```
