Readme

Push Notification Sender

This val can be used in other vals to send notifications to a segment using OneSignal's REST API

This is really handy if you want to send push notifications to your phone without building a native app! I built a barebones React PWA that asks for a password then loads the OneSignal Web SDK that I deployed to Netlify for free. OneSignal has easy to follow docs so you can build this functionality into a React, Angular, Vue app or even Wordpress! Then install the PWA on your platform of choice and you're off to the races!

Setup

  1. Save your ONESIGNAL_TOKEN and SEGMENT_APPID from OneSignal to your Val Town environment variables

  2. Import into another val!

import sendNotification from "https://esm.town/v/gwoods22/sendNotification";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import axios from "npm:axios";
axios.defaults.headers.get["User-Agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
async function run() {
const imageUrl =
"https://prodimage.images-bn.com/lf?set=key%5Bresolve.pixelRatio%5D,value%5B1%5D&set=key%5Bresolve.width%5D,value%5B600%5D&set=key%5Bresolve.height%5D,value%5B10000%5D&set=key%5Bresolve.imageFit%5D,value%5Bcontainerwidth%5D&set=key%5Bresolve.allowImage
console.log("ran");
try {
await axios.get(imageUrl, { responseType: "arraybuffer" });
console.log("ran axios");
} catch (error) {
console.error(error.response["data"]["errors"]);
throw error;
}
}
run();
// export default async () => {
// console.log("starting");
//
// };
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 3, 2024