Local Notification Module Provider
The Local Notification Module Provider simulates sending a notification, but only logs the notification's details in the terminal. This is useful for development.
On Cloud, Medusa configures Medusa Emails, a managed email service, for the email channel by default, which is more suitable for production.
email channel only in local development, as explained in the Medusa Emails Cloud guide.Register the Local Notification Module#
feed channel.Add the module into the providers array of the Notification Module:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
// ...
{
resolve: "@medusajs/medusa/notification-local",
id: "local",
options: {
channels: ["email"],
},
},
],
},
},
],
})
Local Notification Module Options#
Option | Description |
|---|---|
| The channels this notification module is used to send notifications for. While the local notification module doesn't actually send the notification, it's important to specify its channels to make sure it's used when a notification for that channel is created. |
Send Notifications to the Admin Notification Panel#
The Local Notification Module Provider can also be used to send notifications to the Medusa Admin's notification panel. You can send notifications to the admin dashboard when a certain action occurs using a subscriber, a custom workflow or a workflow hook.
For example, to send an admin notification whenever an order is placed, create a subscriber at src/subscribers/order-placed.ts with the following content:
import { Modules } from "@medusajs/framework/utils"
export default async function orderPlacedHandler({
event: { data },
container,
}: SubscriberArgs<{ id: string }>) {
const notificationModuleService = container.resolve(Modules.NOTIFICATION)
await notificationModuleService.createNotifications({
to: "",
channel: "feed",
template: "admin-ui",
data: {
title: "New order",
description: `A new order has been placed`,
},
})
}
export const config: SubscriberConfig = {
event: "order.placed",
}
In this subscriber, you:
- Resolve the Notification Module's main service from the Medusa container.
- Use the
createNotificationsmethod of the Notification Module's main service to create a notification to be sent to the admin dashboard. By specifying thefeedchannel, the Local Notification Module Provider is used to send the notification. - The
toproperty sets which admin users see the notification. Set it to an empty string to show the notification to all admin users, or to an admin user's ID to show it only to that user. Learn more in the Send Notifications to a Specific Admin User section. - The
templateproperty of thecreateNotificationsmethod's parameter must be set toadmin-ui. - The
dataproperty allows you to customize the content of the admin notification. It must containtitleanddescriptionproperties.
data property of every notification, and the List Notifications and Get Notification API routes return it. Any admin user with the notification:read RBAC policy can view it, so don't pass data that those users shouldn't see.Send Notifications to a Specific Admin User#
The List Notifications and Get Notification API routes only return a feed notification to an admin user if the notification's to property is either:
- An empty string, which shows the notification to all admin users.
- The ID of the authenticated admin user.
Notifications on other channels aren't affected by this scoping.
So, to show an admin notification to a single admin user, set the to property to that user's ID. For example:
await notificationModuleService.createNotifications({
to: userId,
channel: "feed",
template: "admin-ui",
data: {
title: "Export ready",
description: "Your export is ready to download",
},
})
Where userId is the ID of the admin user. In an API route, you can get the ID of the authenticated admin user from req.auth_context.actor_id.
Medusa's product import and export, order export, and inventory item export features use this to send their feed notifications only to the admin user who started the import or export. If you execute their workflows, such as exportProductsWorkflow or importProductsWorkflow, pass the admin user's ID in the workflow's to input. If you don't pass to, the notifications are shown to all admin users.
Test Sending Notification#
To test this out, start the Medusa application:
Then, place an order. The subscriber will run, sending a notification to the Medusa Admin's notification panel.