Skip to content

Instantly share code, notes, and snippets.

@nguyenphuocnhatthanh
Created June 14, 2018 06:41
Show Gist options
  • Save nguyenphuocnhatthanh/b6bd107902dd35b6d5e8b13b28a6c199 to your computer and use it in GitHub Desktop.
Save nguyenphuocnhatthanh/b6bd107902dd35b6d5e8b13b28a6c199 to your computer and use it in GitHub Desktop.
Firebase cloud php
<?php
namespace App\Services;
use GuzzleHttp\Client;
class FirebaseCloudMessage
{
/**
* @var Client
*/
private $client;
/**
* @var array
*/
private $registrationIds = [];
/**
* @var array
*/
private $notification = [];
/**
* @var array
*/
private $data = [];
/**
* Firebase constructor.
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* @param array $data
* @return $this
*/
public function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param array $registrationIds
* @return $this
*/
public function setRegistrationIds(array $registrationIds)
{
$this->registrationIds = $registrationIds;
return $this;
}
/**
* @param array $notification
* @return $this
*/
public function setNotification(array $notification)
{
$this->notification = $notification;
return $this;
}
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function sendMessage()
{
$body = $this->setBody([
'registration_ids' => $this->registrationIds,
'notification' => $this->notification
]);
$result = $this->client->post(config('fcm.server_send_url'), $body);
\Log::info('Push notify', [$result->getBody()->getContents()]);
}
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function sendMessageIOS()
{
$body = $this->setBody([
'registration_ids' => $this->registrationIds,
'alert' => $this->notification,
'sound' => 'default'
]);
$result = $this->client->post(config('fcm.server_send_url'), $body);
\Log::info('Push notify', [$result->getBody()->getContents()]);
}
/**
* @param $topicId
* @param array $registrationIds
* @return \Psr\Http\Message\ResponseInterface
*/
public function subscriptionTopic($topicId, array $registrationIds)
{
$body = $this->setBody([
'to' => '/topics/' . $topicId,
'registration_tokens' => $registrationIds
]);
return $this->client->post(config('fcm.server_subscription_topic'), $body);
}
/**
* @param $topicId
* @return \Psr\Http\Message\ResponseInterface
*/
public function sendMessageToTopic($topicId)
{
$body = $this->setBody([
'to' => '/topics/' . $topicId,
'notification' => $this->notification,
]);
return $this->client->post(config('fcm.server_send_url'), $body);
}
/**
* @return array
*/
private function setAuth()
{
return [
'headers' => [
'Authorization' => sprintf('key=%s', config('fcm.server_key')),
'Content-Type' => 'application/json'
]
];
}
/**
* @param array $params
* @return array
*/
private function setBody(array $params)
{
if ($this->data) {
$params['data'] = $this->data;
}
if (! empty($params['notification']['body']['data'])) {
$data = $params['notification']['body']['data'];
$data['content'] = $params['notification']['body']['content'];
$params['data'] = $data;
}
$options = [
'body' => json_encode($params)
];
return array_merge($this->setAuth(), $options);
/*return array_merge($this->setAuth(), [
'body' => json_encode($params)
]);*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment