Skip to content

implemented sending of notifications based on segments (included/excluded) #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OneSignalButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function text($value)
public function toArray()
{
return [
'id' => $this->id,
'id' => $this->id,
'text' => $this->text,
'icon' => $this->icon,
];
Expand Down
26 changes: 25 additions & 1 deletion src/OneSignalPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,45 @@ 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']]);
} 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);
}

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
*
Expand Down
4 changes: 2 additions & 2 deletions src/OneSignalWebButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
128 changes: 90 additions & 38 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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' => '[email protected]']]),
'chrome_icon' => 'Icon',
'adm_small_icon' => 'Icon',
'small_icon' => 'Icon',
'filters' => collect([['field' => 'email', 'value' => '[email protected]']]),
])
->andReturn($response);

Expand All @@ -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()
{
Expand All @@ -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);

Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions tests/NotifiableExcludedSegments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NotificationChannels\OneSignal\Test;

class NotifiableExcludedSegments
{
use \Illuminate\Notifications\Notifiable;

/**
* @return array
*/
public function routeNotificationForOneSignal()
{
return ['excluded_segments' => ['excluded segments']];
}
}
16 changes: 16 additions & 0 deletions tests/NotifiableIncludedSegments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NotificationChannels\OneSignal\Test;

class NotifiableIncludedSegments
{
use \Illuminate\Notifications\Notifiable;

/**
* @return array
*/
public function routeNotificationForOneSignal()
{
return ['included_segments' => ['included segments']];
}
}