Skip to content
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

Send Notification based on the E-Mail instead of the OneSignal User ID #40

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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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' => '[email protected]'];
}
```

### All available methods

- `subject('')`: Accepts a string value for the title.
Expand Down
11 changes: 8 additions & 3 deletions src/OneSignalChannel.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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' => '[email protected]']]),
])
->andReturn($response);

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

namespace NotificationChannels\OneSignal\Test;

class NotifiableEmail
{
use \Illuminate\Notifications\Notifiable;

/**
* @return array
*/
public function routeNotificationForOneSignal()
{
return ['email' => '[email protected]'];
}
}