From 29ad8f9746bb38beeb23b75070ece18374b4f424 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 15 Sep 2014 13:46:02 +0200 Subject: [PATCH] MDL-47163 messages: Upgrade Mobile notifications to use Airnotifier v2 --- .../airnotifier/message_output_airnotifier.php | 61 +++++++++------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/message/output/airnotifier/message_output_airnotifier.php b/message/output/airnotifier/message_output_airnotifier.php index 7a2d78d257b..2abd8610c02 100644 --- a/message/output/airnotifier/message_output_airnotifier.php +++ b/message/output/airnotifier/message_output_airnotifier.php @@ -62,36 +62,24 @@ class message_output_airnotifier extends message_output { // Site id, to map with Moodle Mobile stored sites. $siteid = md5($CFG->wwwroot . $eventdata->userto->username); - // 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. - $notificationdata = array( - "site" => $siteid, - "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. - // Airnotifier using few bytes of the payload, we must limit our message to even less characters. - $maxmsgsize = 205 - core_text::strlen(json_encode($notificationdata)); - $message = s($eventdata->smallmessage); - // If the message size is too big make it shorter. - 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 . '...'; + // Airnotifier can handle custom requests using processors (that are Airnotifier plugins). + // In the extra parameter we indicate which processor to use and also additional data to be handled by the processor. + // Here we clone the eventdata object because will be deleting/adding attributes. + $extra = clone $eventdata; + + // Delete attributes that may content private information. + if (!empty($eventdata->userfrom)) { + $extra->userfromid = $eventdata->userfrom->id; + $extra->userfromfullname = fullname($eventdata->userfrom); + unset($extra->userfrom); } + $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. $airnotifiermanager = new message_airnotifier_manager(); @@ -109,16 +97,15 @@ class message_output_airnotifier extends message_output { 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey); $curl = new curl; $curl->setHeader($header); + $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, - 'notif' => (!empty($eventdata->notification)) ? '1' : '0', - 'token' => $devicetoken->pushid); - $resp = $curl->post($serverurl, $params); + 'token' => $devicetoken->pushid, + 'extra' => $extra + ); + + // JSON POST raw body request. + $resp = $curl->post($serverurl, json_encode($params)); } return true; -- 2.11.4.GIT