Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
How to get real time notification with sound using laravel pusher?

I want to get real-time notifications with sound when an order is placed into my app. I'm using laravel 8 and pusher to that. I get real-time alerts but the sound does not work perfectly. When an order is placed first time sound does not play, after that sound is played perfectly. Here is my code structure ...

My event class

  <?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MyEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return ['my-channel'];
    }

    public function broadcastAs()
    {
        return 'my-event';
    }
}

Here is the js code

Pusher.logToConsole = true;
  var pusher = new Pusher('***pusher key***', {
      cluster: 'ap2'
  });

  var channel = pusher.subscribe('my-channel');

  channel.bind('my-event', function(data) {
      alert(data.message)
      playAudio();

  });

  function playAudio() {
      var x = new Audio('http://127.0.0.1:8000/backend/assets/notification.mp3');
      // Show loading animation.
      var playPromise = x.play();

      if (playPromise !== undefined) {
          playPromise.then(_ => {
                  x.play();.
              })
              .catch(error => {
              });

      }
  }

Here is my controller

public function store(){
    event(new MyEvent('1 new order has been placed'));
    return 'Success';

}

When I hit this route for the first time, real-time alert message is shown but the sound does not play. It shows this error

DOMException: play() failed because the user didn't interact with the document first. 
https://some link

And it works perfectly after first time

I would like to give a sound notification to the admin if the order is placed, now how can I do that?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

With the latest updates of browsers, it is now impossible to start a sound with the code without the user clicking a button.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda