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 6 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
12 changes: 8 additions & 4 deletions src/OneSignalChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class OneSignalChannel
{

/** @var OneSignalClient */
protected $oneSignal;

Expand All @@ -20,23 +21,26 @@ public function __construct(OneSignalClient $oneSignal)
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @throws \NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
{
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) {
if (!$userIds = $notifiable->routeNotificationFor('OneSignal')) {
return;
}

$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);

if ($response->getStatusCode() !== 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class ChannelTest extends TestCase
{

/** @var Mockery\Mock */
protected $oneSignal;

Expand Down Expand Up @@ -80,4 +81,31 @@ 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());
}

}
17 changes: 17 additions & 0 deletions tests/NotifiableEMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace NotificationChannels\OneSignal\Test;

class NotifiableEMail
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this class be named NotifiableEmail instead of NotifiableEMai? 🤔

{

use \Illuminate\Notifications\Notifiable;

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