예제 #1
0
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Create connection
     $certificate = new Certificate($input->getArgument('certificate-file'), $input->getArgument('pass-phrase'));
     $connection = new Connection($certificate, (bool) $input->getOption('sandbox'));
     // Create payload factory
     $payloadFactory = new PayloadFactory();
     // Create notification system
     $notification = new Notification();
     $notification->setPayloadFactory($payloadFactory);
     $notification->setConnection($connection);
     // Create message
     $message = new Message();
     $message->setDeviceToken($input->getArgument('device-token'));
     $apsData = $message->getApsData();
     $apsData->setBody($input->getArgument('message'));
     $apsData->setSound($input->getOption('sound'));
     $apsData->setBadge($input->getOption('badge'));
     // Send message
     try {
         $notification->send($message);
         $output->writeln('<info>Success send push.</info>');
     } catch (\Exception $e) {
         $output->writeln('<error>Error send push notification with message: ' . $e->getMessage() . '.</error>');
     }
 }
예제 #2
0
 /**
  * @param Message $message
  * @param RecipientInterface|AppleDeviceRecipient $recipient
  * @codeCoverageIgnore
  */
 public function send(Message $message, RecipientInterface $recipient)
 {
     $appleMessage = new AppleMessage();
     $appleMessage->setBody((string) $message->getBody());
     $appleMessage->setDeviceToken($recipient->getToken());
     $appleMessage->setCustomData($message->getExtra());
     $notification = new Notification($this->connection);
     $notification->send($appleMessage);
 }
예제 #3
0
<?php

include_once __DIR__ . '/../autoload.php';
if (!interface_exists('Psr\\Log\\LoggerInterface')) {
    \Demo::error('Please install "psr/log" for run this demo (Notification with logger system).');
}
include_once __DIR__ . '/CustomLogger.php';
use Apple\ApnPush\Certificate\Certificate;
use Apple\ApnPush\Notification\Notification;
use Apple\ApnPush\Notification\Connection;
use Apple\ApnPush\Notification\SendException;
// Create connection
$certificate = new Certificate(CERTIFICATE_FILE, PASS_PHRASE);
$connection = new Connection($certificate, SANDBOX_MODE);
// Create custom logger
$logger = new CustomLogger();
// Create notification
$notification = new Notification();
$notification->setLogger($logger)->setConnection($connection);
// Send to correct device token
$notification->sendMessage(DEVICE_TOKEN, '[Correct] Hello');
try {
    // Send to incorrect device token
    $notification->sendMessage(str_repeat('a', 64), '[Incorrect] Hello');
} catch (SendException $e) {
}
$notification->sendMessage(DEVICE_TOKEN, '[Correct] Hello 2 ;)');
예제 #4
0
 /**
  * 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) {
}