/**
  * @inherit
  */
 public function sendMulti($tokens, $text, $payloadData = [], $args = [])
 {
     $tokens = is_array($tokens) ? $tokens : [$tokens];
     // check if its dry run or not
     if ($this->dryRun === true) {
         $this->log($tokens, $text, $payloadData, $args);
         $this->success = true;
         return null;
     }
     $message = new \PHP_GCM\Message();
     foreach ($args as $method => $value) {
         $value = is_array($value) ? $value : [$value];
         call_user_func_array([$message, $method], $value);
     }
     // set a custom payload data
     $payloadData['message'] = $text;
     foreach ($payloadData as $key => $value) {
         $message->addData($key, $value);
     }
     try {
         // send a message
         $result = $this->getClient()->sendMulti($message, $tokens, $this->retryTimes);
         if (is_array($result->getResults())) {
             /** @var \PHP_GCM\Result $phpGcmResult */
             foreach ($result->getResults() as $phpGcmResult) {
                 if ($phpGcmResult->getErrorCode()) {
                     $this->errors[] = $phpGcmResult->getErrorCode();
                 }
             }
         }
         $this->success = $result->getSuccess();
     } catch (\InvalidArgumentException $e) {
         $this->errors[] = $e->getMessage();
         // $deviceRegistrationId was null
     } catch (\PHP_GCM\InvalidRequestException $e) {
         if ($e->getMessage()) {
             $this->errors[] = $e->getMessage();
         } else {
             $this->errors[] = sprintf("Received error code %s from GCM Service", $e->getCode());
         }
         // server returned HTTP code other than 200 or 503
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
         // message could not be sent
     }
     return $message;
 }
示例#2
0
 /**
  * send a push notification for android using GCM client
  *
  * Usage 1:
  * <code>
  * $this->sendMulti('some-valid-token','some-message',
  * array(
  *   'custom_data_key_1'=>'custom_data_value_1',
  *   'custom_data_key_2'=>'custom_data_value_2',
  * ));
  * </code>
  *
  * Usage 2:
  * <code>
  * $this->sendMulti(array('valid-token-1','valid-token-2','valid-token-3'),'some-message',
  * array(
  *   'custom_data_key_1'=>'custom_data_value_1',
  *   'custom_data_key_2'=>'custom_data_value_2',
  * ));
  * </code>
  * @param string|array $tokens
  * @param $text
  * @param array $payloadData
  * @param array $args
  * @return null|\PHP_GCM\Message
  */
 public function sendMulti($tokens, $text, $payloadData = array(), $args = array())
 {
     $tokens = is_array($tokens) ? $tokens : array($tokens);
     // check if its dry run or not
     if ($this->dryRun === true) {
         $this->log($tokens, $text, $payloadData, $args);
         $this->success = true;
         return null;
     }
     $message = new PHP_GCM\Message();
     foreach ($args as $method => $value) {
         $value = is_array($value) ? $value : array($value);
         call_user_func_array(array($message, $method), $value);
     }
     // set a custom payload data
     $payloadData['message'] = $text;
     foreach ($payloadData as $key => $value) {
         $message->addData($key, $value);
     }
     try {
         // send a message
         $result = $this->getClient()->sendMulti($message, $tokens, $this->retryTimes);
         $this->success = $result->getSuccess();
     } catch (InvalidArgumentException $e) {
         $this->errors[] = $e->getMessage();
         // $deviceRegistrationId was null
     } catch (PHP_GCM\InvalidRequestException $e) {
         $this->errors[] = $e->getMessage();
         // server returned HTTP code other than 200 or 503
     } catch (CException $e) {
         $this->errors[] = $e->getMessage();
         // message could not be sent
     }
     return $message;
 }