/** * Test call complete event */ public function testSendMessageEventComplete() { if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) { $this->markTestSkipped('Not found package "symfony/event-dispatcher".'); } $message = self::createMessage('Foo'); $this->connection->expects($this->any())->method('is')->will($this->returnValue(true)); $this->connection->expects($this->once())->method('isReadyRead')->will($this->returnValue(false)); $this->connection->expects($this->once())->method('write')->will($this->returnCallback(function ($a) { return mb_strlen($a); })); $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcher', array('dispatch')); $eventDispatcher->expects($this->once())->method('dispatch')->with(NotificationEvents::SEND_MESSAGE_COMPLETE, $this->isInstanceOf('Apple\\ApnPush\\Notification\\Events\\SendMessageCompleteEvent')); $notification = new Notification(); $notification->setPayloadFactory(new PayloadFactory()); $notification->setConnection($this->connection); $notification->setEventDispatcher($eventDispatcher); $notification->send($message); }
if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) { \Demo::error('Please install "symfony/event-dispatcher" for run this demo (Notification with EventDispatcher).'); } use Symfony\Component\EventDispatcher\EventDispatcher; use Apple\ApnPush\Notification\Notification; use Apple\ApnPush\Notification\NotificationEvents; use Apple\ApnPush\Notification\SendException; use Apple\ApnPush\Notification\Connection; // Create event dispatcher $eventDispatcher = new EventDispatcher(); // Add complete listener $eventDispatcher->addListener(NotificationEvents::SEND_MESSAGE_ERROR, function () { print "[*] Error with send message.\n"; }); // Add error listener $eventDispatcher->addListener(NotificationEvents::SEND_MESSAGE_COMPLETE, function () { print "[-] Complete send message.\n"; }); // Create connection $connection = new Connection(CERTIFICATE_FILE, PASS_PHRASE, SANDBOX_MODE); // Create notification $notification = new Notification($connection); // Set event dispatcher $notification->setEventDispatcher($eventDispatcher); // Send correct message $notification->sendMessage(DEVICE_TOKEN, '[Correct] Hello'); try { // Send incorrect message $notification->sendMessage(str_repeat('a', 64), '[Incorrect] Hello'); } catch (SendException $e) { }