Skip to content

Commit

Permalink
Merge pull request #40 from LKDevelopment/implement-email-targeting
Browse files Browse the repository at this point in the history
Send Notification based on the E-Mail instead of the OneSignal User ID
  • Loading branch information
Lloople committed Jan 5, 2018
2 parents 53cbc7c + 1a6a178 commit 9e87947
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -104,6 +104,15 @@ public function routeNotificationForOneSignal()
}
```

If you want to send the notification based on the OneSignal "syncHashedEmail" feature just return an array with the index "email". **It isn't possible to use multiple E-Mails on one filter because of a limitation of the OneSignal API.**

```php
public function routeNotificationForOneSignal()
{
return ['email' => 'example@example.com'];
}
```

### All available methods

- `subject('')`: Accepts a string value for the title.
Expand Down
11 changes: 8 additions & 3 deletions src/OneSignalChannel.php
Expand Up @@ -3,9 +3,9 @@
namespace NotificationChannels\OneSignal;

use Berkayk\OneSignal\OneSignalClient;
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;
use Illuminate\Notifications\Notification;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Notifications\Notification;
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;

class OneSignalChannel
{
Expand All @@ -32,7 +32,12 @@ public function send($notifiable, Notification $notification)
}

$payload = $notification->toOneSignal($notifiable)->toArray();
$payload['include_player_ids'] = collect($userIds);

if (is_array($userIds) && array_key_exists('email', $userIds)) {
$payload['filters'] = collect([['field' => 'email', 'value' => $userIds['email']]]);
} else {
$payload['include_player_ids'] = collect($userIds);
}

/** @var ResponseInterface $response */
$response = $this->oneSignal->sendNotificationCustom($payload);
Expand Down
26 changes: 26 additions & 0 deletions tests/ChannelTest.php
Expand Up @@ -80,4 +80,30 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification()

$this->channel->send(new Notifiable(), new TestNotification());
}

/**
* @test
*/
public function it_can_send_a_notification_with_email()
{
$response = new Response(200);

$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',
'filters' => collect([['field' => 'email', 'value' => 'test@example.com']]),
])
->andReturn($response);

$this->channel->send(new NotifiableEmail(), new TestNotification());
}
}
16 changes: 16 additions & 0 deletions tests/NotifiableEmail.php
@@ -0,0 +1,16 @@
<?php

namespace NotificationChannels\OneSignal\Test;

class NotifiableEmail
{
use \Illuminate\Notifications\Notifiable;

/**
* @return array
*/
public function routeNotificationForOneSignal()
{
return ['email' => 'test@example.com'];
}
}

0 comments on commit 9e87947

Please sign in to comment.