Send Notification with the Notification Module
In this guide, you'll learn about the different ways to send notifications using the Notification Module.
Using the Create Method#
In your resource, such as a subscriber, resolve the Notification Module's main service and use its create method:
6import { INotificationModuleService } from "@medusajs/framework/types"7 8export default async function productCreateHandler({9 event: { data },10 container,11}: SubscriberArgs<{ id: string }>) {12 const notificationModuleService: INotificationModuleService =13 container.resolve(Modules.NOTIFICATION)14 15 await notificationModuleService.createNotifications({16 to: "user@gmail.com",17 channel: "email",18 template: "product-created",19 data,20 })21}22 23export const config: SubscriberConfig = {24 event: "product.created",25}
The create method accepts an object or an array of objects having the following properties:
Loading...
For a full list of properties accepted, refer to this guide.
Using the sendNotificationsStep#
If you want to send a notification as part of a workflow, You can use the sendNotificationsStep in your workflow.
For example:
1import {2 createWorkflow,3 transform,4} from "@medusajs/framework/workflows-sdk"5import { 6 sendNotificationsStep, 7 useQueryGraphStep,8} from "@medusajs/medusa/core-flows"9 10type WorkflowInput = {11 id: string12}13 14export const sendEmailWorkflow = createWorkflow(15 "send-email-workflow",16 ({ id }: WorkflowInput) => {17 const { data: products } = useQueryGraphStep({18 entity: "product",19 fields: [20 "*",21 "variants.*",22 ],23 filters: {24 id,25 },26 })27 28 const notificationData = transform(29 { products },30 ({ products }) => ({31 product_title: products[0].title,32 product_image: products[0].images[0]?.url,33 })34 )35 36 sendNotificationsStep({37 to: "user@gmail.com",38 channel: "email",39 template: "product-created",40 data: notificationData,41 })42 }43)
For a full list of input properties accepted, refer to the sendNotificationsStep reference.
You can then execute this workflow in a subscriber, API route, or scheduled job.
For example, you can execute it when a product is created:
1import type {2 SubscriberArgs,3 SubscriberConfig,4} from "@medusajs/framework"5import { sendEmailWorkflow } from "../workflows/send-email"6 7export default async function productCreateHandler({8 event: { data },9 container,10}: SubscriberArgs<{ id: string }>) {11 await sendEmailWorkflow(container).run({12 input: {13 id: data.id,14 },15 })16}17 18export const config: SubscriberConfig = {19 event: "product.created",20}
Was this page helpful?