Example #1
0
 /**
  * Send message.
  *
  * @throws Tiqr_Message_Exception_AuthFailure
  * @throws Tiqr_Message_Exception_SendFailure
  * @throws Tiqr_Message_Exception_InvalidDevice    
  *
  */
 public function send()
 {
     $service = self::_getService($this->getOptions());
     $message = new Zend_Mobile_Push_Message_Apns();
     $message->setToken($this->getAddress());
     $message->setId($this->getId());
     $message->setAlert($this->getText());
     $message->setSound('default');
     $message->setExpire(30);
     foreach ($this->getCustomProperties() as $name => $value) {
         $message->addCustomData($name, $value);
     }
     try {
         $service->send($message);
     } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
         throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true, $e);
     } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
         throw new Tiqr_Message_Exception_InvalidDevice("Invalid token", $e);
     } catch (Zend_Mobile_Push_Exception_InvalidPayload $e) {
         throw new Tiqr_Message_Exception_SendFailure("Invalid payload", false, $e);
     } catch (Zend_Mobile_Push_Exception_InvalidTopic $e) {
         throw new Tiqr_Message_Exception_SendFailure("Invalid topic", false, $e);
     } catch (Zend_Mobile_Push_Exception $e) {
         throw new Tiqr_Message_Exception_SendFailure("General send error", false, $e);
     }
     $service->close();
 }
<?php

require_once 'Zend/Mobile/Push/Apns.php';
require_once 'Zend/Mobile/Push/Message/Apns.php';
$message = new Zend_Mobile_Push_Message_Apns();
$message->setAlert('Zend Mobile Push Example');
$message->setBadge(1);
$message->setSound('default');
$message->setId(time());
$message->setToken('ABCDEF0123456789');
$apns = new Zend_Mobile_Push_Apns();
$apns->setCertificate('/path/to/provisioning-certificate.pem');
try {
    $apns->connect(Zend_Mobile_Push_Apns::SERVER_SANDBOX_URI);
} catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
    // you can either attempt to reconnect here or try again later
    exit(1);
} catch (Zend_Mobile_Push_Exception $e) {
    echo 'APNS Connection Error:' . $e->getMessage();
    exit(1);
}
try {
    $apns->send($message);
} catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
    // you would likely want to remove the token from being sent to again
    echo $e->getMessage();
} catch (Zend_Mobile_Push_Exception $e) {
    // all other exceptions only require action to be sent
    echo $e->getMessage();
}
$apns->close();