Advertisement
mauricioribeiro

Untitled

Dec 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Api\Model\Events\Point;
  4.  
  5.  
  6. use Api\Model\Business\Payment\UserBO;
  7. use Api\Model\Entity\Geo\Agroclimapro\Point;
  8. use Api\Model\Entity\Payment\Subscription;
  9. use Api\Model\Repository\Tvs\UserRepository;
  10. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  11.  
  12. class NotifyMobileUsersEvent
  13. {
  14.  
  15.     public function postUpdate(Point $point, LifecycleEventArgs $args, UserRepository $repository, FirebaseNotification $notification)
  16.     {
  17.         $subscription = $point->getSubscription();
  18.         $user = $repository->find($subscription->getUserId());
  19.         $user->getDeviceToken();
  20.  
  21.         if ($user->getDeviceToken()) {
  22.             $notification->send($user->getDeviceToken(), 'POINT', $point);
  23.         }
  24.     }
  25.  
  26. }
  27.  
  28. class FirebaseNotification {
  29.     public function send($deviceToken, $type, $data, $message = null) {
  30.         $messaging = (new Firebase\Factory())->createMessaging();
  31.  
  32.         $message = CloudMessage::withTarget('token', $deviceToken)
  33.             ->withNotification(Notification::create('Agroclima Pro',
  34.                 $message ?? 'Seus pontos foram atualizados. Toque aqui para abrir o app.'))
  35.             ->withData([
  36.                 'type' => $type,
  37.                 'data' => $data
  38.             ]);
  39.  
  40.         $messaging->send($message);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement