|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Laravel Pushwoosh package. |
| 5 | + * |
| 6 | + * (c) Contextmapp B.V. <support@contextmapp.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Contextmapp\Pushwoosh; |
| 13 | + |
| 14 | +use Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable; |
| 15 | +use Contextmapp\Pushwoosh\Contracts\PushwooshNotification; |
| 16 | +use Contextmapp\Pushwoosh\Exceptions\NotificationFailedException; |
| 17 | +use Gomoob\Pushwoosh\Exception\PushwooshException; |
| 18 | +use Gomoob\Pushwoosh\Model\Notification\Android; |
| 19 | +use Gomoob\Pushwoosh\Model\Notification\IOS; |
| 20 | +use Gomoob\Pushwoosh\Model\Request\CreateMessageRequest; |
| 21 | +use Illuminate\Notifications\Notification; |
| 22 | + |
| 23 | +/** |
| 24 | + * Channel for dispatching notifications through Pushwoosh. |
| 25 | + */ |
| 26 | +class PushwooshChannel |
| 27 | +{ |
| 28 | + const NAME = 'pushwoosh'; |
| 29 | + |
| 30 | + private $manager; |
| 31 | + |
| 32 | + /** |
| 33 | + * PushwooshChannel constructor. |
| 34 | + * |
| 35 | + * @param PushwooshManager $manager |
| 36 | + */ |
| 37 | + public function __construct(PushwooshManager $manager) |
| 38 | + { |
| 39 | + $this->manager = $manager; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Send the notification through the Pushwoosh notification channel. |
| 44 | + * |
| 45 | + * @param mixed $notifiable |
| 46 | + * @param \Illuminate\Notifications\Notification $notification |
| 47 | + * |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function send($notifiable, Notification $notification) |
| 51 | + { |
| 52 | + if ((!$notifiable instanceof PushwooshNotifiable) || (!$notification instanceof PushwooshNotification)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $message = $notification->toPushwoosh($notifiable); |
| 57 | + $request = $this->buildRequest($notifiable, $message); |
| 58 | + |
| 59 | + try { |
| 60 | + $response = $this->manager->application($message->application)->createMessage($request); |
| 61 | + } catch (PushwooshException $e) { |
| 62 | + throw new NotificationFailedException('Failed to send notification to Pushwoosh', 0, $e); |
| 63 | + } |
| 64 | + |
| 65 | + if (false === $response->isOk()) { |
| 66 | + throw new NotificationFailedException('Failed to send notification to Pushwoosh', $response->getStatusCode()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Build the request to send to Pushwoosh. |
| 72 | + * |
| 73 | + * @param \Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable $notifiable |
| 74 | + * @param \Contextmapp\Pushwoosh\PushwooshMessage $message |
| 75 | + * |
| 76 | + * @return \Gomoob\Pushwoosh\Model\Request\CreateMessageRequest |
| 77 | + */ |
| 78 | + private function buildRequest(PushwooshNotifiable $notifiable, PushwooshMessage $message): CreateMessageRequest |
| 79 | + { |
| 80 | + $android = new Android(); |
| 81 | + $ios = new IOS(); |
| 82 | + |
| 83 | + $notification = new \Gomoob\Pushwoosh\Model\Notification\Notification(); |
| 84 | + $notification->setContent($message->content); |
| 85 | + $notification->setData($message->data); |
| 86 | + $notification->setAndroid($android); |
| 87 | + $notification->setIOS($ios); |
| 88 | + |
| 89 | + if ($message->subject) { |
| 90 | + $android->setHeader($message->subject); |
| 91 | + } |
| 92 | + |
| 93 | + if ($message->increaseBadgeNumber) { |
| 94 | + $android->setBadges('+1'); |
| 95 | + $ios->setBadges('+1'); |
| 96 | + } |
| 97 | + |
| 98 | + $notification->setConditions($notifiable->routeNotificationForPushwoosh()); |
| 99 | + |
| 100 | + $request = new CreateMessageRequest(); |
| 101 | + $request->addNotification($notification); |
| 102 | + |
| 103 | + return $request; |
| 104 | + } |
| 105 | +} |
0 commit comments