Electron, system notification and playing sound

24 Views Asked by At

I have electron setup with my Ubuntu box , however no matter what i try I can't get this to work

ty// Variable to track if notification has been sent
let notificationSent = false;

// Function to update the countdown
function updateCountdown() {
  // Read the JSON file
  fs.readFile(jsonFilePath, "utf8", (err, data) => {
    if (err) {
      console.error("Error reading file:", err);
      return;
    }

    try {
      // Parse the JSON data
      const jsonData = JSON.parse(data);

      // Get current UTC time
      let now = new Date();

      // Initialize variables to hold current and next activations
      let currentActivation = null;
      let nextActivation = null;

      // Iterate over the gates and lines
      for (let gate in jsonData) {
        let lines = jsonData[gate];
        for (let line in lines) {
          let lineTime = new Date(lines[line]);

          // If the activation time is in the past compared to the current time
          if (lineTime <= now) {
            // Update current activation if this line's time is later than the current one
            if (!currentActivation || lineTime > currentActivation.time) {
              currentActivation = { gate, line, time: lineTime };
            }
          } else {
            // Update next activation if it's not yet set or this line's time is earlier than the next one
            if (!nextActivation || lineTime < nextActivation.time) {
              nextActivation = { gate, line, time: lineTime };

              // If notification has not been sent yet
              if (!notificationSent) {
                // Play the sound
                new Audio(soundFilePath).play();

                // Send a system notification
                if (Notification.permission === "granted") {
                  new Notification("New Activation", {
                    body: `Next Moon activation: ${nextActivation.gate} ${
                      nextActivation.line
                    } at ${nextActivation.time.toLocaleString("en-US", {
                      year: "numeric",
                      month: "2-digit",
                      day: "2-digit",
                      hour: "2-digit",
                      minute: "2-digit",
                      second: "2-digit",
                    })}`,
                    requireInteraction: true,
                  });

                  // Set notificationSent flag to true
                  notificationSent = true;

                  // Close the notification after 30 seconds
                  setTimeout(() => {
                    notificationSent = false; // Reset flag after 30 seconds
                  }, 30000);
                }
              }
            }
          }
        }
      }


the idea is to have the notification sent when I have a new activation. I do have a live countdown until the new activation btw and also have the date and time for when it happens. However what i have so far sends random notifications with the wrong info Thank you for your help

The expectation is for the code to send the notification that stays on my screen for 30 seconds and also plays a sound when the new activation happens. The part of the code is from a javascript file.

0

There are 0 best solutions below