Example #1
0
 /**
  * Send message.
  *
  * @throws Tiqr_Message_Exception_AuthFailure
  * @throws Tiqr_Message_Exception_SendFailure
  */
 public function send()
 {
     $service = self::_getService($this->getOptions());
     $data = $this->getCustomProperties();
     $data['text'] = $this->getText();
     $message = new Zend_Mobile_Push_Message_Gcm();
     $message->addToken($this->getAddress());
     $message->setId($this->getId());
     // TODO: GCM equivalent needed?
     $message->setData($data);
     try {
         $response = $service->send($message);
     } catch (Zend_Htpp_Client_Exception $e) {
         throw new Tiqr_Message_Exception_SendFailure("HTTP client error", true, $e);
     } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
         throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true, $e);
     } catch (Zend_Mobile_Push_Exception_InvalidAuthToken $e) {
         throw new Tiqr_Message_Exception_AuthFailure("Invalid token", $e);
     } catch (Zend_Mobile_Push_Exception_InvalidPayload $e) {
         throw new Tiqr_Message_Exception_SendFailure("Payload too large", $e);
     } catch (Zend_Mobile_Push_Exception $e) {
         throw new Tiqr_Message_Exception_SendFailure("General send error", false, $e);
     }
     // handle errors, ignoring registration_id's
     foreach ($response->getResults() as $k => $v) {
         if (isset($v['error'])) {
             throw new Tiqr_Message_Exception_SendFailure("error in GCM response: " . $v['error'], true);
         }
     }
 }
Example #2
0
 /**
  * Correlate Message and Result
  *
  * @return array
  */
 protected function _correlate()
 {
     $results = $this->_results;
     if ($this->_message && $results) {
         $tokens = $this->_message->getToken();
         while ($token = array_shift($tokens)) {
             $results[$token] = array_shift($results);
         }
     }
     return $results;
 }
Example #3
0
 public function testResponse()
 {
     $responseArr = array('results' => array(array('message_id' => '1:234')), 'success' => 1, 'failure' => 0, 'canonical_ids' => 0, 'multicast_id' => '123');
     $response = new Zend_Mobile_Push_Response_Gcm();
     $response->setResponse($responseArr);
     $this->assertEquals($responseArr, $response->getResponse());
     $this->assertEquals(1, $response->getSuccessCount());
     $this->assertEquals(0, $response->getFailureCount());
     $this->assertEquals(0, $response->getCanonicalCount());
     // test results non correlated
     $expected = array(array('message_id' => '1:234'));
     $this->assertEquals($expected, $response->getResults());
     $expected = array(0 => '1:234');
     $this->assertEquals($expected, $response->getResult(Zend_Mobile_Push_Response_Gcm::RESULT_MESSAGE_ID));
     $message = new Zend_Mobile_Push_Message_Gcm();
     $message->setToken(array('ABCDEF'));
     $response->setMessage($message);
     $expected = array('ABCDEF' => '1:234');
     $this->assertEquals($expected, $response->getResult(Zend_Mobile_Push_Response_Gcm::RESULT_MESSAGE_ID));
 }
Example #4
0
 public function testToJsonIntCollapseKeyEncodedAsString()
 {
     $msg = new Zend_Mobile_Push_Message_Gcm();
     $msg->setId(10);
     $this->assertEquals('{"collapse_key":"10"}', $msg->toJson());
 }
<?php

require_once 'Zend/Mobile/Push/Gcm.php';
require_once 'Zend/Mobile/Push/Message/Gcm.php';
$message = new Zend_Mobile_Push_Message_Gcm();
$message->addToken('ABCDEF0123456789');
$message->setData(array('foo' => 'bar', 'bar' => 'foo'));
$gcm = new Zend_Mobile_Push_Gcm();
$gcm->setApiKey('YOUR_API_KEY');
try {
    $response = $gcm->send($message);
} catch (Zend_Mobile_Push_Exception $e) {
    // exceptions require action or implementation of exponential backoff.
    die($e->getMessage());
}
// handle all errors and registration_id's
foreach ($response->getResults() as $k => $v) {
    if (isset($v['registration_id'])) {
        printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
    }
    if (isset($v['error'])) {
        printf("%s had an error of: %s\r\n", $k, $v['error']);
    }
    if (isset($v['message_id'])) {
        printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
    }
}