示例#1
0
 /**
  * @see AM_Task_Worker_Abstract::_fire()
  * @throws AM_Task_Worker_Exception
  * @return void
  */
 protected function _fire()
 {
     $iIssueId = intval($this->getOption('issue_id'));
     $sMessage = $this->getOption('message');
     $iBadge = intval($this->getOption('badge'));
     $oIssue = AM_Model_Db_Table_Abstract::factory('issue')->findOneBy('id', $iIssueId);
     /* @var $oIssue AM_Model_Db_Issue */
     if (is_null($oIssue)) {
         throw new AM_Task_Worker_Exception('Issue not found');
     }
     $iApplicationId = $oIssue->getApplication()->id;
     if (empty($iApplicationId)) {
         throw new AM_Task_Worker_Exception('Wrong parameters were given');
     }
     $oTokensRow = AM_Model_Db_Table_Abstract::factory('device_token')->getTokens($iApplicationId, AM_Model_Pns_Type::PLATFORM_IOS);
     if (0 === $oTokensRow->count()) {
         $this->finish();
         $this->getLogger()->debug('There are not tokens to notificate');
         return;
     }
     $aSenderTaskOptions = array('message' => $sMessage, 'badge' => $iBadge, 'application_id' => $iApplicationId);
     $aTokensGroups = array_chunk($oTokensRow->toArray(), 100);
     foreach ($aTokensGroups as $aTokensGroup) {
         $aTokens = array();
         foreach ($aTokensGroup as $aToken) {
             $this->getLogger()->debug(sprintf('Preparing message for token apple \'%s\'', $aToken["token"]));
             $aTokens[] = $aToken['token'];
         }
         $aSenderTaskOptions['tokens'] = $aTokens;
         $oTaskSender = new AM_Task_Worker_Notification_Sender_Apple();
         $oTaskSender->setOptions($aSenderTaskOptions);
         $oTaskSender->create();
     }
 }
 public function testShouldSendNotification()
 {
     //GIVEN
     $this->_oWorker = new AM_Task_Worker_Notification_Sender_Apple();
     $this->_oWorker->addOption('message', 'Test message');
     $this->_oWorker->addOption('application_id', 11);
     $this->_oWorker->addOption('tokens', array('1e82db91c7ceddd72bf33d74ae052ac9c84a065b35148ac401388843106a7485'));
     $this->_oWorker->addOption('badge', 0);
     $this->_oWorker->create();
     //THEN
     //Checking certificates
     $this->_oStandardMock->expects($this->at(0))->method('is_readable')->with($this->equalTo('test_path/11_sandbox.pem'))->will($this->returnValue(true));
     //Connecting to APNS
     $this->_oStandardMock->expects($this->any())->method('stream_context_create')->will($this->returnValue(true));
     $this->_oStandardMock->expects($this->any())->method('stream_socket_client')->will($this->returnValue(true));
     //Sending binary notofication
     $this->_oStandardMock->expects($this->any())->method('fwrite')->will($this->returnValue(77));
     //Checking error response from apns
     $this->_oStandardMock->expects($this->any())->method('fread')->will($this->returnValue(pack('CCN', 8, 0, 1)));
     //WHEN
     try {
         $this->_oWorker->run();
     } catch (Exception $oException) {
         $this->fail($oException->getMessage());
     }
 }
示例#3
0
 /**
  * @see AM_Task_Worker_Abstract::_fire()
  * @throws AM_Task_Worker_Exception
  * @return void
  */
 protected function _fire()
 {
     $iIssueId = intval($this->getOption('issue_id'));
     $sMessage = $this->getOption('message');
     $iBadge = intval($this->getOption('badge'));
     $sDeviceToken = $this->getOption('token');
     $this->getLogger()->debug('Token value %s', $sDeviceToken);
     $oIssue = AM_Model_Db_Table_Abstract::factory('issue')->findOneBy('id', $iIssueId);
     /* @var $oIssue AM_Model_Db_Issue */
     if (is_null($oIssue)) {
         throw new AM_Task_Worker_Exception('Issue not found');
     }
     $iApplicationId = $oIssue->getApplication()->id;
     if (empty($iApplicationId)) {
         throw new AM_Task_Worker_Exception('Wrong parameters were given');
     }
     $aSenderTaskOptions = array('message' => $sMessage, 'badge' => $iBadge, 'application_id' => $iApplicationId, 'tokens' => array($sDeviceToken));
     $oTaskSender = new AM_Task_Worker_Notification_Sender_Apple();
     $oTaskSender->setOptions($aSenderTaskOptions);
     $oTaskSender->create();
 }