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/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index d2b7855..5d4b47b 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -15,7 +15,7 @@ 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(); @@ -23,6 +23,10 @@ public static function make($notifiable, Notification $notification, $targeting) $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); } elseif (static::isTargetingTags($targeting)) { $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 +34,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/tests/ChannelTest.php b/tests/ChannelTest.php index 6f70008..00e49e4 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -40,13 +40,13 @@ public function it_can_send_a_notification() $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', + '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); @@ -64,13 +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', - '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); @@ -90,13 +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', - '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); @@ -116,14 +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', + '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); @@ -132,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() { @@ -140,14 +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', + '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); @@ -173,9 +225,9 @@ public function it_can_send_a_silent_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'content_available' => 1, - 'data.action' => 'reload', - 'data.target' => 'inbox', + 'content_available' => 1, + 'data.action' => 'reload', + 'data.target' => 'inbox', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); 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']]; + } +}