1

I'm working with a desktop notification. I'm using this code to show it, and its working fine:

// If the user is okay, let's create a notification
if (permission === "granted") {
  var options = {
    body: "This is the body of the notification",
    icon: "icon.jpg",
    dir : "ltr"
  };
  var notification = new Notification("Hi there",options);
}

But how can I fetch data from a text file into options.body?

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
Ryewell
  • 49
  • 1
  • 6

2 Answers2

2

Adapting the code from this answer, the finished result should look like this:

// If the user is okay, let's create a notification
if (permission === "granted") {
  var options = {
    icon: "icon.jpg",
    dir : "ltr"
  };
  var XHR = new XMLHttpRequest();
  XHR.open("GET", "notificion.txt", true);
  XHR.send();
  XHR.onload = function (){
    options.body = XHR.responseText;
    var notification = new Notification("Hi there",options);
  };
}
Community
  • 1
  • 1
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
0

Example using JQuery $.get()

if (permission === "granted") {
  $.get("notificion.text", function(data) {
    var notification = new Notification("Hi there", {
      icon: "icon.jpg",
      dir: "ltr",
      body: data
    });
  });
}
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11