MDL-44342 airnotifier: Add support for Android notifications
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
* @since Moodle 2.7
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
/**
|
||||
* Airnotifier helper manager class
|
||||
@@ -47,16 +48,8 @@ class message_airnotifier_manager {
|
||||
}
|
||||
|
||||
$config = new stdClass();
|
||||
|
||||
// The URL to use for resource changes.
|
||||
if (!isset($config->resturl)) {
|
||||
$config->resturl = '/message/output/airnotifier/rest.php';
|
||||
}
|
||||
|
||||
// Any additional parameters which need to be included on page submission.
|
||||
if (!isset($config->pageparams)) {
|
||||
$config->pageparams = array();
|
||||
}
|
||||
$config->resturl = '/message/output/airnotifier/rest.php';
|
||||
$config->pageparams = array();
|
||||
|
||||
// Include toolboxes.
|
||||
$PAGE->requires->yui_module('moodle-message_airnotifier-toolboxes', 'M.message.init_device_toolbox', array(array(
|
||||
@@ -92,28 +85,29 @@ class message_airnotifier_manager {
|
||||
|
||||
// First, we look all the devices registered for this user in the Moodle core.
|
||||
// We are going to allow only ios devices (since these are the ones that supports PUSH notifications).
|
||||
if ($userdevices = $DB->get_records('user_devices', $params)) {
|
||||
foreach ($userdevices as $device) {
|
||||
if (core_text::strtolower($device->platform) == 'ios') {
|
||||
// Check if the device is known by airnotifier.
|
||||
if (!$airnotifierdev = $DB->get_record('message_airnotifier_devices', array('userdeviceid' => $device->id))) {
|
||||
// We have to create the device token in airnotifier.
|
||||
$userdevices = $DB->get_records('user_devices', $params);
|
||||
foreach ($userdevices as $device) {
|
||||
if (core_text::strtolower($device->platform)) {
|
||||
// Check if the device is known by airnotifier.
|
||||
if (!$airnotifierdev = $DB->get_record('message_airnotifier_devices',
|
||||
array('userdeviceid' => $device->id))) {
|
||||
|
||||
if (! $this->create_token($device->pushid)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$airnotifierdev = new stdClass;
|
||||
$airnotifierdev->userdeviceid = $device->id;
|
||||
$airnotifierdev->enable = 1;
|
||||
$airnotifierdev->id = $DB->insert_record('message_airnotifier_devices', $airnotifierdev);
|
||||
// We have to create the device token in airnotifier.
|
||||
if (! $this->create_token($device->pushid)) {
|
||||
continue;
|
||||
}
|
||||
$device->id = $airnotifierdev->id;
|
||||
$device->enable = $airnotifierdev->enable;
|
||||
$devices[] = $device;
|
||||
|
||||
$airnotifierdev = new stdClass;
|
||||
$airnotifierdev->userdeviceid = $device->id;
|
||||
$airnotifierdev->enable = 1;
|
||||
$airnotifierdev->id = $DB->insert_record('message_airnotifier_devices', $airnotifierdev);
|
||||
}
|
||||
$device->id = $airnotifierdev->id;
|
||||
$device->enable = $airnotifierdev->enable;
|
||||
$devices[] = $device;
|
||||
}
|
||||
}
|
||||
|
||||
return $devices;
|
||||
}
|
||||
|
||||
@@ -195,4 +189,3 @@ class message_airnotifier_manager {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -63,9 +63,11 @@ class message_output_airnotifier extends message_output {
|
||||
|
||||
// Mandatory notification data that need to be sent in the payload. They have variable length.
|
||||
// We need to take them in consideration to calculate the maximum message size.
|
||||
// Since at this point we don't know the device, we use a 10 chars device platform.
|
||||
$notificationdata = array(
|
||||
"site" => $siteid,
|
||||
"type" => $eventdata->component . '_' . $eventdata->name,
|
||||
"device" => "xxxxxxxxxx",
|
||||
"userfrom" => fullname($eventdata->userfrom));
|
||||
|
||||
// Calculate the size of the message knowing Apple payload must be lower than 256 bytes.
|
||||
@@ -111,6 +113,7 @@ class message_output_airnotifier extends message_output {
|
||||
'site' => $siteid,
|
||||
'type' => $eventdata->component . '_' . $eventdata->name,
|
||||
'userfrom' => fullname($eventdata->userfrom),
|
||||
'device' => $devicetoken->platform,
|
||||
'token' => $devicetoken->pushid);
|
||||
$resp = $curl->post($serverurl, $params);
|
||||
}
|
||||
@@ -126,6 +129,11 @@ class message_output_airnotifier extends message_output {
|
||||
public function config_form($preferences) {
|
||||
global $CFG, $OUTPUT, $USER, $PAGE;
|
||||
|
||||
$systemcontext = context_system::instance();
|
||||
if (!has_capability('message/airnotifier:managedevice', $systemcontext)) {
|
||||
return get_string('nopermissiontomanagedevices', 'message_airnotifier');
|
||||
}
|
||||
|
||||
if (!$this->is_system_configured()) {
|
||||
return get_string('notconfigured', 'message_airnotifier');
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user