Search code examples
laravelpush-notificationpusher

How to push laravel notification via pusher beams ? and How can I add interests to my created notification so I can Listen in my react app?


I've implemented pusher beams like this:

  1. I've added credentials in config/services.php
'pusher' => [
  'beams_instance_id' => '********',
  'beams_secret_key' => '*************',
],
  1. I've created a new notification
class TestOne extends Notification
{
    use Queueable;

    public $message;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;

    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [PusherChannel::class , 'database'];
    }



    /**
     * Get the array representation of the notification.
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
    //    event( new PostLiked($notifiable->id ,$this->message));
        return [
            'title' => $this->message->title,
            'content' => $this->message->content,
        ];
    }
    // notification type
    public function broadcastType()
{
    return 'users';
}
public function toPushNotification($notifiable)
    {
        return PusherMessage::create()
            ->platform('web')
            ->badge(1)
            ->sound('success')
            ->body("Your ac account was approved!");
    }


}

  1. I've added Notifiable to User Model :
class User extends Authenticatable
{
  use HasApiTokens, HasFactory, Notifiable;

How to specify the interest in my backend so I can listen to it in my react app and how to use beams debug console to make sure the Laravel notification is being fired?


Solution

  • public function toPushNotification($notifiable)
        {
            return PusherMessage::create()
                ->platform('web')
                ->badge(1)
                ->sound('success')
                ->body("Your ac account was approved!");
        }
    

    While I was tracking this code , It seems that I had a certificate issues problem in my local environment.

    So I moved to this way in connection to beams :

    $beamsClient = new \Pusher\PushNotifications\PushNotifications(array(
            "instanceId" => "*********************************",
            "secretKey" => "***************",
    
        ), new GuzzleHTTP\Client(['verify' => false]));
    
    
        $publishResponse = $beamsClient->publishToUsers(
            array("user-001", "user-002", "1"),
            array("web" => array("notification" => array(
                "title" => "fofo",
                "body" => "Hello, World!",
                "deep_link" => "https://www.pusher.com",
            )),
            ));