Skip to content

Commit

Permalink
Merge pull request #30 from watzenare/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
watzenare committed Jun 23, 2015
2 parents 919e79c + f0c885d commit c4682a7
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 116 deletions.
68 changes: 54 additions & 14 deletions PushApi/Controllers/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class LogController extends Controller
private $message = '';
private $theme;
private $subject;
private $template;
private $redirect;
private $delay;

/**
Expand All @@ -45,16 +47,16 @@ class LogController extends Controller
* @var "user_id"
* @var "channel"
*/
public function sendMessage()
{
public function sendMessage()
{
/**
* The most important first values to check are message and theme because if theme it's
* multicast we don't need to check the other parameters
*/
if (!isset($this->requestParams['message']) || !isset($this->requestParams['theme'])) {
throw new PushApiException(PushApiException::NO_DATA, "Expected case param");
}

$this->message = $this->requestParams['message'];
$this->theme = $this->requestParams['theme'];

Expand All @@ -63,6 +65,20 @@ public function sendMessage()
$this->subject = $this->requestParams['subject'];
}

// Default message to send is set in the "message" value but we can send templates via mail and it is set in "template" value
if (isset($this->requestParams['template'])) {
$this->template = $this->requestParams['template'];
}

/**
* This option could be used if your app is non-native (you are using PhoneGap or another service)
* When notification received by the device, we need to redirect the user between the pages. Using
* this param you can solve this problem.
*/
if (isset($this->requestParams['redirect'])) {
$this->redirect = $this->requestParams['redirect'];
}

// If delay is set, notification will be send after the delay time.
// Delay must be in seconds and a message can't be delayed more than 1 hour.
if (isset($this->requestParams['delay'])) {
Expand Down Expand Up @@ -98,7 +114,7 @@ public function sendMessage()
case Theme::BROADCAST:
$this->broadcastChecker($theme, $log);
break;

default:
throw new PushApiException(PushApiException::INVALID_ACTION);
break;
Expand Down Expand Up @@ -159,7 +175,6 @@ private function unicastChecker($theme, $log)
false
);

// Registering message
$log->theme_id = $theme->id;
$log->user_id = $userId;
$log->message = $this->message;
Expand Down Expand Up @@ -279,21 +294,21 @@ private function preQueuingDecider($preference, $email, $android_id, $ios_id, $m
if (!$multiple) {
// Checking if user wants to recive via smartphone
if ((Preference::SMARTPHONE & $preference) == Preference::SMARTPHONE) {
if ($android_id != 0) {
if (isset($android_id) && !empty($android_id)) {
// Android receivers requires to be stored into an array structure
$this->addToDeviceQueue(array($android_id), QueueController::ANDROID);
}
if ($ios_id != 0) {
if (isset($ios_id) && !empty($ios_id)) {
$this->addToDeviceQueue($ios_id, QueueController::IOS);
}
}
} else {
// Checking if user wants to recive via smartphone
if ((Preference::SMARTPHONE & $preference) == Preference::SMARTPHONE) {
if ($android_id != 0) {
if (isset($android_id) && !empty($android_id)) {
array_push($this->androidUsers, $android_id);
}
if ($ios_id != 0) {
if (isset($ios_id) && !empty($ios_id)) {
array_push($this->iosUsers, $ios_id);
}

Expand Down Expand Up @@ -335,12 +350,37 @@ private function addToDeviceQueue($receiver, $device)
$data["theme"] = $this->theme;
$data["message"] = $this->message;

if (isset($this->subject)) {
$data["subject"] = $this->subject;
}
// Depending of the target device, the standard message can be updated
switch ($device) {
case QueueController::EMAIL:
if (isset($this->subject)) {
$data["subject"] = $this->subject;
}

if (isset($this->delay)) {
$data["delay"] = $this->delay;
if (isset($this->delay)) {
$data["delay"] = $this->delay;
}

// If template is set, it is prefered to use it instead of the plain message
if (isset($this->template)) {
$data["message"] = $this->template;
}
break;

case QueueController::IOS:
case QueueController::ANDROID:
// If set, we can redirect user with non-native apps that are using bowser to display the app
if (isset($this->redirect)) {
$data["redirect"] = $this->redirect;
} else {
// If redirect is not set, we can't generate the push message (we can remove it when we have native apps)
return false;
}
break;

default:
throw new PushApiException(PushApiException::INVALID_ACTION);
break;
}

(new QueueController())->addToQueue($data, $device);
Expand Down
9 changes: 5 additions & 4 deletions PushApi/Controllers/QueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Controlls the various actions that can be done into the queues
*/
class QueueController extends Controller
{
{
const EMAIL = "email";
const ANDROID = "android";
const IOS = "ios";
Expand Down Expand Up @@ -41,6 +41,7 @@ public function addToQueue($data, $target)
return false;
break;
}

return true;
}

Expand All @@ -58,12 +59,12 @@ public function getFromQueue($target)
break;

case self::ANDROID:
$element = $this->redis->lPop(self::ANDROID, 0);
$element = $this->redis->blPop(self::ANDROID, 0);
return json_decode($element[1]);
break;

case self::IOS:
$element = $this->redis->lPop(self::IOS, 0);
$element = $this->redis->blPop(self::IOS, 0);
return json_decode($element[1]);
break;

Expand All @@ -72,4 +73,4 @@ public function getFromQueue($target)
break;
}
}
}
}
Loading

0 comments on commit c4682a7

Please sign in to comment.