Push notification with Socket.io

Asked

Viewed 801 times

0

I made a small application with Node + socket.io to send notification. adding the code below on the client page, the notification is displayed in the div "messagebox".

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://localhost:4555/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost:4555', {transports: ['websocket', 'polling', 'flashsocket']});
  socket.on('notificacao', function (data) {
    document.getElementById('messagebox').innerHTML = data;

  });
</script>

<div id="messagebox">

My question now is how to make those notifications appear in the facebook style. or to be clearer equal to the pushcrew notifications.

I haven’t found anything that can help me :/ someone has some material or knows how I can do it ?

  • I didn’t understand what you want exactly, could explain better?

  • @Rafaelaugusto Good morning Rafael yes, next... the way I did, as soon as I triggered the notification it appears on the page by div MESSAGEBOX but I would like instead to appear in div, appears this way here --> prntscr.com/joj5xi I could not do it :/

  • What you want is to use the HTML, Notification take a look https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API

  • @Rafaelaugusto had already seen this documentation, but I could not integrate with my application, due to lack of more advanced knowledge on the subject :/ even so thank you.

  • I’ll set a functional example for you

  • @Rafaelaugusto ta joia, thanks man :)

  • @Rafaelaugusto can check what is wrong with this code --> http://prntscr.com/jokj9q. when sending the notification appears the alert to accept, the notification appears on the console, but it is not displayed as I said it --> http://prntscr.com/joj5xi

Show 2 more comments

1 answer

2


I think a solution to your problem would be this

var socket = io('http://localhost:4555', {transports: ['websocket', 'polling', 'flashsocket']});
socket.on('notificacao', function (data) {
    console.log(data) //VERIFIQUE OS DADOS QUE ESTÃO CHEGANDO 
    notify(data.nome,data.mensagem)
});

function notify(nome,mensagem) {
    Notification.requestPermission(function() {
        var notification = new Notification(nome, {
            icon: 'ICONE DA SUA APLICAÇÃO',
            body: mensagem
        });
        notification.onclick = function() {
            //SUA LÓGICA AQUI
        }
    });
}

Code based on: Desktop notifications in Chrome with Javascript

  • Thanks for the contribution, but I couldn’t put that code together with mine to make it work :/

  • Error appears?

  • I edited my answer, copy this code and take a look at your console

  • opa, appeared like this in the console --> http://prntscr.com/jokdyq but the notification did not appear, and whenever I trigger it asks if I want to accept receive notification

  • I did this --> http://prntscr.com/jokj9q. but the notification is not yet available :/

  • You must allow the notification once, and then it starts to appear. This is from the browser itself.

  • so whenever I send the notification it requests that I allow it to receive, I click ALLOW send again and it makes me and ask if I want to receive :) but does not create the notification, only appears on the console

  • Find out the problem :) when I uploaded the application to a server it worked :) thanks friend.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.