web/lib/Zend/Mobile/Push/Gcm.php
changeset 1230 68c69c656a2c
parent 808 6b6c2214f778
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Mobile
    16  * @package    Zend_Mobile
    17  * @subpackage Zend_Mobile_Push
    17  * @subpackage Zend_Mobile_Push
    18  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id$
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /** Zend_Http_Client **/
    23 /** Zend_Http_Client **/
    36  * GCM Push
    36  * GCM Push
    37  *
    37  *
    38  * @category   Zend
    38  * @category   Zend
    39  * @package    Zend_Mobile
    39  * @package    Zend_Mobile
    40  * @subpackage Zend_Mobile_Push
    40  * @subpackage Zend_Mobile_Push
    41  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  * @version    $Id$
    43  * @version    $Id$
    44  */
    44  */
    45 class Zend_Mobile_Push_Gcm extends Zend_Mobile_Push_Abstract
    45 class Zend_Mobile_Push_Gcm extends Zend_Mobile_Push_Abstract
    46 {
    46 {
    51     const SERVER_URI = 'https://android.googleapis.com/gcm/send';
    51     const SERVER_URI = 'https://android.googleapis.com/gcm/send';
    52 
    52 
    53     /**
    53     /**
    54      * Http Client
    54      * Http Client
    55      *
    55      *
    56      * @var Client
    56      * @var Zend_Http_Client
    57      */
    57      */
    58     protected $_httpClient;
    58     protected $_httpClient;
    59 
    59 
    60     /**
    60     /**
    61      * API Key
    61      * API Key
   118     }
   118     }
   119 
   119 
   120     /**
   120     /**
   121      * Send Message
   121      * Send Message
   122      *
   122      *
   123      * @param Zend_Mobile_Push_Message_Gcm $message
   123      * @param  Zend_Mobile_Push_Message_Abstract $message
       
   124      * @throws Zend_Http_Client_Exception
       
   125      * @throws Zend_Mobile_Push_Exception
       
   126      * @throws Zend_Mobile_Push_Exception_InvalidAuthToken
       
   127      * @throws Zend_Mobile_Push_Exception_InvalidPayload
       
   128      * @throws Zend_Mobile_Push_Exception_ServerUnavailable
   124      * @return Zend_Mobile_Push_Response_Gcm
   129      * @return Zend_Mobile_Push_Response_Gcm
   125      * @throws Zend_Mobile_Push_Exception
       
   126      */
   130      */
   127     public function send(Zend_Mobile_Push_Message_Abstract $message)
   131     public function send(Zend_Mobile_Push_Message_Abstract $message)
   128     {
   132     {
   129         if (!$message->validate()) {
   133         if (!$message->validate()) {
   130             throw new Zend_Mobile_Push_Exception('The message is not valid.');
   134             throw new Zend_Mobile_Push_Exception('The message is not valid.');
   133         $this->connect();
   137         $this->connect();
   134 
   138 
   135         $client = $this->getHttpClient();
   139         $client = $this->getHttpClient();
   136         $client->setUri(self::SERVER_URI);
   140         $client->setUri(self::SERVER_URI);
   137         $client->setHeaders('Authorization', 'key=' . $this->getApiKey());
   141         $client->setHeaders('Authorization', 'key=' . $this->getApiKey());
   138 
       
   139         $json = array('registration_ids' => $message->getToken());
       
   140         if ($data = $message->getData()) {
       
   141             $json['data'] = $data;
       
   142         }
       
   143         if ($id = $message->getId()) {
       
   144             $json['id'] = $id;
       
   145         }
       
   146 
   142 
   147         $response = $client->setRawData($message->toJson(), 'application/json')
   143         $response = $client->setRawData($message->toJson(), 'application/json')
   148                            ->request('POST');
   144                            ->request('POST');
   149         $this->close();
   145         $this->close();
   150 
   146