Push Notifications
Overview
The admin should be able to receive push notidfications pertaining to support tickets, Borku Africa orders, vendor approvals, and other important events.
note
- Please note that the initialization for the messaging service of the Firebase SDK, the target creation and admin topic subscription should be done after the user has logged in successfully (for a good user experience).
- All notifications for the admins are sent to the admin topic.
Firebase SDK and Firebase Messaging Setup
A full guide on how to install the Firebase SDK can be found here.
Create Target
This method is to link the client with the logged in user and register the client to our Appwrite backend.
note
- We will be using the user's session ID to create a target, since it's already a unique identifier. It also helps to be able to delete the target when the user logs out.
fcmTokenis the token generated by the Firebase SDK when the user logs in.FCM_PROVIDERis the ID of the Firebase provider in Appwrite.
try {
// get session details
const session = await account.getSession("current");
await account.createPushTarget(session.$id, fcmToken, "FCM_PROVIDER");
console.log("Target created successfully");
} catch (error) {
if (error instanceof AppwriteException) {
console.error(error.message);
// display error message to user
} else {
console.error(error);
// display error message to user
}
}
Subscribe To Admin Topic
This method is to subscribe the client to the admin topic, where all notifications for the admins are sent.
try {
// get session details
const session = await account.getSession("current");
const targetId = session.$id;
const userId = session.userId;
await messaging.createSubscriber("ADMINS_TOPIC", userId, targetId);
console.log("Subscriber created successfully");
} catch (error) {
if (error instanceof AppwriteException) {
console.error(error.message);
// display error message to user
} else {
console.error(error);
// display error message to user
}
}
Delete Target
This method is to delete the target when the user logs out.
try {
// get session details
const session = await account.getSession("current");
const userId = session.userId;
await messaging.deleteSubscriber("ADMINS_TOPIC", userId);
console.log("Subscriber deleted successfully");
} catch (error) {
if (error instanceof AppwriteException) {
console.error(error.message);
// display error message to user
} else {
console.error(error);
// display error message to user
}
}