Context
Sending push notifications from pushengage.com dashboard is pretty easy but what about f we want to automate some process in our web site? For instance, let say we want to send a push notification every time you create a blog post
The code
const baseUrl = 'https://api.pushengage.com/apiv1/notifications';
const params = {
title: '[notification title here]',
message: '[notification message here]',
url: '[notification url here]',
imageUrl: '[your image url here]',
};
const requestParams = `notification_type=draft¬ification_title=${params.title}¬ification_message=${params.message}¬ification_url=${params.url}&image_url=${params.imageUrl}`;
const url = baseUrl;
const res = await fetch(url,
{
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
api_key: [your_api_key_here],
},
body: requestParams,
});
const body = await res.text();
console.info(res.status);
console.info(body);
Note: have in mind we are sending the notification as a draft: notification_type=draft. Once ready, remove that parameter to send the real notification
Output
The first console log will print 200 status code and the second one the operation result as json
200
{"success":true,"notification_id":13623215772}
Photo by Andres Canavesi on Unsplash