update v1.0.7.9 R.C.

This is a Release Candidate. We are still testing.
This commit is contained in:
Sujit Prasad
2016-08-03 20:04:36 +05:30
parent 8b6b924d09
commit ffa56a43cb
3830 changed files with 181529 additions and 495353 deletions

View File

@@ -0,0 +1,45 @@
<?php namespace LaravelFCM\Sender;
/**
* Class BaseSender
*
* @package LaravelFCM\Sender
*/
abstract class BaseSender {
/**
* Guzzle Client
* @var \Illuminate\Foundation\Application|mixed
*/
protected $client;
/**
* configuration
* @var array
*/
protected $config;
/**
* url
* @var mixed
*/
protected $url;
/**
* BaseSender constructor.
*/
public function __construct()
{
$this->client = app('fcm.client');
$this->config = app('config')->get('fcm.http', []);
$this->url = $this->getUrl();
}
/**
* get the url
*
* @return string
*/
protected abstract function getUrl();
}

View File

@@ -0,0 +1,108 @@
<?php namespace LaravelFCM\Sender;
use LaravelFCM\Request\GroupRequest;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
/**
* Class FCMGroup
*
* @package LaravelFCM\Sender
*/
class FCMGroup extends BaseSender {
const CREATE = "create";
const ADD = "add";
const REMOVE = "remove";
/**
* Create a group
*
* @param $notificationKeyName
* @param array $registrationIds
*
* @return null
*/
public function createGroup($notificationKeyName, array $registrationIds)
{
$request = new GroupRequest(self::CREATE, $notificationKeyName, null, $registrationIds);
$response = $this->client->post($this->url, $request->build());
return $this->getNotificationToken($response);
}
/**
* add registrationId to a existing group
*
* @param $notificationKeyName
* @param $notificationKey
* @param array $registrationIds registrationIds to add
*
* @return null
*/
public function addToGroup($notificationKeyName, $notificationKey, array $registrationIds)
{
$request = new GroupRequest(self::ADD, $notificationKeyName, $notificationKey, $registrationIds);
$response = $this->client->post($this->url, $request->build());
return $this->getNotificationToken($response);
}
/**
* remove registrationId to a existing group
*
* >Note: if you remove all registrationIds the group is automatically deleted
*
* @param $notificationKeyName
* @param $notificationKey
* @param array $registeredIds registrationIds to remove
*
* @return null
*/
public function removeFromGroup($notificationKeyName, $notificationKey, array $registeredIds)
{
$request = new GroupRequest(self::REMOVE, $notificationKeyName, $notificationKey, $registeredIds);
$response = $this->client->post($this->url, $request->build());
return $this->getNotificationToken($response);
}
/**
* @internal
*
* @param GuzzleResponse $response
*
* @return null
*/
private function getNotificationToken(GuzzleResponse $response)
{
if ($this->isValidResponse($response)) {
return null;
}
$json = json_decode($response->getBody(), true);
return $json['notification_key'];
}
/**
* @internal
*
* @param $response
*
* @return bool
*/
public function isValidResponse(GuzzleResponse $response)
{
return $response->getReasonPhrase() != 'OK' || $response->getStatusCode() != 200;
}
/**
* get the url
*
* @return string
*/
protected function getUrl()
{
return $this->config['server_group_url'];
}
}

View File

@@ -0,0 +1,131 @@
<?php namespace LaravelFCM\Sender;
use GuzzleHttp\Exception\ClientException;
use LaravelFCM\FCMRequest;
use LaravelFCM\Message\Options;
use LaravelFCM\Message\PayloadData;
use LaravelFCM\Message\PayloadNotification;
use \GuzzleHttp\Psr7\Response as GuzzleResponse;
use LaravelFCM\Message\Topics;
use LaravelFCM\Request\Request;
use LaravelFCM\Response\DownstreamResponse;
use LaravelFCM\Response\GroupResponse;
use LaravelFCM\Response\TopicResponse;
/**
* Class FCMSender
*
* @package LaravelFCM\Sender
*/
class FCMSender extends BaseSender {
const MAX_TOKEN_PER_REQUEST = 1000;
/**
* send a downstream message to
*
* - a unique device with is registration Token
* - or to multiples devices with an array of registrationIds
*
* @param String|array $to
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return DownstreamResponse|null
*
*/
public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$response = null;
if (is_array($to)) {
$partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
foreach ($partialTokens as $tokens) {
$request = new Request($tokens, $options, $notification, $data);
try {
$responseGuzzle = $this->client->post($this->url, $request->build());
}
catch (ClientException $e) {
$responseGuzzle = $e->getResponse();
}
$responsePartial = new DownstreamResponse($responseGuzzle, $tokens);
if (!$response) {
$response = $responsePartial;
}
else {
$response->merge($responsePartial);
}
}
}
else {
$request = new Request($to, $options, $notification, $data);
$response = $this->client->post($this->url, $request->build());
$response = new DownstreamResponse($response, $to);
}
return $response;
}
/**
* Send a message to a group of devices identified with them notification key
*
* @param $notificationKey
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return GroupResponse
*/
public function sendToGroup($notificationKey, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$request = new Request($notificationKey, $options, $notification, $data);
try {
$response = $this->client->post($this->url, $request->build());
}
catch (ClientException $e) {
$response = $e->getResponse();
}
return new GroupResponse($response, $notificationKey);
}
/**
* Send message devices registered at a or more topics
*
* @param Topics $topics
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return TopicResponse
*/
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$request = new Request(null, $options, $notification, $data, $topics);
try {
$response = $this->client->post($this->url, $request->build());
}
catch (ClientException $e) {
$response = $e->getResponse();
}
return new TopicResponse($response, $topics);
}
/**
* get the url
*
* @return string
*/
protected function getUrl()
{
return $this->config[ 'server_send_url' ];
}
}