diff --git a/README.md b/README.md index 1902a93..6928b1c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/OneSignalChannel.php b/src/OneSignalChannel.php index 61e2f37..3d1a279 100644 --- a/src/OneSignalChannel.php +++ b/src/OneSignalChannel.php @@ -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 { @@ -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); diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 614b7dd..c572b2e 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -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()); + } } diff --git a/tests/NotifiableEmail.php b/tests/NotifiableEmail.php new file mode 100644 index 0000000..a0e309e --- /dev/null +++ b/tests/NotifiableEmail.php @@ -0,0 +1,16 @@ + 'test@example.com']; + } +}