pub / newsplanet

Planet-Style Newsfeed generated with perlanet
git clone https://src.jayvii.de/pub/newsplanet.git
Home | Log | Files | Exports | Refs | README | RSS

push.js (1232B)


      1 /* Function to send notifiations to the user
      2  * adjusted from example code in:
      3  * https://developer.mozilla.org/en-US/docs/Web/API/Notification
      4  */
      5 
      6 function send_notification(title, body_text, url) {
      7   
      8   if (!("Notification" in window)) {
      9 
     10     // Send alert, if the browser does not support notifications
     11     alert("This browser does not support desktop notification");
     12 
     13   } else if (Notification.permission === "granted") {
     14 
     15     // send notification, if the user allowed them
     16     create_notification(title, body_text, url);
     17 
     18   } else if (Notification.permission !== "denied") {
     19 
     20     // ask for notification permission, if it neither denied nor granted
     21     // send notification, if the user grants permission
     22     Notification.requestPermission().then((permission) => {
     23       if (permission === "granted") {
     24         create_notification(title, body_text, url);
     25       }
     26     });
     27 
     28   }
     29 }
     30 
     31 function create_notification(title, body_text, url) {
     32 
     33   // Create notification object
     34   const notification = new Notification(
     35     title,
     36     {
     37       body: body_text,
     38       icon: "/assets/favicon.png",
     39       requireInteraction: true
     40     }
     41   );
     42 
     43   // add onclick event for opening the given URL
     44   notification.onclick = () => window.open(url);
     45 
     46 }