Rendering Notifications
On the client in the customer's dashboard.
import { NotificationMenu } from '@notifyr/sdk';
import { useEffect, useState } from 'react';
const Notifications = () => {
const [clientSecret, setClientSecret] = useState('');
useEffect(() => {
(async () => {
const response = await fetch('https://my-app.com/get-client-secret');
const { clientSecret } = await resonse.json();
setClientSecret(clientSecret);
})()
}, [])
return <NotificationMenu clientSecret={clientSecret} />
}
On the sever fetching the client secret.
export default async function handler(req, res) {
// Add validation
try {
const response = await fetch(`https://notifyr.vercel.app/api/v1/generate-client-secret`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.NOTIFYR_API_KEY}`,
},
body: JSON.stringify({
groupId: 'test_user@gmail.com',
}),
});
const result = await response.json();
res.status(200).json(result);
} catch (e) {
return res.status(500).json({ error: e });
}
}
Sending Notifications
Send a notification on your sever when certain actions occur.
export default async function handler(req, res) {
// Add validation
try {
const notifyr = new Notifyr(process.env.NOTIFYR_API_KEY);
const response = await notifyr.sendNotification({
title,
body,
type,
groupId,
});
res.status(200).json(response);
} catch (e) {
return res.status(500).json({ error: e });
}
}