mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-47163 messages: Upgrade Mobile notifications to use Airnotifier v2
This commit is contained in:
parent
d07b0302a9
commit
29ad8f9746
@ -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) : ''
|
||||
);
|
||||
// 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;
|
||||
|
||||
// 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 . '...';
|
||||
// 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user