MDL-47163 messages: Upgrade Mobile notifications to use Airnotifier v2

This commit is contained in:
Juan Leyva 2014-09-15 13:46:02 +02:00
parent d07b0302a9
commit 29ad8f9746

View file

@ -62,36 +62,24 @@ class message_output_airnotifier extends message_output {
// Site id, to map with Moodle Mobile stored sites. // Site id, to map with Moodle Mobile stored sites.
$siteid = md5($CFG->wwwroot . $eventdata->userto->username); $siteid = md5($CFG->wwwroot . $eventdata->userto->username);
// Mandatory notification data that need to be sent in the payload. They have variable length. // Airnotifier can handle custom requests using processors (that are Airnotifier plugins).
// We need to take them in consideration to calculate the maximum message size. // In the extra parameter we indicate which processor to use and also additional data to be handled by the processor.
$notificationdata = array( // Here we clone the eventdata object because will be deleting/adding attributes.
"site" => $siteid, $extra = clone $eventdata;
"type" => $eventdata->component . '_' . $eventdata->name,
"device" => "xxxxxxxxxx", // Since at this point we don't know the device, we use a 10 chars device platform.
"notif" => "x", // 1 or 0 wheter is a notification or not (it may be a private message).
"userfrom" => (!empty($eventdata->userfrom)) ? fullname($eventdata->userfrom) : ''
);
// Calculate the size of the message knowing Apple payload must be lower than 256 bytes. // Delete attributes that may content private information.
// Airnotifier using few bytes of the payload, we must limit our message to even less characters. if (!empty($eventdata->userfrom)) {
$maxmsgsize = 205 - core_text::strlen(json_encode($notificationdata)); $extra->userfromid = $eventdata->userfrom->id;
$message = s($eventdata->smallmessage); $extra->userfromfullname = fullname($eventdata->userfrom);
// If the message size is too big make it shorter. unset($extra->userfrom);
if (core_text::strlen($message) >= $maxmsgsize) {
// Cut the message to the maximum possible size. -4 for the the ending 3 dots (...).
$message = core_text::substr($message, 0 , $maxmsgsize - 4);
// We need to check when the message is "escaped" then the message is not too long.
$encodedmsgsize = core_text::strlen(json_encode($message));
if ($encodedmsgsize > $maxmsgsize) {
$totalescapedchar = $encodedmsgsize - core_text::strlen($message);
// Cut the message to the maximum possible size (taking the escaped character in account).
$message = core_text::substr($message, 0 , $maxmsgsize - 4 - $totalescapedchar);
}
$message = $message . '...';
} }
$extra->usertoid = $eventdata->userto->id;
unset($extra->userto);
$extra->processor = 'moodle';
$extra->site = $siteid;
$extra->date = (!empty($eventdata->timecreated)) ? $eventdata->timecreated : time();
$extra->notification = (!empty($eventdata->notification)) ? 1 : 0;
// We are sending to message to all devices. // We are sending to message to all devices.
$airnotifiermanager = new message_airnotifier_manager(); $airnotifiermanager = new message_airnotifier_manager();
@ -109,16 +97,15 @@ class message_output_airnotifier extends message_output {
'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey); 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey);
$curl = new curl; $curl = new curl;
$curl->setHeader($header); $curl->setHeader($header);
$params = array( $params = array(
'alert' => $message,
'date' => (!empty($eventdata->timecreated)) ? $eventdata->timecreated : time(),
'site' => $siteid,
'type' => $eventdata->component . '_' . $eventdata->name,
'userfrom' => (!empty($eventdata->userfrom)) ? fullname($eventdata->userfrom) : '',
'device' => $devicetoken->platform, 'device' => $devicetoken->platform,
'notif' => (!empty($eventdata->notification)) ? '1' : '0', 'token' => $devicetoken->pushid,
'token' => $devicetoken->pushid); 'extra' => $extra
$resp = $curl->post($serverurl, $params); );
// JSON POST raw body request.
$resp = $curl->post($serverurl, json_encode($params));
} }
return true; return true;