mahdihty
4 years ago
5 changed files with 156 additions and 4 deletions
-
54app/Channels/Messages/SocketMessage.php
-
84app/Channels/SocketChannel.php
-
16app/Http/Controllers/AuthController.php
-
4app/Models/User.php
-
2routes/api.php
@ -0,0 +1,54 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Channels\Messages; |
|||
|
|||
|
|||
class SocketMessage |
|||
{ |
|||
/** |
|||
* The devices token to send the message from. |
|||
* |
|||
* @var array|string |
|||
*/ |
|||
public $to; |
|||
|
|||
/** |
|||
* The data of the Socket message. |
|||
* |
|||
* @var array |
|||
*/ |
|||
public $data; |
|||
|
|||
|
|||
/** |
|||
* Set the devices token to send the message from. |
|||
* |
|||
* @param array|string $to |
|||
* @return $this |
|||
*/ |
|||
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. |
|||
* |
|||
* @param array $data |
|||
* @return $this |
|||
*/ |
|||
public function data(array $data) |
|||
{ |
|||
$this->data = $data; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,84 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Channels; |
|||
|
|||
use App\Channels\Messages\FcmMessage; |
|||
use Illuminate\Notifications\Notification; |
|||
use GuzzleHttp\Client as HttpClient; |
|||
|
|||
class SocketChannel |
|||
{ |
|||
/** |
|||
* The API URL for Socket. |
|||
* |
|||
* @var string |
|||
*/ |
|||
const API_URI = ''; //todo: fill it
|
|||
|
|||
/** |
|||
* The HTTP client instance. |
|||
* |
|||
* @var \GuzzleHttp\Client |
|||
*/ |
|||
protected $http; |
|||
|
|||
/** |
|||
* Create a new Socket channel instance. |
|||
* |
|||
* @param \GuzzleHttp\Client $http |
|||
* @return void |
|||
*/ |
|||
public function __construct(HttpClient $http) |
|||
{ |
|||
$this->http = $http; |
|||
} |
|||
|
|||
/** |
|||
* Send the given notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @param \Illuminate\Notifications\Notification $notification |
|||
* @return void |
|||
*/ |
|||
public function send($notifiable, Notification $notification) |
|||
{ |
|||
$message = $notification->toSocket($notifiable); |
|||
|
|||
$message->to($notifiable->routeNotificationFor('socket', $notification)); |
|||
|
|||
if (! $message->to) { |
|||
return; |
|||
} |
|||
|
|||
$this->http->post(self::API_URI, [ |
|||
'headers' => [ |
|||
'Authorization' => "key={$this->apiKey}", |
|||
'Content-Type' => 'application/json', |
|||
], |
|||
'json' => $this->buildJsonPayload($message), |
|||
]); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue