Files
medusa-store/www/apps/resources/app/architectural-modules/notification/send-notification/page.mdx
Shahed Nasser 964927b597 docs: general fixes and improvements (#7918)
* docs improvements and changes

* updated module definition

* modules + dml changes

* fix build

* fix vale error

* fix lint errors

* fixes to stripe docs

* fix condition

* fix condition

* fix module defintion

* fix checkout

* disable UI action

* change oas preview action

* flatten provider module options

* fix lint errors

* add module link docs

* pr comments fixes

* fix vale error

* change node engine version

* links -> linkable

* add note about database name

* small fixes

* link fixes

* fix response code in api reference

* added migrations step
2024-07-04 17:26:03 +03:00

98 lines
2.9 KiB
Plaintext

import { TypeList } from "docs-ui"
export const metadata = {
title: `Send Notification with the Notification Module`,
}
# {metadata.title}
In this guide, you'll learn how to send a notification using the Notification Module.
## Use the Create Method
In your resource, such as a subscriber, resolve the Notification Module's main service and use its `create` method:
export const highlights = [
["12", "notificationModuleService", "Resolve the Notification Module."],
["17", "create", "Create the notification to be sent."],
[
"19",
'"email"',
"Use the module provider defined for the `email` channel to send an email.",
],
[
"20",
'"product-created"',
"The ID of the template defined in the third-party service, such as SendGrid.",
],
[
"21",
"data",
"The data to pass to the template defined in the third-party service.",
],
]
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
import type {
SubscriberArgs,
SubscriberConfig,
} from "@medusajs/medusa"
import { ModuleRegistrationName } from "@medusajs/utils"
import { INotificationModuleService } from "@medusajs/types"
export default async function productCreateHandler({
data,
container,
}: SubscriberArgs<{ id: string }>) {
const notificationModuleService: INotificationModuleService =
container.resolve(ModuleRegistrationName.NOTIFICATION)
await notificationModuleService.create({
to: "shahednasser@gmail.com",
channel: "email",
template: "product-created",
data: "data" in data ? data.data : data,
})
}
export const config: SubscriberConfig = {
event: "product.created",
}
```
The `create` method accepts an object or an array of objects having the following properties:
<TypeList
types={[
{
name: "to",
type: "`string`",
description:
"The destination to send the notification to. When sending an email, it'll be the email address. When sending an SMS, it'll be the phone number.",
optional: false,
},
{
name: "channel",
type: "`string`",
description:
"The channel to send the notification through. For example, `email` or `sms`. The module provider defined for that channel will be used to send the notification.",
optional: false,
},
{
name: "template",
type: "`string`",
description:
"The ID of the template used for the notification. This is useful for providers like SendGrid, where you define templates within SendGrid and use their IDs here.",
optional: false,
},
{
name: "data",
type: "`Record<string, unknown>`",
description: "The data to pass along to the template, if necessary.",
},
]}
sectionTitle="Use the Create Method"
/>
For a full list of properties accepted, refer to [this guide](/references/notification-provider-module#create).