Last active
August 29, 2015 14:06
-
-
Save jleyva/682e28bbfbb7d00edefc to your computer and use it in GitHub Desktop.
Message airnotifier send_message callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function send_message($eventdata) { | |
global $CFG; | |
require_once($CFG->libdir . '/filelib.php'); | |
if (!empty($CFG->noemailever)) { | |
// Hidden setting for development sites, set in config.php if needed. | |
debugging('$CFG->noemailever active, no airnotifier message sent.', DEBUG_MINIMAL); | |
return true; | |
} | |
// Skip any messaging suspended and deleted users. | |
if ($eventdata->userto->auth === 'nologin' or | |
$eventdata->userto->suspended or | |
$eventdata->userto->deleted) { | |
return true; | |
} | |
// Site id, to map with Moodle Mobile stored sites. | |
$siteid = md5($CFG->wwwroot . $eventdata->userto->username); | |
// 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(); | |
$devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $eventdata->userto->id); | |
foreach ($devicetokens as $devicetoken) { | |
if (!$devicetoken->enable) { | |
continue; | |
} | |
// Sending the message to the device. | |
$serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/notification/'; | |
$header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname, | |
'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey); | |
$curl = new curl; | |
$curl->setHeader($header); | |
$params = array( | |
'device' => $devicetoken->platform, | |
'token' => $devicetoken->pushid, | |
'extra' => $extra | |
); | |
// JSON POST raw body request. | |
$resp = $curl->post($serverurl, json_encode($params)); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment