diff --git a/.styleci.yml b/.styleci.yml index 916d27e..0285f17 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,3 +1 @@ preset: laravel - -linting: true diff --git a/src/OneSignalButton.php b/src/OneSignalButton.php index 28bea10..93fe450 100644 --- a/src/OneSignalButton.php +++ b/src/OneSignalButton.php @@ -65,7 +65,7 @@ public function text($value) public function toArray() { return [ - 'id' => $this->id, + 'id' => $this->id, 'text' => $this->text, 'icon' => $this->icon, ]; diff --git a/src/OneSignalChannel.php b/src/OneSignalChannel.php index 8453430..9622af7 100644 --- a/src/OneSignalChannel.php +++ b/src/OneSignalChannel.php @@ -28,7 +28,7 @@ public function __construct(OneSignalClient $oneSignal) */ public function send($notifiable, Notification $notification) { - if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) { + if (! $userIds = $notifiable->routeNotificationFor('OneSignal', $notification)) { return; } diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 31c5a55..79ad6cf 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,32 +3,20 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; +use NotificationChannels\OneSignal\Traits\Deprecated; +use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; +use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers; +use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; +use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; class OneSignalMessage { - /** @var string */ - protected $body; - - /** @var string */ - protected $subject; - - /** @var string */ - protected $url; - - /** @var string */ - protected $icon; - - /** @var array */ - protected $data = []; + use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated; /** @var array */ - protected $buttons = []; - - /** @var array */ - protected $webButtons = []; - - /** @var array */ - protected $extraParameters = []; + protected $payload = []; /** * @param string $body @@ -45,176 +33,96 @@ public static function create($body = '') */ public function __construct($body = '') { - $this->body = $body; + $this->setBody($body); } /** * Set the message body. * - * @param string $value - * - * @return $this - */ - public function body($value) - { - $this->body = $value; - - return $this; - } - - /** - * Set the message icon. - * - * @param string $value + * @param mixed $value * * @return $this */ - public function icon($value) + public function setBody($value) { - $this->icon = $value; - - return $this; + return $this->setParameter('contents', $this->parseValueToArray($value)); } /** * Set the message subject. * - * @param string $value + * @param mixed $value * * @return $this */ - public function subject($value) + public function setSubject($value) { - $this->subject = $value; - - return $this; + return $this->setParameter('headings', $this->parseValueToArray($value)); } /** - * Set the message url. + * Set the message template_id. * * @param string $value * * @return $this */ - public function url($value) - { - $this->url = $value; - - return $this; - } - - /** - * Set the iOS badge increment count. - * - * @param int $count - * - * @return $this - */ - public function incrementIosBadgeCount($count = 1) + public function setTemplate($value) { - return $this->setParameter('ios_badgeType', 'Increase') - ->setParameter('ios_badgeCount', $count); - } + Arr::forget($this->payload, 'contents'); - /** - * Set the iOS badge decrement count. - * - * @param int $count - * - * @return $this - */ - public function decrementIosBadgeCount($count = 1) - { - return $this->setParameter('ios_badgeType', 'Increase') - ->setParameter('ios_badgeCount', -1 * $count); + return $this->setParameter('template_id', $value); } /** - * Set the iOS badge count. + * @param mixed $value * - * @param int $count - * - * @return $this + * @return array */ - public function setIosBadgeCount($count) + protected function parseValueToArray($value) { - return $this->setParameter('ios_badgeType', 'SetTo') - ->setParameter('ios_badgeCount', $count); + return (is_array($value)) ? $value : ['en' => $value]; } /** * Set additional data. * * @param string $key - * @param string $value + * @param mixed $value * * @return $this */ - public function setData($key, $value) + public function setData(string $key, $value) { - $this->data[$key] = $value; - - return $this; + return $this->setParameter("data.{$key}", $value); } /** - * Set additional parameters. + * Set parameters. * * @param string $key - * @param string $value - * - * @return $this - */ - public function setParameter($key, $value) - { - $this->extraParameters[$key] = $value; - - return $this; - } - - /** - * Add a web button to the message. - * - * @param OneSignalWebButton $button + * @param mixed $value * * @return $this */ - public function webButton(OneSignalWebButton $button) + public function setParameter(string $key, $value) { - $this->webButtons[] = $button->toArray(); + Arr::set($this->payload, $key, $value); return $this; } /** - * Add a native button to the message. + * Get parameters. * - * @param OneSignalButton $button - * - * @return $this - */ - public function button(OneSignalButton $button) - { - $this->buttons[] = $button->toArray(); - - return $this; - } - - /** - * Set an image to all possible attachment variables. - * @param string $imageUrl + * @param string $key + * @param mixed $default * - * @return $this + * @return mixed */ - public function setImageAttachments($imageUrl) + public function getParameter(string $key, $default = null) { - $this->extraParameters['ios_attachments']['id1'] = $imageUrl; - $this->extraParameters['big_picture'] = $imageUrl; - $this->extraParameters['adm_big_picture'] = $imageUrl; - $this->extraParameters['chrome_big_picture'] = $imageUrl; - - return $this; + return Arr::get($this->payload, $key, $default); } /** @@ -222,35 +130,6 @@ public function setImageAttachments($imageUrl) */ public function toArray() { - $message = [ - 'contents' => ['en' => $this->body], - 'headings' => $this->subjectToArray(), - 'url' => $this->url, - 'buttons' => $this->buttons, - 'web_buttons' => $this->webButtons, - 'chrome_web_icon' => $this->icon, - 'chrome_icon' => $this->icon, - 'adm_small_icon' => $this->icon, - 'small_icon' => $this->icon, - ]; - - foreach ($this->extraParameters as $key => $value) { - Arr::set($message, $key, $value); - } - - foreach ($this->data as $data => $value) { - Arr::set($message, 'data.'.$data, $value); - } - - return $message; - } - - protected function subjectToArray() - { - if ($this->subject === null) { - return []; - } - - return ['en' => $this->subject]; + return $this->payload; } } diff --git a/src/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index d2b7855..6367b8e 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -15,14 +15,24 @@ class OneSignalPayloadFactory * * @return array */ - public static function make($notifiable, Notification $notification, $targeting) : array + public static function make($notifiable, Notification $notification, $targeting): array { $payload = $notification->toOneSignal($notifiable)->toArray(); if (static::isTargetingEmail($targeting)) { $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); } elseif (static::isTargetingTags($targeting)) { - $payload['tags'] = collect([$targeting['tags']]); + $array = $targeting['tags']; + $res = count($array) == count($array, COUNT_RECURSIVE); + if ($res) { + $payload['tags'] = collect([$targeting['tags']]); + } else { + $payload['tags'] = collect($targeting['tags']); + } + } elseif (static::isTargetingIncludedSegments($targeting)) { + $payload['included_segments'] = collect($targeting['included_segments']); + } elseif (static::isTargetingExcludedSegments($targeting)) { + $payload['excluded_segments'] = collect($targeting['excluded_segments']); } else { $payload['include_player_ids'] = collect($targeting); } @@ -30,6 +40,26 @@ public static function make($notifiable, Notification $notification, $targeting) return $payload; } + /** + * @param mixed $targeting + * + * @return bool + */ + protected static function isTargetingIncludedSegments($targeting) + { + return is_array($targeting) && array_key_exists('included_segments', $targeting); + } + + /** + * @param mixed $targeting + * + * @return bool + */ + protected static function isTargetingExcludedSegments($targeting) + { + return is_array($targeting) && array_key_exists('excluded_segments', $targeting); + } + /** * @param mixed $targeting * diff --git a/src/OneSignalWebButton.php b/src/OneSignalWebButton.php index 49b53e5..8c4bb44 100644 --- a/src/OneSignalWebButton.php +++ b/src/OneSignalWebButton.php @@ -82,10 +82,10 @@ public function url($value) public function toArray() { return [ - 'id' => $this->id, + 'id' => $this->id, 'text' => $this->text, 'icon' => $this->icon, - 'url' => $this->url, + 'url' => $this->url, ]; } } diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php new file mode 100644 index 0000000..d685f03 --- /dev/null +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -0,0 +1,125 @@ +setParameter('ios_badgeType', 'Increase') + ->setParameter('ios_badgeCount', $count); + } + + /** + * Set the iOS badge decrement count. + * + * @param int $count + * + * @return $this + */ + public function decrementIosBadgeCount(int $count = 1) + { + return $this->setParameter('ios_badgeType', 'Increase') + ->setParameter('ios_badgeCount', -1 * $count); + } + + /** + * Set the iOS badge count. + * + * @param int $count + * + * @return $this + */ + public function setIosBadgeCount(int $count) + { + return $this->setParameter('ios_badgeType', 'SetTo') + ->setParameter('ios_badgeCount', $count); + } + + /** + * Set the iOS Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setIosSound(string $soundUrl) + { + return $this->setParameter('ios_sound', $soundUrl); + } + + /** + * Set the Android Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setAndroidSound(string $soundUrl) + { + return $this->setParameter('android_sound', $soundUrl); + } + + /** + * Set the Windows Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setWindowsSound(string $soundUrl) + { + return $this->setParameter('wp_sound', $soundUrl) + ->setParameter('wp_wns_sound', $soundUrl); + } + + /** + * Set the Sound for all Systems. + * + * @param string $soundUrl + * + * @return $this + */ + public function setSound(string $soundUrl) + { + return $this->setAndroidSound($soundUrl) + ->setIosSound($soundUrl) + ->setWindowsSound($soundUrl); + } + + /** + * Set the message icon. + * + * @param string $iconPath + * + * @deprecated use setIcon instead + * + * @return $this + */ + public function icon(string $iconPath) + { + return $this->setIcon($iconPath); + } + + /** + * Set the message icon. + * + * @param string $iconPath + * + * @return $this + */ + public function setIcon(string $iconPath) + { + return $this->setParameter('chrome_web_icon', $iconPath) + ->setParameter('chrome_icon', $iconPath) + ->setParameter('adm_small_icon', $iconPath) + ->setParameter('small_icon', $iconPath); + } +} diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php new file mode 100644 index 0000000..3bfda1a --- /dev/null +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -0,0 +1,83 @@ + $imageUrl]; + + return $this->setParameter('ios_attachments', $imageUrl); + } + + /** + * Set the Big Picture Image only for Android. + * + * @param string $imageUrl + * + * @return $this + */ + public function setAndroidBigPicture(string $imageUrl) + { + return $this->setParameter('big_picture', $imageUrl); + } + + /** + * Set the Big Picture Image only for FireOS (Amazon). + * + * @param string $imageUrl + * + * @return $this + */ + public function setAmazonBigPicture(string $imageUrl) + { + return $this->setParameter('adm_big_picture', $imageUrl); + } + + /** + * Set the Big Picture Image only for Chrome. + * + * @param string $imageUrl + * + * @return $this + */ + public function setChromeBigPicture(string $imageUrl) + { + return $this->setParameter('chrome_big_picture', $imageUrl); + } + + /** + * Set the additional URL for all Platforms. + * + * @param string $url + * + * @return $this + */ + public function setUrl(string $url) + { + return $this->setParameter('url', $url); + } + + /** + * Set an image to all possible attachment variables. + * + * @param string $imageUrl + * + * @return $this + */ + public function setImageAttachments(string $imageUrl) + { + return $this->setIosAttachment($imageUrl) + ->setAndroidBigPicture($imageUrl) + ->setAmazonBigPicture($imageUrl) + ->setChromeBigPicture($imageUrl); + } +} diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php new file mode 100644 index 0000000..7b3dd06 --- /dev/null +++ b/src/Traits/Categories/ButtonHelpers.php @@ -0,0 +1,65 @@ +setParameter('web_buttons', array_merge($this->getParameter('web_buttons', []), [$button->toArray()])); + } + + /** + * Adds more than one web button to the message. + * + * @param array[OnSignalWebButton] $buttons + * + * @return $this + */ + public function setWebButtons(array $buttons) + { + collect($buttons)->map(function ($button) { + $this->setWebButton($button); + }); + + return $this; + } + + /** + * Add a native button to the message. + * + * @param OneSignalButton $button + * + * @return $this + */ + public function setButton(OneSignalButton $button) + { + return $this->setParameter('buttons', array_merge($this->getParameter('buttons', []), [$button->toArray()])); + } + + /** + * Adds more than one native button to the message. + * + * @param array $buttons + * + * @return $this + */ + public function setButtons(array $buttons) + { + collect($buttons)->map(function ($button) { + $this->setButton($button); + }); + + return $this; + } +} diff --git a/src/Traits/Categories/DeliveryHelpers.php b/src/Traits/Categories/DeliveryHelpers.php new file mode 100644 index 0000000..a98dff9 --- /dev/null +++ b/src/Traits/Categories/DeliveryHelpers.php @@ -0,0 +1,66 @@ +setParameter('send_after', $date); + } + + /** + * Set the deplayed option. + * + * @param string $delayedOption + * + * @return $this + */ + public function setDelayedOption(string $delayedOption) + { + return $this->setParameter('delayed_option', $delayedOption); + } + + /** + * Set the delivery at time of the day. Use with delayed option = timezone. + * + * @param string $timeOfDay + * + * @return $this + */ + public function setDeliveryTimeOfDay(string $timeOfDay) + { + return $this->setParameter('delivery_time_of_day', $timeOfDay); + } + + /** + * Set the Time to Live in Seconds. + * + * @param int $ttl + * + * @return $this + */ + public function setTtl(int $ttl) + { + return $this->setParameter('ttl', $ttl); + } + + /** + * Set the Priority. + * + * @param int $priority + * + * @return $this + */ + public function setPriority(int $priority) + { + return $this->setParameter('priority', $priority); + } +} diff --git a/src/Traits/Categories/GroupingHelpers.php b/src/Traits/Categories/GroupingHelpers.php new file mode 100644 index 0000000..c6586fa --- /dev/null +++ b/src/Traits/Categories/GroupingHelpers.php @@ -0,0 +1,48 @@ +setParameter('android_group', $group) + ->setParameter('android_group_message', $groupMessage); + } + + /** + * Set the Amazon (FireOS) Grouping Parameters. + * + * @param string $group + * @param array $groupMessage + * + * @return $this + */ + public function setAmazonGroup(string $group, array $groupMessage) + { + return $this->setParameter('adm_group', $group) + ->setParameter('adm_group_message', $groupMessage); + } + + /** + * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)). + * + * @param string $group + * @param array $groupMessage + * + * @return $this + */ + public function setGroup(string $group, array $groupMessage) + { + return $this->setAndroidGroup($group, $groupMessage) + ->setAmazonGroup($group, $groupMessage); + } +} diff --git a/src/Traits/Categories/SilentHelpers.php b/src/Traits/Categories/SilentHelpers.php new file mode 100644 index 0000000..5ba33a0 --- /dev/null +++ b/src/Traits/Categories/SilentHelpers.php @@ -0,0 +1,20 @@ +payload, 'contents'); //removes any contents that are set by constructor. + + return $this->setParameter('content_available', 1); + } +} diff --git a/src/Traits/Deprecated.php b/src/Traits/Deprecated.php new file mode 100644 index 0000000..ee78b9d --- /dev/null +++ b/src/Traits/Deprecated.php @@ -0,0 +1,106 @@ +setBody($value); + } + + /** + * Set the message subject. + * + * @param mixed $value + * + * @deprecated Use setSubject instead + * + * @return $this + */ + public function subject($value) + { + return $this->setParameter('headings', $this->parseValueToArray($value)); + } + + /** + * Set the message url. + * + * @param string $value + * + * @deprecated use setUrl Instead + * + * @return $this + */ + public function url($value) + { + return $this->setUrl($value); + } + + /** + * Add a web button to the message. + * + * @param OneSignalWebButton $button + * + * @deprecated use setWebButton instead + * + * @return $this + */ + public function webButton(OneSignalWebButton $button) + { + return $this->setWebButton($button); + } + + /** + * Adds more than one web button to the message. + * + * @param array[OnSignalWebButton] $buttons + * + * @deprecated use setWebButtons instead + * + * @return $this + */ + public function webButtons(array $buttons) + { + return $this->setWebButtons($buttons); + } + + /** + * Add a native button to the message. + * + * @param OneSignalButton $button + * + * @deprecated use setButton instead + * @return $this + */ + public function button(OneSignalButton $button) + { + return $this->setButton($button); + } + + /** + * Adds more than one native button to the message. + * + * @param array $buttons + * + * @deprecated use setButtons instead + * + * @return $this + */ + public function buttons(array $buttons) + { + return $this->setButtons($buttons); + } +} diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index a7da90d..ada7e4c 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -40,15 +40,13 @@ public function it_can_send_a_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); @@ -66,15 +64,13 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); @@ -94,15 +90,13 @@ public function it_can_send_a_notification_with_array() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect(['player_id_1', 'player_id_2']), ]) ->andReturn($response); @@ -122,16 +116,14 @@ public function it_can_send_a_notification_with_email() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', - 'filters' => collect([['field' => 'email', 'value' => 'test@example.com']]), + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'filters' => collect([['field' => 'email', 'value' => 'test@example.com']]), ]) ->andReturn($response); @@ -140,6 +132,58 @@ public function it_can_send_a_notification_with_email() $this->assertInstanceOf(ResponseInterface::class, $channel_response); } + /** + * @test + */ + public function it_can_send_a_notification_with_included_segments() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'included_segments' => collect(['included segments']), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableIncludedSegments(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + + /** + * @test + */ + public function it_can_send_a_notification_with_excluded_segments() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'excluded_segments' => collect(['excluded segments']), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableExcludedSegments(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + /** @test */ public function it_can_send_a_notification_with_tags() { @@ -148,16 +192,14 @@ public function it_can_send_a_notification_with_tags() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', - 'tags' => collect([['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000']]), + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'tags' => collect([['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000']]), ]) ->andReturn($response); @@ -166,6 +208,33 @@ public function it_can_send_a_notification_with_tags() $this->assertInstanceOf(ResponseInterface::class, $channel_response); } + /** @test */ + public function it_can_send_a_notification_with_multiple_tags() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'tags' => collect([ + ['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000'], + ['key' => 'role', 'relation' => '=', 'value' => 'admin'], + ]), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableMultipleTags(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + /** @test */ public function it_sends_nothing_and_returns_null_when_player_id_empty() { @@ -175,4 +244,23 @@ public function it_sends_nothing_and_returns_null_when_player_id_empty() $channel_response = $this->channel->send(new EmptyNotifiable(), new TestNotification()); $this->assertNull($channel_response); } + + public function it_can_send_a_silent_notification() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'content_available' => 1, + 'data.action' => 'reload', + 'data.target' => 'inbox', + 'include_player_ids' => collect('player_id'), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new Notifiable(), new TestSilentNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 9bec5d4..0b0a145 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -37,7 +37,7 @@ public function it_provides_a_create_method() /** @test */ public function it_can_set_the_body() { - $this->message->body('myBody'); + $this->message->setBody('myBody'); $this->assertEquals('myBody', Arr::get($this->message->toArray(), 'contents.en')); } @@ -45,7 +45,7 @@ public function it_can_set_the_body() /** @test */ public function it_can_set_the_subject() { - $this->message->subject('mySubject'); + $this->message->setSubject('mySubject'); $this->assertEquals('mySubject', Arr::get($this->message->toArray(), 'headings.en')); } @@ -53,13 +53,13 @@ public function it_can_set_the_subject() /** @test */ public function it_does_not_append_empty_subject_value_when_subject_is_null() { - $this->assertEquals([], Arr::get($this->message->toArray(), 'headings')); + $this->assertEquals(null, Arr::get($this->message->toArray(), 'headings')); } /** @test */ public function it_can_set_the_url() { - $this->message->url('myURL'); + $this->message->setUrl('myURL'); $this->assertEquals('myURL', Arr::get($this->message->toArray(), 'url')); } @@ -114,7 +114,7 @@ public function it_can_set_additional_parameter() /** @test */ public function it_can_set_the_icon() { - $this->message->icon('myIcon'); + $this->message->setIcon('myIcon'); $this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_web_icon')); $this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_icon')); @@ -141,7 +141,7 @@ public function it_can_set_a_web_button() /** @test */ public function it_can_set_a_button() { - $this->message->button( + $this->message->setButton( OneSignalButton::create('buttonID') ->text('buttonText') ->icon('buttonIcon') @@ -152,6 +152,31 @@ public function it_can_set_a_button() $this->assertEquals('buttonIcon', Arr::get($this->message->toArray(), 'buttons.0.icon')); } + /** @test */ + public function it_can_set_a_web_buttons_with_chain() + { + $this->message->setWebButton( + OneSignalWebButton::create('buttonID_1') + ->text('buttonText_1') + ->url('buttonURL_1') + ->icon('buttonIcon_1') + )->setWebButton( + OneSignalWebButton::create('buttonID_2') + ->text('buttonText_2') + ->url('buttonURL_2') + ->icon('buttonIcon_2') + ); + + $this->assertEquals('buttonID_1', Arr::get($this->message->toArray(), 'web_buttons.0.id')); + $this->assertEquals('buttonText_1', Arr::get($this->message->toArray(), 'web_buttons.0.text')); + $this->assertEquals('buttonURL_1', Arr::get($this->message->toArray(), 'web_buttons.0.url')); + $this->assertEquals('buttonIcon_1', Arr::get($this->message->toArray(), 'web_buttons.0.icon')); + $this->assertEquals('buttonID_2', Arr::get($this->message->toArray(), 'web_buttons.1.id')); + $this->assertEquals('buttonText_2', Arr::get($this->message->toArray(), 'web_buttons.1.text')); + $this->assertEquals('buttonURL_2', Arr::get($this->message->toArray(), 'web_buttons.1.url')); + $this->assertEquals('buttonIcon_2', Arr::get($this->message->toArray(), 'web_buttons.1.icon')); + } + /** @test */ public function it_can_set_a_image() { diff --git a/tests/NotifiableExcludedSegments.php b/tests/NotifiableExcludedSegments.php new file mode 100644 index 0000000..0ef435c --- /dev/null +++ b/tests/NotifiableExcludedSegments.php @@ -0,0 +1,16 @@ + ['excluded segments']]; + } +} diff --git a/tests/NotifiableIncludedSegments.php b/tests/NotifiableIncludedSegments.php new file mode 100644 index 0000000..4235ffb --- /dev/null +++ b/tests/NotifiableIncludedSegments.php @@ -0,0 +1,16 @@ + ['included segments']]; + } +} diff --git a/tests/NotifiableMultipleTags.php b/tests/NotifiableMultipleTags.php new file mode 100644 index 0000000..5779314 --- /dev/null +++ b/tests/NotifiableMultipleTags.php @@ -0,0 +1,20 @@ + [ + ['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000'], + ['key' => 'role', 'relation' => '=', 'value' => 'admin'], + ], + ]; + } +} diff --git a/tests/TestSilentNotification.php b/tests/TestSilentNotification.php new file mode 100644 index 0000000..185ddc1 --- /dev/null +++ b/tests/TestSilentNotification.php @@ -0,0 +1,17 @@ +setSilent() + ->setData('action', 'reload') + ->setData('target', 'inbox'); + } +}