send-notification
Admin users can send a push notification
localhost:3000/send-notification
Notifications use the Firebase Cloud Messaging service (FCM)
Projects data is filtered based on the user's organisation
src/pages/send-notification.js
// get all project ids
async function getAllProjects() {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("projects")
.select("*")
.eq("organisation", organisation)
.order("created_at", { ascending: false });
if (error && status !== 406) {
throw error;
}
if (data) {
setProjects(data);
}
} catch (error) {
} finally {
setLoading(false);
}
}
User profiles are filtered using separate functions
src/pages/send-notification.js
// filter project
async function getUsersFilterProject(d) {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("profiles")
.select("*")
.filter("project", "in", '("' + d + '")')
.eq("organisation", organisation)
.neq("fcm_token", null);
if (error && status !== 406) {
throw error;
}
if (data) {
setUserData(data);
}
} catch (error) {
console.log(error);
} finally {
setLoading(false);
}
}
// filter province
async function getUsersFilterProvince(d) {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("profiles")
.select("*")
.filter("province", "in", '("' + d + '")')
.eq("organisation", organisation)
.neq("fcm_token", null);
if (error && status !== 406) {
throw error;
}
if (data) {
setUserData(data);
}
} catch (error) {
console.log(error);
// console.log("user not logged in");
} finally {
setLoading(false);
}
}
// filter district
async function getUsersFilterDistrict(d) {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("profiles")
.select("*")
.filter("district", "in", '("' + d + '")')
.eq("organisation", organisation)
.neq("fcm_token", null);
if (error && status !== 406) {
throw error;
}
if (data) {
console.log(data);
setUserData(data);
}
} catch (error) {
console.log(error);
// console.log("user not logged in");
} finally {
setLoading(false);
}
}
// filter health facility
async function getUsersFilterFacility(d) {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("profiles")
.select("*")
.filter("health_facility", "in", '("' + d + '")')
.eq("organisation", organisation)
.neq("fcm_token", null);
if (error && status !== 406) {
throw error;
}
if (data) {
setUserData(data);
}
} catch (error) {
console.log(error);
// console.log("user not logged in");
} finally {
setLoading(false);
}
}
// filter health role
async function getUsersFilterRole(d) {
try {
setLoading(true);
let { data, error, status } = await supabase
.from("profiles")
.select("*")
.filter("role", "in", '("' + d + '")')
.eq("organisation", organisation)
.neq("fcm_token", null);
if (error && status !== 406) {
throw error;
}
if (data) {
setUserData(data);
}
} catch (error) {
console.log(error);
// console.log("user not logged in");
} finally {
setLoading(false);
}
}
A Supabase serverless function is used to call the FCM service
The function is invoked whenever a new record is added to the notifications table using Supabase via Web hooks
src/pages/send-notification.js
// set notification to users
const userNotificationsSend = () => {
if (text && title) {
userData.map(function (user, index) {
sendNotifications(user);
});
} else {
sethandleError(true);
}
};
async function sendNotifications(userData) {
sethandleError(false);
try {
setLoading(true);
const dataEnter = {
user_id: userData.id,
title: title && title.entry,
body: text && text.entry,
};
const { data, error } = await supabase
.from("notifications")
.insert(dataEnter)
.select();
if (error) {
throw error;
}
} catch (error) {
alert(error.message);
} finally {
setPushSent(true);
setLoading(false);
}
}