laravel-notification-channels / pusher-push-notifications

Pusher Beams notifications channel for Laravel
http://laravel-notification-channels.com
MIT License
270 stars 67 forks source link

How to get instance of beamsClient in route #59

Closed ljanecek closed 1 year ago

ljanecek commented 3 years ago

I would like to ask you, how right get beamsClient in route for generating token.

At this moment, my implementation looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Pusher\PushNotifications\PushNotifications;

class BeamsController extends Controller
{

    protected $beamsClient;

    public function __construct()
    {

        $this->middleware('auth');

        $config = config('services.pusher');

        $this->beamsClient = new PushNotifications([
            'instanceId' => $config['beams_instance_id'],
            'secretKey' => $config['beams_secret_key'],
        ]);
    }

    public function index()
    {
        $user = Auth::user();
        $beamsToken = $this->beamsClient->generateToken('App.User.' . $user->id);
        return response()->json($beamsToken);
    }
}

I think, that Pusher\PushNotifications\PushNotifications should be registered in the Service provider to get instance with configurations. But, there is just booting. But, I'm not sure how exactly the Service provider in Laravel works.

Thanks!

danieledelgiudice commented 3 years ago

This would be useful when trying to use generateToken to associate a user device with his own user id and/or deleteUser. Simply exposing the $beamsClient should be enough, I can create a PR if you're open to it.

geeksalah7 commented 2 years ago

Simple. Dependency injection.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Pusher\PushNotifications\PushNotifications;

class BeamsController extends Controller
{

    protected $beamsClient;

    public function __construct(PushNotifications $beamsClient)
    {

        $this->middleware('auth');
        $this->beamsClient = $beamsClient;
    }

    public function index()
    {
        $user = Auth::user();
        $beamsToken = $this->beamsClient->generateToken('App.User.' . $user->id);
        return response()->json($beamsToken);
    }
}

Laravel Docs

hypnocill commented 2 years ago

@ljanecek How did you get it work? @geeksalah7 answer doesn't seem to work since PusherPushNotificationsServiceProvider lets only PusherChannel::class get injected with PushNotifications::class? Is there a way to get the beams instance without having to patch the property from protected to public?

socieboy commented 1 year ago

@hypnocill rewrite the service provider to this.

 /**
     * Bootstrap the application services.
     */
    public function register()
    {
        $this->app->when(PusherChannel::class)
            ->needs(PushNotifications::class);

        $this->app->bind(PushNotifications::class, function ($app) {

            $config = $app['config']['services']['pusher'];

            return new PushNotifications([
                'instanceId' => $config['instance_id'],
                'secretKey' => $config['secret_key'],
            ]);
        });
    }