Eintrag

Electron Notifications

Einleitung

Ein einfaches Beispiel für eine Benachrichtigung in Electron. In den Code-Beispielen befinden sich noch ein paar Kommentare - damit der Code evtl. besser verständlich ist.

index.hmtl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <title>Notification</title>
  </head>
  <body>
    <!-- input-Felder für Zeitangabe -->
    <input type="text" id="hour" value="Stunde">
    <input type="text" id="minute" value="Minute">
    <input type="text" id="notification" value="Benachrichtigung">
    <!-- Buttons zum Aufruf der Funktionen -->
    <button id ="start" type="button" class="btn btn-info" onclick="startAlarm()">Start</button>
    <button id ="stop" type="button" class="btn btn-info" onclick="stopAlarm()">Stop</button>
    
    <script src="js/routing.js"></script>
  </body>
</html>

main.js

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
27
const { app, BrowserWindow, ipcMain} = require('electron')

function createWindow(){
    const win = new BrowserWindow({
      width: 800,  
      height: 600,
      webPreferences: {
          nodeIntegration: true,
          enableRemoteModule: true,
          contextIsolation: false
      }  
    })
    win.loadFile('src/index.html')
    win.webContents.openDevTools()
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () =>{
    app.quit()
})

app.on('activate', ()=>{
    if (BrowserWindow.getAllWindows().length === 0){
        createWindow()
    }
})

routing.js

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// einfache Benachrichtigung die sofort beim öffnen der App aufgerufen wird.
const notification = new Notification("I`m a notification", {
  body: "Notification from render process",
});

// alert() auslösen, wenn auf die Benachrichtigung geklickt wurde.
notification.onclick = () => {
  alert("clicked")
};

let timer;

function startAlarm() {
  // setInterval(function, Interval) Hinweis: (1000ms = 1 Sekunde)
  timer = setInterval(alarm, 1000)
}

function alarm() {
  const alarmHour = document.getElementById("hour")
  const alarmMinute = document.getElementById("minute")
  const alarmNotification = document.getElementById("notification")

  // aktuelle Zeit
  let time = new Date();
  let currentHour = time.getHours()
  let currentMinute = time.getMinutes()
  let currentSecond = time.getSeconds()

  console.log(currentHour + ":" + currentMinute + ":" + currentSecond);

  // wenn aktuelle Zeit und die eingestellte Zeit übereinstimmen, 
  // löst die Benachrichtigung aus
  if (currentMinute == alarmMinute.value && currentHour == alarmHour.value) {
    const notification = new Notification("Alarm notification", {
        body: alarmNotification.value
      });
    // reset des Interval
    clearInterval(timer)
  }
}

// stop-Funktion des Interval
function stopAlarm(){
    clearInterval(timer)
}
Dieser Eintrag ist vom Autor unter CC BY 4.0 lizensiert.