/** * Envia a notificação * * @param Notification $notification * Notificação a ser enviada * @return NotificationResult Resultado do envio da notificação */ public function send($notification) { // verifica a notificação if (!$notification) { throw new \InvalidArgumentException("A notificação não foi informada ou é inválida."); } $iosDevices = new DeviceCollection(); $androidDevices = new DeviceCollection(); $data = $notification->getData(); if ($data) { $message = new Message($notification->getMessage(), $notification->getData()); } else { $message = new Message($notification->getMessage()); } // separa os dispositivos por tipo foreach ($notification->getDevices() as $device) { switch ($device->getDeviceType()) { case Device::ANDROID: $androidDevices->add(new PusherDevice($device->getToken())); break; case Device::IOS: $iosDevices->add(new PusherDevice($device->getToken())); break; } } $notificationResult = new NotificationResponse(); // envia as notificações AndroidPushController::send($androidDevices, $message, $notificationResult, $this->pushManager); IosPushController::send($iosDevices, $message, $notificationResult, $this->pushManager); return $notificationResult; }
/** * {@inheritdoc} * * @throws \Sly\NotificationPusher\Exception\PushException */ public function push(PushInterface $push) { $client = $this->getOpenedServiceClient(); $pushedDevices = new DeviceCollection(); foreach ($push->getDevices() as $device) { $message = $this->getServiceMessageFromOrigin($device, $push->getMessage()); try { $this->response = $client->send($message); } catch (ServiceRuntimeException $e) { throw new PushException($e->getMessage()); } if (ServiceResponse::RESULT_OK === $this->response->getCode()) { $pushedDevices->add($device); } } return $pushedDevices; }
/** * {@inheritdoc} * * @throws \Sly\NotificationPusher\Exception\PushException */ public function push(PushInterface $push) { $client = $this->getOpenedClient(new ServiceClient()); $pushedDevices = new DeviceCollection(); $tokens = array_chunk($push->getDevices()->getTokens(), 100); foreach ($tokens as $tokensRange) { $message = $this->getServiceMessageFromOrigin($tokensRange, $push->getMessage()); try { $this->response = $client->send($message); } catch (ServiceRuntimeException $e) { throw new PushException($e->getMessage()); } if ((bool) $this->response->getSuccessCount()) { foreach ($tokensRange as $token) { $pushedDevices->add($push->getDevices()->get($token)); } } } return $pushedDevices; }