Endroid\Gcm\Client::send PHP Method

send() public method

Sends the message via the GCM server.
public send ( mixed $data, array $registrationIds = [], array $options = [] ) : boolean
$data mixed
$registrationIds array
$options array
return boolean
    public function send($data, array $registrationIds = array(), array $options = array())
    {
        $this->responses = array();
        $data = array_merge($options, array('data' => $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;
    }

Usage Example

コード例 #1
0
 public static function sendMessageRemoved($message, $userFrom, $userTo)
 {
     // GCM SENDER
     $client = new Client(__API_KEY__);
     // REGISTRATION IDS IN ARRAY
     $registrationIds = [];
     $registrationIds[] = $userFrom->registrationId;
     $registrationIds[] = $userTo->registrationId;
     $data = array('type' => 3, 'id' => $message->id, 'userFrom_id' => $userFrom->id, 'userTo_id' => $userTo->id);
     $options = ['collapse_key' => 'messageRemoved', 'delay_while_idle' => false, 'time_to_live' => 4 * 7 * 24 * 60 * 60, 'restricted_package_name' => 'br.com.thiengo.gcmexample', 'dry_run' => false];
     $client->send($data, $registrationIds, $options);
     // ENVIA A PUSH MESSAGE
     $responses = $client->getResponses();
     // ACESSA A ÚNICA POSIÇÃO POSSÍVEL, PRIMEIRA POSIÇÃO
     foreach ($responses as $response) {
         $response = json_decode($response->getContent());
         // VERIFICA SE HÁ ALGUM CANONICAL_ID, QUE INDICA QUE AO MENOS UM REGISTRATION_ID DEVE SER ATUALIZADO
         if ($response->canonical_ids > 0 || $response->failure > 0) {
             // PERCORRE TODOS OS RESULTADOS VERIFICANDO SE HÁ UM REGISTRATION_ID PARA SER ALTERADO
             for ($i = 0, $tamI = count($response->results); $i < $tamI; $i++) {
                 if (!empty($response->results[$i]->canonical_id)) {
                     // SE HÁ UM NOVO REGISTRATION_ID, ENTÃO ALTERANO BD
                     if ($i == 0) {
                         $userFrom->registrationId = $response->results[$i]->canonical_id;
                         CgdUser::updateRegistrationId($userFrom);
                     } else {
                         $userTo->registrationId = $response->results[$i]->canonical_id;
                         CgdUser::updateRegistrationId($userTo);
                     }
                 } else {
                     if (strcasecmp($response->results[$i]->error, "NotRegistered") == 0) {
                         // DELETE REGISTRO DO BD
                         if ($i == 0) {
                             CgdUser::deleteUser($userFrom);
                         } else {
                             CgdUser::deleteUser($userTo);
                         }
                     }
                 }
             }
         }
     }
 }