Example #1
0
 /**
  * Sends the message via the GCM server.
  *
  * @param mixed $data
  * @param array $registrationIds
  * @param array $options
  * @param string $payload_type
  *
  * @return bool
  */
 public function send($data, array $registrationIds = array(), array $options = array(), $payload_type = 'data')
 {
     $this->responses = array();
     if (!in_array($payload_type, ['data', 'notification'])) {
         $payload_type = 'data';
     }
     $data = array_merge($options, array($payload_type => $data));
     if (isset($options['to'])) {
         $this->responses[] = $this->browser->post($this->apiUrl, $this->getHeaders(), json_encode($data));
     } elseif (count($registrationIds) > 0) {
         // Chunk number of registration ID's according to the maximum allowed by GCM
         $chunks = array_chunk($registrationIds, $this->registrationIdMaxCount);
         // Perform the calls (in parallel)
         foreach ($chunks as $registrationIds) {
             $data['registration_ids'] = $registrationIds;
             $this->responses[] = $this->browser->post($this->apiUrl, $this->getHeaders(), json_encode($data));
         }
     }
     $this->client->flush();
     foreach ($this->responses as $response) {
         $message = json_decode($response->getContent());
         if ($message === null || $message->success == 0 || $message->failure > 0) {
             return false;
         }
     }
     return true;
 }
 public function testMultiCurlExecutesRequestsConcurently()
 {
     $client = new MultiCurl();
     $client->setTimeout(10);
     $calls = array();
     $callback = function ($client, $request, $response, $options, $error) use(&$calls) {
         $calls[] = func_get_args();
     };
     for ($i = 3; $i > 0; $i--) {
         $request = new Request();
         $request->fromUrl($_SERVER['TEST_SERVER'] . '?delay=' . $i);
         $client->send($request, new Response(), array('callback' => $callback));
     }
     $client->flush();
     $this->assertCount(3, $calls);
 }