laravel-notification-channels / apn

APN push notifications channel for Laravel
http://laravel-notification-channels.com
MIT License
189 stars 56 forks source link

[Proposal] Send notifications via "apn" instead of the full "ApnChannel::class". #51

Closed skcin7 closed 5 years ago

skcin7 commented 5 years ago

Presently, to send these APN notifications, our via function will look something like this:

public function via($notifiable)
{
return [ApnChannel::class];
}

However, I want to be able to send them like this:

public function via($notifiable)
{
return ['apn'];
}

How can this be achieved?

So basically, I want to be able to give a custom name of this APN notification channel called "apn". I looked in the Laravel documentation and it does not seem to be explained how to achieve this, and I also dug into the code a little bit but can't seem to figure it out. It seems like this should be a trivial thing but apparently it's not. If anyone knows how this can be achieved, please post here. If it works well and is agreed to upon by this community potentially we can do a pull request. If for some reason this is not desired or bad practice, please explain why. Either way, it would be nice if this could be achieved.

Thanks for any insight or help you may provide.

dwightwatson commented 5 years ago

For whatever reason when this collection was put together it was opted to use the full class syntax. I think it's probably beneficial in the sense it would reduce the potential for naming clashes. If you did want to discuss it further I'd suggest taking the issue to the new channels repository where it could be part of a larger discussion.

It is trivial to provide custom names for drivers - you can achieve that by calling extend on the channel manager and providing the key and callback for your driver. In fact, Laravel removed the Slack and Nexmo drivers from the core and broke them out into their own packages just this week that demonstrate how you would do it this way.

        Notification::extend('nexmo', function ($app) {
            return new Channels\NexmoSmsChannel(
                new NexmoClient(new NexmoCredentials(
                    $this->app['config']['services.nexmo.key'],
                    $this->app['config']['services.nexmo.secret']
                )),
                $this->app['config']['services.nexmo.sms_from']
            );
        });

See the full service provider for more information. Hope this helps!

skcin7 commented 5 years ago

@dwightwatson Thank you for the response. From what you showed me, I was able to figure out how to achieve this. To anybody else that might also want to know how to achieve this, here's my exact full ApnServiceProvider class:

<?php

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Notification;
use ZendService\Apple\Apns\Client\Message as Client;
use Illuminate\Events\Dispatcher;

class ApnServiceProvider extends ServiceProvider
{
    public function register()
    {
        Notification::extend('apn', function ($app) {
            $config = $app['config'];
            return new ApnChannel(
                new Client(),
                new Dispatcher(),
                new ApnAdapter(),
                new ApnCredentials(
                    $config->get('broadcasting.connections.apn.environment'),
                    $config->get('broadcasting.connections.apn.certificate'),
                    $config->get('broadcasting.connections.apn.pass_phrase')
                )
            );
        });
    }
}

For the record, NO WHERE in the documentation is this currently explained, and a few other people seem to have had this problem without a clear answer ever explained. So hopefully this can be made more clear in the documentation somewhere.

Thank you @dwightwatson