From 69e5c3ed086d1952a78de8370618f56e521e5d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florent=20Th=C3=A9venet?= Date: Sun, 8 Jun 2014 08:27:07 +0200 Subject: [PATCH] Notifications android : make it work ! --- src/Payutc/Bom/Notification.php | 14 +++--- src/Payutc/Bom/Task.php | 44 ++++++++++++++----- .../Migrations/Version20140608034837.php | 2 +- src/Payutc/Service/NOTIFICATIONS.php | 4 ++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/Payutc/Bom/Notification.php b/src/Payutc/Bom/Notification.php index 4944990..e5243ac 100644 --- a/src/Payutc/Bom/Notification.php +++ b/src/Payutc/Bom/Notification.php @@ -10,8 +10,10 @@ namespace Payutc\Bom; use \Payutc\Exception\NotImplemented; use \Payutc\Exception\MissingConfiguration; -use \Payutc\Db\Dbal; use \Payutc\Exception\NotificationInvalidDeviceType; +use \Payutc\Db\Dbal; +use \Payutc\Config; +use \Httpful\Request; class Notification { public static function addDevice($type, $token, $user) { @@ -56,13 +58,13 @@ public static function send($user_id, $message) { ->select('not_token', 'not_type') ->from('t_notification_not', 'n') ->where('not_user = :user') - ->setParameter('user', $data['tas_user']) + ->setParameter('user', $user_id) ->execute(); while ($device = $qb->fetch()) { switch($device['not_type']) { case 'Android': - Notification::sendToAndroid($device['not_token'], $message); + return Notification::sendToAndroid($device['not_token'], $message); break; default: throw new NotImplement("DeviceType : " . $device['not_type']); @@ -77,13 +79,15 @@ protected static function sendToAndroid($token, $message) { } $res = Request::post('https://android.googleapis.com/gcm/send') + ->useProxy(Config::get('proxy_host'), Config::get('proxy_port'), null, + Config::get('proxy_login'), Config::get('proxy_password')) ->addHeader('Authorization', 'key=' . $key) ->addHeader('Content-Type', 'application/json') - ->body(json_encode(array('data' => $message, + ->body(json_encode(array('data' => array('message' => $message), 'registration_ids' => array($token)))) ->expectsJson() ->send(); - if ($res-> + return $res; } } diff --git a/src/Payutc/Bom/Task.php b/src/Payutc/Bom/Task.php index 6cf4ed8..0d39d43 100644 --- a/src/Payutc/Bom/Task.php +++ b/src/Payutc/Bom/Task.php @@ -10,15 +10,15 @@ namespace Payutc\Bom; use \Payutc\Db\Dbal; use \Payutc\Exception\NotImplemented; -use \Httpful\Request; -class Blocked { +class Task { public static function addNotification($message, $user) { Dbal::conn()->insert('t_task_tas', array('tas_message' => $message, 'tas_user' => $user->getId(), - 'tas_type' => 'notification'), - array('string', 'integer', 'string')); + 'tas_type' => 'notification', + 'tas_created_at' => new \DateTime('now')), + array('string', 'integer', 'string', 'datetime')); } /** @@ -30,10 +30,13 @@ public static function addNotification($message, $user) { * false il n'y a plus de tâches à traiter */ public static function doOne() { - $qb = Dbal::conn()->createQueryBuilder() + $conn = Dbal::conn(); + $conn->beginTransaction(); + + $qb = $conn->createQueryBuilder() ->select('tas_id', 'tas_message', 'tas_user', 'tas_type') ->from('t_task_tas', 't') - ->orderBy('tas_id', 'ACS') + ->orderBy('tas_id', 'ASC') ->setMaxResults(1); $query = $qb->execute(); @@ -41,13 +44,30 @@ public static function doOne() { return false; } - $data = $query->fetch(); - if ($data['tas_type'] == 'notification') { - Notification::send($data['tas_user'], $data['tas_message']); - return true; - } else { - throw new NotImplement("Task type : " . $data['tas_type']); + try { + $data = $query->fetch(); + if ($data['tas_type'] == 'notification') { + Notification::send($data['tas_user'], $data['tas_message']); + } else { + throw new NotImplement("Task type : " . $data['tas_type']); + } + } catch (\Exception $e) { + $qdelete = $conn->createQueryBuilder() + ->delete('t_task_tas') + ->where('tas_id = :id') + ->setParameter('id', $data['tas_id']) + ->execute(); + $conn->commit(); + throw $e; } + + $qdelete = $conn->createQueryBuilder() + ->delete('t_task_tas') + ->where('tas_id = :id') + ->setParameter('id', $data['tas_id']) + ->execute(); + $conn->commit(); + return true; } } diff --git a/src/Payutc/Migrations/Version20140608034837.php b/src/Payutc/Migrations/Version20140608034837.php index 30168d0..d4c9c6a 100644 --- a/src/Payutc/Migrations/Version20140608034837.php +++ b/src/Payutc/Migrations/Version20140608034837.php @@ -14,7 +14,7 @@ public function up(Schema $schema) { $this->addSql("CREATE TABLE t_task_tas ( `tas_id` INT(11) unsigned NOT NULL AUTO_INCREMENT, - `tas_created_at` DATETIME NOT NULL DEFAULT NOW(), + `tas_created_at` DATETIME NOT NULL, `tas_type` VARCHAR(15) NOT NULL COMMENT 'Type de la tâche', `tas_message` VARCHAR(255) COLLATE utf8_general_ci, `tas_user` INT(11) unsigned, diff --git a/src/Payutc/Service/NOTIFICATIONS.php b/src/Payutc/Service/NOTIFICATIONS.php index 44cbcb4..5dfc422 100644 --- a/src/Payutc/Service/NOTIFICATIONS.php +++ b/src/Payutc/Service/NOTIFICATIONS.php @@ -3,6 +3,7 @@ namespace Payutc\Service; use \Payutc\Bom\Notification; +use \Payutc\Bom\Task; use \Payutc\Exception\UserError; use \Payutc\Config; use \Payutc\Log; @@ -22,6 +23,9 @@ public function addDevice($type, $token) { } $id = Notification::addDevice($type, $token, $this->user()); + if ($id) { + Task::addNotification("Votre appareil recevra maintenant des notifications", $this->user()); + } return true; } }