Skip to content

Ft push notification #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions fasta-backend/api/transporters-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const https = require("https");
const options = {
hostname: "maps.googleapis.com",
port: 443,
path: `/maps/api/place/textsearch/json?query=transport&location=${this.latitude},${this.longitude}&radius=10000&key=AIzaSyC8G-WQhDTvlVflIFPzOj30rDQGk0Fekx8`,
path: `/maps/api/place/textsearch/json?query=transport&location=${this.latitude},${this.longitude}&radius=10000&key=AIzaSyAm00Wsdh6jJB2QzlW5c6t_nu0gMRAZB9s`,
method: "GET"
};

Expand All @@ -17,7 +17,7 @@ const client = new Client({});
// Allow function to receive coordinate argument from client
class GetTripInfo {
constructor(latitude, longitude, method) {
this.latitude = latitude;
this.latitude = latitude;
this.longitude = longitude;
this.method = method;
}
Expand Down Expand Up @@ -48,3 +48,7 @@ class GetTripInfo {
}

module.exports = GetTripInfo;


// path: `/maps/api/place/textsearch/json?query=transport&location=${this.latitude},${this.longitude}&radius=10000&key=AIzaSyDg-6GC6doxzpE_etI9E-yJR2NOLyFzBYc`,
// path: `/maps/api/place/textsearch/json?query=transport&location=${this.latitude},${this.longitude}&radius=10000&key=AIzaSyC8G-WQhDTvlVflIFPzOj30rDQGk0Fekx8`,
4 changes: 3 additions & 1 deletion fasta-backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const logger = require("morgan");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");


dotenv.config();

const CORS = require("cors");
Expand All @@ -17,6 +16,7 @@ const reportRouter = require("./routes/report");

const tripInfoRouter = require("./routes/tripInfo");

const notififoRouter = require("./routes/notification");


require("./db/index");
Expand All @@ -43,6 +43,8 @@ app.use("/api/v1/", sendRouter);
app.use("/api/v1/", getTransporter);
app.use("/api/v1", reportRouter);
app.use("/api/v1", tripInfoRouter);
app.use("/api/v1", notififoRouter);


app.get("/", (req, res) => res.send("<h2>Welcome to FASTA</h2><p><a href=\"/api/v1\">Check out version 1 of the FASTA API</a></p>"));

Expand Down
2 changes: 1 addition & 1 deletion fasta-backend/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const http = require("http");
*/

// eslint-disable-next-line no-use-before-define
const port = normalizePort(process.env.PORT || "3000");
const port = normalizePort(process.env.PORT || "8080");
app.set("port", port);

/**
Expand Down
115 changes: 115 additions & 0 deletions fasta-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion fasta-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"nodemailer": "^6.4.6",
"nodemailer-smtp-transport": "^2.7.4",
"reverse-geocoding": "^3.0.2",
"reverse-geocoding-google": "^1.1.0"
"reverse-geocoding-google": "^1.1.0",
"web-push": "^3.4.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
Expand Down
Binary file added fasta-backend/public/car.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions fasta-backend/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Push Notifications for FASTA</title>
</head>
<body>
<h1>Welcome to FASTA</h1>

<p>Push Notifications being implemented for FASTA...</p><p><a href="/api/v1">Check out version 1 of the FASTA API</a></p>
<script src="pushClient.js"></script>
</body>
</html>
52 changes: 52 additions & 0 deletions fasta-backend/public/pushClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const VAPID_PUBLIC_KEY = "BJOK8gh8hHPlZbr5Ci4LmJMWNlDsTdmQ2bGfE7TA6c9UhGmEuHTvjS179aqzobeQfQgWE94ELR26qbozfOfAVpc";

// check for service Worker
if ("serviceWorker" in navigator) {
// eslint-disable-next-line no-use-before-define
send().catch((err) => console.log(err));
}

// Register SW, Regisster Push, Send Push
async function send() {
// Register SW
console.log("Registering service worker...");
const register = await navigator.serviceWorker.register("/worker.js", { scope: "/" });
console.log("Service Worker Registered");

// Register Push
console.log("Registering Push...");
const subscription = await register.pushManager.subscribe({
userVisibleOnly: true,
// eslint-disable-next-line no-use-before-define
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY)
});
console.log("Push Registered...");

// Send Push Notification
console.log("Sending Push...");
await fetch("api/v1/subscribe", {
method: "POST",
body: JSON.stringify(subscription),
headers: {
"content-type": "application/json"
}
});
}

function urlBase64ToUint8Array(base64String) {
// eslint-disable-next-line no-mixed-operators
const padding = "=".repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
// eslint-disable-next-line no-useless-escape
.replace(/\-/g, "+")
.replace(/_/g, "/");

const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);

// eslint-disable-next-line no-plusplus
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
26 changes: 26 additions & 0 deletions fasta-backend/public/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
console.log("Service worker loaded!");


// eslint-disable-next-line no-restricted-globals
self.addEventListener("push", (e) => {
const { data } = e;
console.log(data, "Push received...");


fetch("/api/v1/notification").then((ed) => ed.json()).then((d) => {
const { options } = d;
// eslint-disable-next-line no-restricted-globals
self.registration.showNotification(`${options.subject}`, {
body: `${options.text}
${options.output}`,
icon: "/car.png"
});
})
.catch((ee) => console.log(ee));

// eslint-disable-next-line no-restricted-globals
// self.registration.showNotification("FASTA is here... Expect MORE!!!", {
// body: "Notified by FASTA.ng",
// icon: "/car.png"
// });
});
19 changes: 18 additions & 1 deletion fasta-backend/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@

const express = require("express");

const router = express.Router();
const webpush = require("web-push");

const vpidkeys = webpush.generateVAPIDKeys();
// console.log(vpidkeys.privateKey);


// eslint-disable-next-line max-len
// webpush.setVapidDetails("mailto:thevetdoctor@gmail.com", process.env.VAPID_PUBLIC_KEY, process.env.VAPID_PRIVATE_KEY);
webpush.setVapidDetails("mailto:thevetdoctor@gmail.com", vpidkeys.publicKey, vpidkeys.privateKey);


router.get("/", (req, res) => res.status(200).json({ message: "Welcome to FASTA, taking you faster in safety & convenience!" }));

router.post("/subscribe", (req, res) => {
const subscription = req.body;

const payload = JSON.stringify({ title: "Push test" });
webpush.sendNotification(subscription, payload).catch((err) => console.log(err));
res.status(201).json({ message: "Push sent" });
});

module.exports = router;
Loading