public function onUserLogout($event) { if ($device_token = trim($event->getRequest()->get('device_token'))) { $devices = $this->pushDeviceService->findDeviceBy(['deviceToken' => $device_token, 'userId' => $event->getUser()->getId()]); foreach ($devices as $device) { $this->pushDeviceService->remove($device); } } }
/** * @param $message * @param $recipients * @return bool|int if TRUE - all messages are sent, otherwise number of not delivered messages */ public function push($text, $recipients, array $extra = null, $badge = 0) { $this->openStream(); if (is_bool($recipients) || is_array($recipients) && count($recipients) == 0) { $this->logger->error('Recipient is not a string, integer or array. Or empty array. Message will not be delivered', [$this->loggerContext]); return false; } if (!is_array($recipients)) { $recipients = [$recipients]; } foreach ($recipients as $recipient) { $deviceToken = $recipient->getDeviceToken(); $message = new ApnsMessage($deviceToken); $message->setText($text); // $message->setCustomIdentifier(sprintf("Message-Badge-%03d", $i)); $message->setBadge($badge); $message->setSound(); if (is_array($extra) && count($extra) > 0) { foreach ($extra as $key => $value) { $message->setCustomProperty($key, $value); } } $this->eventDispatcher->dispatch(PushMessageEvents::AFTER_CREATED, new PushMessageEvent($message, $deviceToken)); $this->service->add($message); } $this->service->send(); $this->closeStream(); $aErrorQueue = $this->service->getErrors(); if (!empty($aErrorQueue)) { $this->logger->error(var_export($aErrorQueue, true)); foreach ($aErrorQueue as $errorQuery) { foreach ($errorQuery['ERRORS'] as $error) { if ($error['statusCode'] == 8) { // token not found $invalidDeviceToken = $errorQuery['MESSAGE']->getRecipient(); $this->deviceService->remove($invalidDeviceToken); $this->logger->info('Removing invalid token #' . $invalidDeviceToken); } } } return count($aErrorQueue); } return true; }