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);
         }
     }
 }
 public function onOAuthServerTokenIssued(OAuthServerEvent $event)
 {
     if ($device_token = $event->getRequest()->get('device_token')) {
         $deviceOS = null;
         if ($this->pushDeviceService->detect->match("iOS")) {
             $deviceOS = "iOS";
         } elseif ($this->pushDeviceService->detect->match("AndroidOS")) {
             $deviceOS = "AndroidOS";
         }
         if (null === $deviceOS) {
             $this->logger->error('Cannot detect device OS', ['PushNotification']);
             return;
         }
         $response = json_decode($event->getResponse()->getContent(), true);
         $token = $this->accessTokenManager->findTokenByToken($response['access_token']);
         $this->pushDeviceService->upsert($device_token, $deviceOS, $token->getUser()->getId());
     }
 }
 /**
  * @param $message
  * @param $users
  * @param null $extra
  * @return bool|int     Return TRUE if all users get messages, otherwise number of user not received message
  */
 public function send($message, $users, $extra = null, $badge = 0)
 {
     if (!is_array($users)) {
         $users = [$users];
     }
     $sentNum = 0;
     foreach ($users as $user) {
         $devices = $this->pushDeviceService->findDevicesByUser($user);
         $atLeastOneSent = false;
         if (count($devices) > 0) {
             foreach ($devices as $device) {
                 // TODO: save to DB sent notifications
                 if ($device->isiOS()) {
                     if ($this->container->has('push_apns.service')) {
                         $res = $this->container->get('push_apns.service')->push($message, $device, $extra, $badge);
                         $devicesToLog = $device->getDeviceToken();
                         if (true === $res) {
                             $this->logger->info('Message: ' . $message . ', was sent to device#: ' . $devicesToLog, ['PushNotification']);
                             $atLeastOneSent = true;
                         } else {
                             $this->logger->warn('Error occurred while sending message: ' . $message . ', to device#: ' . $devicesToLog, ['PushNotification']);
                         }
                     } else {
                         $this->logger->error('Notification cannot be send as service push_apns.service is not available', ['PushNotification']);
                     }
                 } elseif ($device->isAndroidOS()) {
                     // TODO: TBD
                 }
             }
         } else {
             $this->logger->warn('User #' . $user . ' has no registered devices', ['PushNotification']);
         }
         if ($atLeastOneSent) {
             ++$sentNum;
         }
     }
     if (count($users) == $sentNum) {
         return true;
     } else {
         return count($users) - $sentNum;
     }
 }
Ejemplo n.º 4
0
 /**
  * @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;
 }