/** * Send notification to devices tokens * * @throws InvalidArgumentException * @param Payload $payload * @param array $tokens */ public function send(Payload $payload, $tokens) { if (!count($tokens) > 0) { return true; } //Open connection $this->connect(); // Encode payload as JSON $json_payload = json_encode($payload->getApsFormat()); // Build the binary notification to each token $data = ''; foreach ($tokens as $tk) { $data .= chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $tk)) . pack('n', strlen($json_payload)) . $json_payload; } // Send data to the server return $this->write($data); }
/** * Send notification to devices tokens * * @throws InvalidArgumentException * @param AbstractPayload $payload * @param array $tokens */ public function send(Payload $payload, $tokens) { if (!is_array($tokens)) { throw new \InvalidArgumentException('Tokens must be an array'); } if (!count($tokens) > 0) { return true; } $gcm_message = ["registration_ids" => $tokens, "data" => $payload->getGcmFormat()]; $headers = ["Authorization: key=" . Config::get('pushnotification.gcm.apiKey'), "Content-Type: application/json"]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->uri); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($gcm_message)); $result = curl_exec($ch); if ($result === false) { throw new \Exception("Curl failed: " . curl_error($ch)); } curl_close($ch); return $result; }