Devices
Class Lib\Device
Introduction
This class is useful for retrieving information about the device that called the current webservice (plateform, deviceuid)
Note
The main asset of this class is to recover the deviceuid. This can be useful, for example, to associate the device with a user, or to send notifications to this device.
Methods
getInstance
<?php public static function \Lib\Device::getInstance()
Description
Creates an instance of Device class.
get
<?php public function get()
Description
Fetch device properties such as :
deviceuid
platform
(iOS, Android)
Return
A string representing the value property or null if the property does not exist.
isPushable
<?php public function isPushable()
Description
Returns if push notifications are enabled on the device.
Return
Boolean : true
or false
Examples
A POST webservice that retrieves a user associated with a database deviceuid and sends it a notification to a date passed as a parameter.
(See the "Push Notifications" section for more details)
<?php
namespace App\Modules\Users;
use Lib\Controller\WSController;
class Send
{
public static function post(WSController $controller, $params = array())
{
$dataRequest = \Lib\DataRequest::getInstance();
$post = $dataRequest->get('post');
$device = \Lib\Device::getInstance();
$deviceuid = $device->get('deviceuid');
if (empty($post['date'])) {
return \Lib\Tools::formatError('date_missing', true, 1, 422, 100);
}
// retrieve the user in database
$db = \Lib\DB::getInstance();
$db->query('
SELECT
id, firstname, lastname
FROM
`user`
WHERE
`deviceuid` = :deviceuid
AND active = 1
');
$db->bind('deviceuid', $deviceuid);
$user = $db->fetch();
if(empty($user)){
return \Lib\Tools::formatError('user_not_found', true, 1, 404, 100);
}
// See the "Pushs Notifications" section for more details on the code below
$push_params['target'] = array($freeter->getUsersId()->getDeviceuid());
$push_params['badge'] = "1";
$push_params['url'] = "";
$push_params['sound'] = "";
$push_params['inapp'] = "";
$push_params['expire_soft'] = "";
$push_params['expire_hard'] = "";
$push_params['priority'] = "";
$push_params['datas']['is_notif_from_ws'] = '1';
$push_params['datas']['type'] = 'sent';
$push_params['datas']['id_user'] = (int)$user['id'];
$date = new \DateTime($post['date']);
$date->setTimezone(new \DateTimeZone('Europe/Paris'));
$message = "Hello " . $user['firstname'] . ' ' . $user['lastname'];
\Lib\Push::send($message, $push_params, $date->format('Y-m-d H:i:s'));
return array(
"response" => "ok"
);
}
}
Updated about 7 years ago