0

Hi i want to send desktop notifications to guest which is visited my web site.

So i need to use Chrome apis for this. Because only chrome browser has the desktop notification.

Here is my Codes to send notification. I checked out this Chrome Desktop Notification Example

function notifyMe() {
   // Let's check if the browser supports notifications
   if (!("Notification" in window)) {
     alert("This browser does not support desktop notification");
   }

   // Let's check whether notification permissions have already been granted
   else if (Notification.permission === "granted") {
     // If it's okay let's create a notification
     var notification = new Notification("Hi there!");
   }

   // Otherwise, we need to ask the user for permission
   else if (Notification.permission !== 'denied') {
     Notification.requestPermission(function (permission) {
       // If the user accepts, let's create a notification
       if (permission === "granted") {
         var notification = new Notification("Hi there!");
       }
     });
   }
 }
<!DOCTYPE html>
<html>
<head>
 <title></title>
 <script src="check.js"></script>
</head>
<body>
 <input type="button" onclick="notifyMe()" value="Send Notification"></input>
</body>
</html>

When i clicked the button a notification appears just for me. But i want to make an admin panel and when i clicked the button that notification should be appear for all users who visited my web site and allow for notification. For example in the May 21 i need to send notification like "Today is May 21 you can get cheap stuffs !" This message must be appear for all users who allowed for notifications. Thank you.

Community
  • 1
  • 1
Nezih
  • 381
  • 6
  • 19

1 Answers1

0

EDIT : The following code will send the notification only if the browser is open. If you want to send notifications on a closed web app check this tutorial that presents the push API and how to implement it :https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web

This is my suggestion : you can use socket.io in your web application. Socket.io enables real-time bidirectional event-based communication between your server and all the users of your website.

In order to make it work you should have a server that runs with nodejs. Every time an user will load your page a socket connection will be created between him and your server.

You will be able to emit events on those sockets, that you will catch in the javascript code that you will provide on your page. For your case you can broadcast an event "send_notification" to all your users when you want. In your front js script send the notification when you receive this event from the server.

The cool part with socket.io is that you can broadcast an avent and attach to it data. In your case it could be your notification message.

Example Code :

app.js (server part)

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(8080);

app.get('/', function (req, res) {
  res.sendfile('index.html');
});

io.on('connection', function (socket) {

  io.sockets.emit('notification', { message: 'Hello the world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html. (the front Part)

<html>
   <head>
   </head>
   <body>
      <h1>HELLO</h1>
      <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
      <script>
         function notifyMe(message) {
           // Let's check if the browser supports notifications
           if (!("Notification" in window)) {
             alert("This browser does not support notifications");
           }

           // Let's check whether notification permissions have already been granted
           else if (Notification.permission === "granted") {
             // If it's okay let's create a notification
             var notification = new Notification(message);
           }

           // Otherwise, we need to ask the user for permission
           else if (Notification.permission !== 'denied') {
             Notification.requestPermission(function (permission) {

               // We store the authorization information
               if(!('permission' in Notification)) {
                 Notification.permission = permission;
               }

               // If the user accepts, let's create a notification
               if (permission === "granted") {
                 var notification = new Notification(message);
               }
             });
           }


         }


          // Connect to the server
           var socket = io.connect('https://socket-io-notif-maximilienandile.c9users.io:8080');
           // WHEN we receive the notification event execute the notifyMe function
           socket.on('notification', function (data) {
             notifyMe(data.message);
           });
      </script>
   </body>
</html>

In order to implement it take a look at this official socket.io tutorial link : http://socket.io/docs/#using-with-the-express-framework

In your app.js you should then decide when to broadcast the event. Here the event is broadcasted each time an user load the index.html page of your app.

Here is the example i made with cloud9. Here the notification is fired after the connection. https://ide.c9.io/maximilienandile/socket_io_notif You can view the live application here : https://socket-io-notif-maximilienandile.c9users.io/

I hope it will help you !

  • Check also this previous answer it can also solve your problem : http://stackoverflow.com/a/35720229/2862130. – maximilienAndile Mar 13 '16 at 22:01
  • Thank you very much for your suggestion ! Also, do you know the working logic of push crew api ? I want to make my own api so my clients can use it for their web sites not just me. – Nezih Mar 13 '16 at 23:42
  • I did not know this one but i will take a look at it,thanks – maximilienAndile Mar 14 '16 at 00:02
  • I got some questions, why did you used var socket = io.connect('https://socket-io-notif-maximilienandile.c9users.io:8080'); And when i click button on (localhost:8080) it appears me a notification but i dont have device to test it. Can i send desktop notification to all users who allowed permission now ? – Nezih Mar 14 '16 at 00:12
  • You should replace 'https://socket-io-notif-maximilienandile.c9users.io:8080' by the url of your app (or localhost). If you broadcast an event through socket.io in you app.js it will send it to all sockets now connected. – maximilienAndile Mar 14 '16 at 00:18
  • Even browser closed ? – Nezih Mar 14 '16 at 00:20
  • No, only if the browser is open... sorry if you wanted also this – maximilienAndile Mar 14 '16 at 00:24
  • I'm sorry i thought i couldnt explain my self clear enought. Chrome desktop notification is working even browser closed. If im not wrong its working for only chrome.You can see it on facebook. Settings / notifications. Site And Mobile section is avaliable only if you connect from chrome browser. – Nezih Mar 14 '16 at 00:31
  • Oh I see, Take a look at the Google Cloud Messaging : https://developers.google.com/cloud-messaging/. It can do what you want I guess – maximilienAndile Mar 14 '16 at 00:37