- Published on
How to Push Notifications using PushOver in Node.js
- Authors
- Name
- Ashik Nesin
- @AshikNesin
Even wanted to get custom push notifications without the complexity of building your app and maintaining it?
That's where PushOver comes in.
Once it is configured, you can get a real-time notification to your devices (Android, iPhone, iPad, and Desktop) using their API.
And you can customize its sound, message content, icon, etc with a simple API
To use that API, we'll need to configure your PUSHOVER_USER_KEY
and PUSHOVER_API_TOKEN
to make the request.
For that, we'll need to create an account, and then get a license for the device on which you want push notifications. And it costs roughly ~$5 one-time purchase per client (meaning, you can get a single Android/iOS client license and use it on all the devices that you own)
That's pretty much it.
Here's the snippet that I generally use for sending push notifications in Node.js
npm install axios
import axios from 'axios';
const PUSHOVER_USER_KEY = process.env.PUSHOVER_USER_KEY;
const PUSHOVER_API_TOKEN = process.env.PUSHOVER_API_TOKEN;
const PUSHOVER_MSG_URL = 'https://api.pushover.net/1/messages.json';
const pushOverDefaults = {
user: PUSHOVER_USER_KEY,
token: PUSHOVER_API_TOKEN,
};
export const sendPushOverNotification = async payload => {
const data = {
...pushOverDefaults,
...payload,
};
try {
const res = await axios.default.post(PUSHOVER_MSG_URL, data);
return res.data;
} catch (error) {
// Ensure confidential information are not printed in public build logs
if (error && error.config && error.config.data) {
delete error.config.data;
}
if (error.response.status === 400) {
console.log(
'Failed to send Pushover.net message possibly due to invalid token/user or your application is over its quota.'
);
console.log(
'Please check Pushover docs for more details: https://pushover.net/api#friendly'
);
}
if (error.response.data) {
console.log(error.response.data);
}
throw error;
}
};
export default sendPushOverNotification;
Happy Push Notifications!