/** * Ребенок пришел/ушел в/из школу - на вход получаешь id ребенка и тип действия * (0-пришел в школу, 1-ушел из школы)Находишь всех родителей ребенка, * и делаешь запрос к серверу GCM как написано вот тут * https://developers.google.com/cloud-messaging/ * @param $childId * @return integer ResponseCode::consts */ public function event($childId, $data) { $child = Children::find($childId); if (!$child) { return ResponseCode::CHILDREN_NOT_FOUND; } $dataArray = json_decode($data, true); $responses = []; $parents = $child->getParents(); foreach ($parents as $parent) { /** * @var $parent Parents */ $tokens = $parent->getTokens(); if ($tokens === null) { $responses[] = ResponseCode::PARENT_NOT_HAVE_TOKEN; continue; } foreach ($tokens as $token) { /** @var $token Tokens */ $resp = CloudMessaging::send($token->token, $dataArray); $code = ResponseCode::fromResponse($resp); if ($code === ResponseCode::NOT_REGISTERED || $code === ResponseCode::UNKNOWN_ERROR) { $token->delete(); } if ($code === ResponseCode::OK) { $message = new Message(); $message->message = $dataArray['message']; $message->child_id = $child->id; $message->parent_id = $parent->id; $message->date = date("Y-m-d H:i:s"); $message->save(); } $responses[] = $code; } } return $responses; }
<?php /** * User: sasik * Date: 7/17/15 * Time: 8:28 PM */ define('APPLICATION_PATH', __DIR__ . '/../'); define('CONFIG', APPLICATION_PATH . '/configs/config.json'); require_once APPLICATION_PATH . "vendor/autoload.php"; use Sasik\Google\CloudMessaging; $config = new \Noodlehaus\Config(CONFIG); define('APP_KEY', $config->get('app_key')); $token = 'APA91bGi5JhZbR5DebXc5HzpcCHHk4Ct_3RBhQwwpxEntOG_nIHNgHvVNUO-SelCsY5s8f638uukDCYC3bxuPeXky0WeHWB8pXUFLB2E7Q5eLEvBp4vwPTU3lEknni9M6mv4VUP1W9iY'; //$token = 'APA91bGgAA6lIMDfReRkB1mEuCmVajg2rcMNWYndf1If3JyTObaTr-dy7rjkHlI1CJL6no_60cV5rMeuVZr_J9OVIkA6XC3kJmEvlroKgf1q2vTVszRzqhP1RlFRwdMPQBe5lDhJp-4W'; //$token = 'APA91bG95sMzVadM-ZOk9o8akiXPG4JthPudKI_u0EgoDjigV_cZL4iKlD5rVGaT6izsztckY4W7nTfcmZpux9vGTVNvE0fBT3_CBoGEVsgce2UfX0t21URkfZT6CYMeWKYp5t_mGMIU'; //$token = 'APA91bG15iDpwV60IQy7LlSCVBBL4dzeL7qeNpfdtIy_K2zfNEhqmT2bHfAJ_NA-ktE2fASzwp-m434w61V8gbzZJILyAT0RjeEkHMIiLoC719vPmCuy4zFW0yudOd7Eojz_jKTJ59ZM'; $resp = CloudMessaging::send($token, ['message' => 'hi mother f****r', 'timestamp' => time(), 'type' => rand(0, 1)]); dump($resp->getBody()->getContents()); dump($resp->getStatusCode()); dump($resp->getHeaders()); //dump(\Sasik\Google\Request::send('abc', ['hi message'])); //dump($resp);