Browse Source

add socket channel, message, notification and some minor change

mohammad
mahdihty 4 years ago
parent
commit
30430b27e0
  1. 6
      app/Channels/Messages/SocketMessage.php
  2. 34
      app/Channels/SocketChannel.php
  3. 2
      app/Notifications/FcmNotification.php
  4. 52
      app/Notifications/SocketNotification.php
  5. 4
      app/Providers/AppServiceProvider.php
  6. 18
      config/socket.php
  7. 4
      routes/api.php

6
app/Channels/Messages/SocketMessage.php

@ -29,17 +29,13 @@ class SocketMessage
*/
public function to($to)
{
if (is_array($to) && count($to) === 1) {
$this->to = $to[0];
} else {
$this->to = $to;
}
return $this;
}
/**
* Set the data of the FCM message.
* Set the data of the socket message.
*
* @param array $data
* @return $this

34
app/Channels/SocketChannel.php

@ -3,7 +3,6 @@
namespace App\Channels;
use App\Channels\Messages\FcmMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
@ -14,7 +13,7 @@ class SocketChannel
*
* @var string
*/
const API_URI = ''; //todo: fill it
protected $socket_url;
/**
* The HTTP client instance.
@ -29,9 +28,10 @@ class SocketChannel
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http)
public function __construct(HttpClient $http, string $socket_url)
{
$this->http = $http;
$this->socket_url = $socket_url;
}
/**
@ -46,39 +46,17 @@ class SocketChannel
$message = $notification->toSocket($notifiable);
$message->to($notifiable->routeNotificationFor('socket', $notification));
$message->to('1');
if (! $message->to) {
return;
}
$this->http->post(self::API_URI, [
$this->http->post($this->socket_url . '/emit/' . $message->to, [
'headers' => [
'Authorization' => "key={$this->apiKey}",
'Content-Type' => 'application/json',
],
'json' => $this->buildJsonPayload($message),
'data' => [$message->data],
]);
}
protected function buildJsonPayload(FcmMessage $message)
{
$payload = array_filter([
'priority' => $message->priority,
'data' => $message->data,
'notification' => $message->notification,
'condition' => $message->condition,
]);
if ($message->topic) {
$payload['to'] = "/topics/{$message->topic}";
} else {
if (is_array($message->to)) {
$payload['registration_ids'] = $message->to;
} else {
$payload['to'] = $message->to;
}
}
return $payload;
}
}

2
app/Notifications/FcmNotification.php

@ -34,7 +34,7 @@ class FcmNotification extends Notification
}
/**
* Get the voice representation of the notification.
* Get the fcm representation of the notification.
*
* @param mixed $notifiable
* @return FcmMessage

52
app/Notifications/SocketNotification.php

@ -0,0 +1,52 @@
<?php
namespace App\Notifications;
use App\Channels\Messages\SocketMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class SocketNotification 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 ['socket'];
}
/**
* Get the socket representation of the notification.
*
* @param mixed $notifiable
* @return SocketMessage
*/
public function toSocket($notifiable)
{
return (new SocketMessage())
->data([
'title' => $this->message['title'],
'body' => $this->message['body'],
]);
}
}

4
app/Providers/AppServiceProvider.php

@ -3,6 +3,7 @@
namespace App\Providers;
use App\Channels\FcmChannel;
use App\Channels\SocketChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
@ -21,6 +22,9 @@ class AppServiceProvider extends ServiceProvider
$service->extend('fcm', function ($app) {
return new FcmChannel(new HttpClient, config('fcm.key'));
});
$service->extend('socket', function ($app) {
return new SocketChannel(new HttpClient, config('socket.url'));
});
});
}

18
config/socket.php

@ -0,0 +1,18 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| FCM API Key
|--------------------------------------------------------------------------
|
| This key allows you to send Push Notifications. To obtain this key go
| to the porject settings and click on the "Cloud Messaging" tab, now
| copy the API Key of "Legacy server key".
|
*/
'url' => env('SOCKET_URL'),
];

4
routes/api.php

@ -2,6 +2,10 @@
/** @var \Illuminate\Routing\$router */
$router->get('/ntest', function () {
$user = \App\Models\User::find(1);
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss']));
});
$router->group(['prefix' => 'actions'], function () use ($router) {
$router->group(['prefix' => 'businesses'], function () use ($router) {
$router->group(['prefix' => '{business}', 'middleware' => 'bindBusiness'], function () use ($router) {

Loading…
Cancel
Save