Skip to content

Commit 9e87947

Browse files
authored
Merge pull request #40 from LKDevelopment/implement-email-targeting
Send Notification based on the E-Mail instead of the OneSignal User ID
2 parents 53cbc7c + 1a6a178 commit 9e87947

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ public function routeNotificationForOneSignal()
104104
}
105105
```
106106

107+
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.**
108+
109+
```php
110+
public function routeNotificationForOneSignal()
111+
{
112+
return ['email' => '[email protected]'];
113+
}
114+
```
115+
107116
### All available methods
108117

109118
- `subject('')`: Accepts a string value for the title.

src/OneSignalChannel.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace NotificationChannels\OneSignal;
44

55
use Berkayk\OneSignal\OneSignalClient;
6-
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;
7-
use Illuminate\Notifications\Notification;
86
use Psr\Http\Message\ResponseInterface;
7+
use Illuminate\Notifications\Notification;
8+
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;
99

1010
class OneSignalChannel
1111
{
@@ -32,7 +32,12 @@ public function send($notifiable, Notification $notification)
3232
}
3333

3434
$payload = $notification->toOneSignal($notifiable)->toArray();
35-
$payload['include_player_ids'] = collect($userIds);
35+
36+
if (is_array($userIds) && array_key_exists('email', $userIds)) {
37+
$payload['filters'] = collect([['field' => 'email', 'value' => $userIds['email']]]);
38+
} else {
39+
$payload['include_player_ids'] = collect($userIds);
40+
}
3641

3742
/** @var ResponseInterface $response */
3843
$response = $this->oneSignal->sendNotificationCustom($payload);

tests/ChannelTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,30 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification()
8080

8181
$this->channel->send(new Notifiable(), new TestNotification());
8282
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function it_can_send_a_notification_with_email()
88+
{
89+
$response = new Response(200);
90+
91+
$this->oneSignal->shouldReceive('sendNotificationCustom')
92+
->once()
93+
->with([
94+
'contents' => ['en' => 'Body'],
95+
'headings' => ['en' => 'Subject'],
96+
'url' => 'URL',
97+
'buttons' => [],
98+
'web_buttons' => [],
99+
'chrome_web_icon' => 'Icon',
100+
'chrome_icon' => 'Icon',
101+
'adm_small_icon' => 'Icon',
102+
'small_icon' => 'Icon',
103+
'filters' => collect([['field' => 'email', 'value' => '[email protected]']]),
104+
])
105+
->andReturn($response);
106+
107+
$this->channel->send(new NotifiableEmail(), new TestNotification());
108+
}
83109
}

tests/NotifiableEmail.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace NotificationChannels\OneSignal\Test;
4+
5+
class NotifiableEmail
6+
{
7+
use \Illuminate\Notifications\Notifiable;
8+
9+
/**
10+
* @return array
11+
*/
12+
public function routeNotificationForOneSignal()
13+
{
14+
return ['email' => '[email protected]'];
15+
}
16+
}

0 commit comments

Comments
 (0)