|
1
|
import { Table } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Local Notification Module Provider`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
The Local Notification Module Provider simulates sending a notification, but only logs the notification's details in the terminal. This is useful for development. |
|
10
|
|
|
11
|
--- |
|
12
|
|
|
13
|
## Register the Local Notification Module |
|
14
|
|
|
15
|
<Note> |
|
16
|
|
|
17
|
The Local Notification Module Provider is registered by default in your application. It's configured to run on the `feed` channel. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Add the module into the `providers` array of the Notification Module: |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
Only one provider can be defined for a channel. |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
```ts title="medusa-config.ts" |
|
30
|
module.exports = defineConfig({ |
|
31
|
// ... |
|
32
|
modules: [ |
|
33
|
{ |
|
34
|
resolve: "@medusajs/medusa/notification", |
|
35
|
options: { |
|
36
|
providers: [ |
|
37
|
// ... |
|
38
|
{ |
|
39
|
resolve: "@medusajs/medusa/notification-local", |
|
40
|
id: "local", |
|
41
|
options: { |
|
42
|
channels: ["email"], |
|
43
|
}, |
|
44
|
}, |
|
45
|
], |
|
46
|
}, |
|
47
|
}, |
|
48
|
], |
|
49
|
}) |
|
50
|
``` |
|
51
|
|
|
52
|
### Local Notification Module Options |
|
53
|
|
|
54
|
<Table> |
|
55
|
<Table.Header> |
|
56
|
<Table.Row> |
|
57
|
<Table.HeaderCell> |
|
58
|
Option |
|
59
|
</Table.HeaderCell> |
|
60
|
<Table.HeaderCell> |
|
61
|
Description |
|
62
|
</Table.HeaderCell> |
|
63
|
</Table.Row> |
|
64
|
</Table.Header> |
|
65
|
<Table.Body> |
|
66
|
<Table.Row> |
|
67
|
<Table.Cell> |
|
68
|
|
|
69
|
`channels` |
|
70
|
|
|
71
|
</Table.Cell> |
|
72
|
<Table.Cell> |
|
73
|
|
|
74
|
The channels this notification module is used to send notifications for. While the local notification module doesn't actually send the notification, |
|
75
|
it's important to specify its channels to make sure it's used when a notification for that channel is created. |
|
76
|
|
|
77
|
</Table.Cell> |
|
78
|
</Table.Row> |
|
79
|
</Table.Body> |
|
80
|
</Table> |
|
81
|
|
|
82
|
--- |
|
83
|
|
|
84
|
## Send Notifications to the Admin Notification Panel |
|
85
|
|
|
86
|
The Local Notification Module Provider can also be used to send notifications to the [Medusa Admin's notification panel](!user-guide!#check-notifications). |
|
87
|
You can send notifications to the admin dashboard when a certain action occurs using a subscriber, a custom workflow or a workflow hook. |
|
88
|
|
|
89
|
For example, to send an admin notification whenever an order is placed, create a [subscriber](!docs!/learn/fundamentals/events-and-subscribers) at `src/subscribers/order-placed.ts` with the following content: |
|
90
|
|
|
91
|
export const highlights = [ |
|
92
|
["11", "notificationModuleService", "Resolve the Notification Module."], |
|
93
|
["13", "createNotifications", "Create the notification to be sent."], |
|
94
|
[ |
|
95
|
"15", |
|
96
|
'"feed"', |
|
97
|
"By specifying the `feed` channel, the Local Notification Module Provider will be used to send the notification.", |
|
98
|
], |
|
99
|
["16", '"admin-ui"', "The ID of the template used for admin dashboard notifications"], |
|
100
|
["17", "data", "The data for the notification. It must contain `title` and `description` properties."], |
|
101
|
] |
|
102
|
|
|
103
|
```ts title="src/subscribers/order-placed.ts" highlights={highlights} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
104
|
import type { |
|
105
|
SubscriberArgs, |
|
106
|
SubscriberConfig, |
|
107
|
} from "@medusajs/framework" |
|
108
|
import { Modules } from "@medusajs/framework/utils" |
|
109
|
|
|
110
|
export default async function orderPlacedHandler({ |
|
111
|
event: { data }, |
|
112
|
container, |
|
113
|
}: SubscriberArgs<{ id: string }>) { |
|
114
|
const notificationModuleService = container.resolve(Modules.NOTIFICATION) |
|
115
|
|
|
116
|
await notificationModuleService.createNotifications({ |
|
117
|
to: "", |
|
118
|
channel: "feed", |
|
119
|
template: "admin-ui", |
|
120
|
data: { |
|
121
|
title: "New order", |
|
122
|
description: `A new order has been placed`, |
|
123
|
}, |
|
124
|
}) |
|
125
|
} |
|
126
|
|
|
127
|
export const config: SubscriberConfig = { |
|
128
|
event: "order.placed", |
|
129
|
} |
|
130
|
``` |
|
131
|
|
|
132
|
In this subscriber, you: |
|
133
|
|
|
134
|
- Resolve the Notification Module's main service from the [Medusa container](!docs!/learn/fundamentals/medusa-container). |
|
135
|
- Use the `createNotifications` method of the Notification Module's main service to create a notification to be sent to the admin dashboard. By specifying the `feed` channel, the Local Notification Module Provider is used to send the notification. |
|
136
|
- The `template` property of the `createNotifications` method's parameter must be set to `admin-ui`. |
|
137
|
- The `data` property allows you to customize the content of the admin notification. It must contain `title` and `description` properties. |
|
138
|
|
|
139
|
### Test Sending Notification |
|
140
|
|
|
141
|
To test this out, start the Medusa application: |
|
142
|
|
|
143
|
```bash npm2yarn |
|
144
|
npm run dev |
|
145
|
``` |
|
146
|
|
|
147
|
Then, place an order. The subscriber will run, sending a notification to the Medusa Admin's notification panel. |
|
148
|
|