/** * Envia a notificação para dispositivos Android * * @param Device[] $devices * Dispositivos * @param Message $message * Notificação * @param NotificationResult $notificationResult * Resultado do envio da notificação * @param PushManager $pushManager * Gerenciador de push */ public static function send($devices, $message, $notificationResult, $pushManager) { if (iterator_count($devices->getIterator()) > 0) { try { $gcmAdapter = new GcmAdapter(array('apiKey' => ANDROID_API_KEY)); // envia as notificações $push = new Push($gcmAdapter, $devices, $message); $pushManager->add($push); $pushManager->push(); // obtém a resposta do envio do envio $response = $gcmAdapter->getResponse(); // dispositivo que não receberam as notificações e motivo $failureResults = $response->getResult(Response::RESULT_ERROR); $failureDevices = AndroidPushController::handleFailureResult($failureResults); // dispositivo que tiveram seus identificadores modificados $canonicalResults = $response->getResult(Response::RESULT_CANONICAL); AndroidPushController::handleCanonicalResult($canonicalResults); $notificationResult->addDevicesNotNotified($failureDevices); } catch (\Exception $e) { global $log; $log->Error($e); $notificationResult->setAndroidFailed(true); $notificationResult->setAndroidFailureReason($e->getMessage()); } } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $adapter = $this->getReadyAdapter($input, $output); $pushManager = new PushManager($input->getOption('env')); $message = new Message($input->getArgument('message')); $push = new Push($adapter, new Device($input->getArgument('token')), $message); $pushManager->add($push); $pushManager->push(); }
/** * Send Push Notification * * @param $message * @param array $options * @return $this */ public function send($message, $options = array()) { //Build Message $this->message = $message instanceof Message ? $message : new Message($message, $options); //Create Push $push = new Push($this->adapter, $this->addresses, $this->message); //Add Push to Manager $this->pushManager->add($push); //Send Notification $this->pushManager->push(); return $this; }
private function sendApnsNotification($tokens, $apnsMessage) { $apnsDevices = []; foreach ($tokens as $token) { $apnsDevices[] = new Device($token, array('badge' => 1, 'sound' => 'chime.aiff')); } $devices = new DeviceCollection($apnsDevices); $message = new Message($apnsMessage); $push = new Push($this->apnsAdapter, $devices, $message); if ($this->env == 'prod') { $pushManager = new PushManager(PushManager::ENVIRONMENT_PROD); } else { $pushManager = new PushManager(PushManager::ENVIRONMENT_DEV); } $pushManager->add($push); $pushManager->push(); //$this->pushManager->add($push); //$this->pushManager->push(); }
/** * Sends a notification. * * @param PushNotifications_NotificationModel $notification * * @return DeviceCollection[] */ public function sendNotification(PushNotifications_NotificationModel $notification) { // Get max power craft()->config->maxPowerCaptain(); // Determine environment $environment = craft()->config->get('devMode') ? PushManager::ENVIRONMENT_DEV : PushManager::ENVIRONMENT_PROD; // Get app $app = $notification->getApp(); // Start pushmanager $pushManager = new PushManager($environment); // Gather notified devices $notified = array(); // Loop through platforms foreach ($app->getEnabledPlatforms() as $platform => $setting) { // Gather devices $devices = array(); $notified[$platform] = 0; // Get devices by platform $criteria = craft()->elements->getCriteria('PushNotifications_Device'); $criteria->app = $app->handle; $criteria->platform = $platform; // Check if we have results if ($criteria->count()) { // Loop through devices foreach ($criteria->find() as $device) { // Grab device instance $devices[] = new Device($device->token); $notified[$platform]++; } // Parse device collection $devices = new DeviceCollection($devices); // Set the push message and the options $message = new Message($notification->body, array('title' => $notification->title, 'custom' => array('command' => $notification->command))); // Finally, create and add the push to the manager, and push it! $push = new Push($this->getAdapter($platform, $setting), $devices, $message); $pushManager->add($push); $pushManager->push(); } } // Returns a collection of notified devices per platform return $notified; }
<?php require_once 'vendor/autoload.php'; use Sly\NotificationPusher\PushManager, Sly\NotificationPusher\Adapter\Apns as ApnsAdapter, Sly\NotificationPusher\Collection\DeviceCollection, Sly\NotificationPusher\Model\Device, Sly\NotificationPusher\Model\Message, Sly\NotificationPusher\Model\Push; // First, instantiate the manager. // // Example for production environment: // $pushManager = new PushManager(PushManager::ENVIRONMENT_PRODUCTION); // // Development one by default (without argument). $pushManager = new PushManager(PushManager::ENVIRONMENT_DEV); // Then declare an adapter. $apnsAdapter = new ApnsAdapter(array('certificate' => 'cer/Certificates.pem')); // Set the device(s) to push the notification to. $devices = new DeviceCollection(array(new Device('1dbf2cf3bce3a40c6f4e8a19b9ce127553d0409b114c26e2dfd90a27720e66ed'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('3362cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091499'), new Device('2262cd42ec2fafdef5ebc996d98823aa2430997882d0dabccb1ae94fc6091415'))); // Then, create the push skel. $message = new Message('Vin driver arrived.'); // Finally, create and add the push to the manager, and push it! $push = new Push($apnsAdapter, $devices, $message); $pushManager->add($push); $pushManager->push();
function sendAPNMessageTest($registration_ids, $txt) { $pushManager = new PushManager(PushManager::ENVIRONMENT_DEV); $apnsAdapter = new ApnsAdapter(array('certificate' => 'certs/PemCertificateNew.pem', 'passPhrase' => 'apple')); $devices = []; foreach ($registration_ids as $rid) { $devices[] = new Device($rid); } $devCollection = new DeviceCollection($devices); $message = new Message($txt); // Finally, create and add the push to the manager, and push it! $push = new Push($apnsAdapter, $devCollection, $message); $pushManager->add($push); $pushManager->push(); // Returns a collection of notified devices }
public function testPush() { $this->if($this->mockGenerator()->orphanize('__construct'))->and($this->mockClass('\\Sly\\NotificationPusher\\Adapter\\Apns', '\\Mock'))->and($apnsAdapter = new \Mock\Apns())->and($apnsAdapter->getMockController()->push = true)->and($this->mockGenerator()->orphanize('__construct'))->and($this->mockClass('\\Sly\\NotificationPusher\\Model\\Push', '\\Mock'))->and($push = new \Mock\Push())->and($push->getMockController()->getMessage = new BaseMessage('Test'))->and($push->getMockController()->getDevices = new BaseDeviceCollection(array(new BaseDevice(self::APNS_TOKEN_EXAMPLE))))->and($push->getMockController()->getAdapter = $apnsAdapter)->and($object = new TestedModel())->and($object->add($push))->object($object->push())->isInstanceOf('\\Sly\\NotificationPusher\\Collection\\PushCollection')->hasSize(1); }