Skip to content

Commit

Permalink
Notifications android : make it work !
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Thévenet committed Jun 8, 2014
1 parent 093aea3 commit 69e5c3e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/Payutc/Bom/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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']);
Expand All @@ -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;
}
}
44 changes: 32 additions & 12 deletions src/Payutc/Bom/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand All @@ -30,24 +30,44 @@ 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();

if ($query->rowCount() == 0) {
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;
}
}

2 changes: 1 addition & 1 deletion src/Payutc/Migrations/Version20140608034837.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/Payutc/Service/NOTIFICATIONS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 69e5c3e

Please sign in to comment.