Laravel integration for the Pushy SDK including a notification channel.
The preferred way to install this extension is through composer.
With Composer installed, you can then install the extension using the following commands:
$ php composer.phar require jlorente/laravel-pushy
or add
...
"require": {
"jlorente/laravel-pushy": "*"
}
to the require
section of your composer.json
file.
- Register the ServiceProvider in your config/app.php service provider list.
config/app.php
return [
//other stuff
'providers' => [
//other stuff
\Jlorente\Laravel\Pushy\PushyServiceProvider::class,
];
];
- Add the following facade to the $aliases section.
config/app.php
return [
//other stuff
'aliases' => [
//other stuff
'Pushy' => \Jlorente\Laravel\Pushy\Facades\Pushy::class,
];
];
- Publish the package in order to copy the configuration file to the config folder.
$ php artisan vendor:publish --provider=Jlorente\\Laravel\\Pushy\\PushyServiceProvider
- Set the api_key in the config/pushy.php file or use the predefined env variables.
config/pushy.php
return [
'api_key' => 'YOUR_SECRET_API_KEY',
//other configuration
];
or .env
//other configurations
PUSHY_API_KEY=<YOUR_SECRET_API_KEY>
PUSHY_NOTIFICATION_TTL=<YOUR_CUSTOM_VALUE>
You can use the facade alias Pushy to execute api calls. The authentication params will be automaticaly injected.
Pushy::api()->deviceInfo($token);
A notification channel is included in this package and allows you to integrate the Pushy send notifications service with the Laravel notifications.
If you want to send a notification to Pushy, you should define a toPushy method on the notification class. This method will receive a $notifiable entity and should return a Jlorente\Laravel\Pushy\Notifications\Messages\PushyMessage instance or an array with the payload to be sent on the notification:
/**
* Get the PushyMessage that represents the notification.
*
* @param mixed $notifiable
* @return \Jlorente\Laravel\Pushy\Notifications\Messages\PushyMessage|array
*/
public function toPushy($notifiable)
{
return (new PushyMessage)
->setData([
'eventName' => 'my_event_name'
])
->setTimeToLive(3600);
}
Once done, you must add the notification channel in the array of the via() method of the notification:
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [PushyChannel::class];
}
You can find more info about Laravel notifications in this page.
When sending notifications via Pushy channel, the notification system will automatically look for a pushy_token attribute on the notifiable entity. If you would like to customize the token of the device the notification is delivered to, define a routeNotificationForPushy method on the entity:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Route notifications for the Pushy channel.
*
* @param \Illuminate\Notifications\Notification $notification
* @return string
*/
public function routeNotificationForPushy($notification)
{
return $this->device_token;
}
}
You can find more info about Laravel notifications in this page.
Copyright © 2019 José Lorente Martín jose.lorente.martin@gmail.com.
Licensed under the BSD 3-Clause License. See LICENSE.txt for details.