Skip to content

Commit 5657297

Browse files
committed
initial commit
0 parents  commit 5657297

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+8957
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
/node_modules

README.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Medusa Push Notifications Plugin
2+
3+
A plugin for Medusa e-commerce that adds push notification functionality. This plugin allows you to manage device registrations and send push notifications to customers across multiple devices.
4+
5+
## Features
6+
7+
- 🔔 Push notification support for web browsers
8+
- 📱 Multi-device support per customer
9+
- 🔄 Automatic device registration and management
10+
- 🔐 Secure VAPID-based implementation
11+
- 🛠️ Admin API for sending notifications
12+
- 🔌 Event-based notification triggers
13+
14+
## Prerequisites
15+
16+
- Medusa backend
17+
- Redis (for event bus)
18+
- PostgreSQL
19+
- VAPID Keys (for push notifications)
20+
21+
## Installation
22+
23+
```bash
24+
npm install medusa-push-notification-v1
25+
```
26+
27+
## Configuration
28+
29+
Add to your `medusa-config.js`:
30+
31+
```javascript
32+
const plugins = [
33+
// ... other plugins
34+
{
35+
resolve: `medusa-plugin-push-notifications`,
36+
}
37+
]
38+
```
39+
40+
### Environment Variables
41+
42+
```bash
43+
VAPID_PUBLIC_KEY=your_public_key
44+
VAPID_PRIVATE_KEY=your_private_key
45+
VAPID_SUBJECT=mailto:your@email.com
46+
```
47+
48+
### Generate VAPID Keys
49+
50+
```bash
51+
npx web-push generate-vapid-keys
52+
```
53+
54+
## API Routes
55+
56+
### Store API
57+
58+
```bash
59+
# Register device
60+
POST /store/push-notifications
61+
{
62+
"subscription": {
63+
"endpoint": "https://push-service.example.com/...",
64+
"keys": {
65+
"p256dh": "base64-encoded-key",
66+
"auth": "base64-encoded-auth"
67+
}
68+
},
69+
"device_info": {
70+
"type": "mobile",
71+
"browser": "Chrome",
72+
"os": "Android",
73+
"model": "Samsung S21"
74+
}
75+
}
76+
77+
# Unregister device
78+
DELETE /store/push-notifications/{device_id}
79+
```
80+
81+
### Admin API
82+
83+
```bash
84+
# Send notification to customers
85+
POST /admin/push-notifications
86+
{
87+
"customer_ids": ["cust_123", "cust_456"],
88+
"notification": {
89+
"title": "Special Offer!",
90+
"body": "Check out our new products",
91+
"icon": "/icon.png",
92+
"data": {
93+
"url": "/products/new"
94+
}
95+
}
96+
}
97+
```
98+
99+
## Service Methods
100+
101+
```typescript
102+
// Get service instance
103+
const pushService = container.resolve("push")
104+
105+
// Register a device
106+
await pushService.registerDevice({
107+
customer_id: "cust_123",
108+
device_info: {
109+
type: "mobile",
110+
browser: "Chrome",
111+
os: "Android",
112+
model: "Pixel 6"
113+
},
114+
subscription: {
115+
endpoint: "...",
116+
keys: {
117+
p256dh: "...",
118+
auth: "..."
119+
}
120+
}
121+
})
122+
123+
// Send notification
124+
await pushService.sendNotification(
125+
["cust_123"],
126+
{
127+
title: "Hello",
128+
body: "This is a test notification",
129+
data: { url: "/orders/123" }
130+
}
131+
)
132+
```
133+
134+
## Frontend Implementation
135+
136+
1. Register Service Worker:
137+
138+
```javascript
139+
// public/sw.js
140+
self.addEventListener('push', function(event) {
141+
if (!event.data) return
142+
143+
const data = event.data.json()
144+
145+
event.waitUntil(
146+
self.registration.showNotification(data.title, {
147+
body: data.body,
148+
icon: data.icon,
149+
badge: data.badge,
150+
data: data.data,
151+
})
152+
)
153+
})
154+
155+
self.addEventListener('notificationclick', function(event) {
156+
event.notification.close()
157+
158+
if (event.notification.data?.url) {
159+
event.waitUntil(
160+
clients.openWindow(event.notification.data.url)
161+
)
162+
}
163+
})
164+
```
165+
166+
2. Example React Component:
167+
168+
```jsx
169+
import { useNotification } from './hooks/useNotification'
170+
171+
function NotificationButton() {
172+
const {
173+
permission,
174+
isLoading,
175+
error,
176+
enableNotifications
177+
} = useNotification()
178+
179+
return (
180+
181+
{permission === 'default' && (
182+
183+
Enable Push Notifications
184+
185+
)}
186+
{/* ... other status displays */}
187+
188+
)
189+
}
190+
```
191+
192+
193+
## Events
194+
195+
The plugin listens to the following events by default:
196+
- `order.placed`
197+
- `order.canceled`
198+
- `order.items_returned`
199+
- `order.shipment_created`
200+
- `retrun.requested`
201+
202+
## Development
203+
204+
```bash
205+
# Clone the repository
206+
git clone your-repo-url
207+
cd medusa-plugin-push-notifications
208+
209+
# Install dependencies
210+
npm install
211+
212+
# Build
213+
npm run build
214+
215+
# Watch mode
216+
npx medusa develop
217+
```
218+
219+
## Contributing
220+
221+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
222+
223+
## License
224+
225+
[MIT](https://choosealicense.com/licenses/mit/)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa";
2+
export declare function GET(req: MedusaRequest, res: MedusaResponse): Promise<void>;

dist/api/admin/push-notifications/devices/route.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/admin/push-notifications/devices/route.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa";
2+
export declare function POST(req: MedusaRequest, res: MedusaResponse): Promise<void>;

dist/api/admin/push-notifications/route.js

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/admin/push-notifications/route.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa";
2+
export declare function DELETE(req: MedusaRequest, res: MedusaResponse): Promise<void>;

dist/api/store/push-notifications/[device_id]/route.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)