Skip to content

Commit da19342

Browse files
committed
Implement Support for multiple E-Mail Adresses
1 parent 50921f2 commit da19342

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-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" and the E-Mail or an Array with E-Mails.
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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function send($notifiable, Notification $notification)
3434

3535
$payload = $notification->toOneSignal($notifiable)->toArray();
3636
if (is_array($userIds) && array_key_exists('email', $userIds)) {
37-
$payload['filters'] = collect([["field" => "email", "relation" => "=", "value" => $userIds['email']]]);
37+
$payload['filters'] = collect($this->parseEmailsToFilters($userIds['email']));
3838
} else {
3939
$payload['include_player_ids'] = collect($userIds);
4040
}
@@ -45,4 +45,27 @@ public function send($notifiable, Notification $notification)
4545
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
4646
}
4747
}
48+
49+
/**
50+
* Parse the given E-Mails to the array-format that OneSignal needs.
51+
*
52+
* @param mixed $userEmails
53+
*
54+
* @return array
55+
*/
56+
public function parseEmailsToFilters($userEmails)
57+
{
58+
if (is_array($userEmails)) {
59+
return collect($userEmails)->map(function ($email, $key) use ($userEmails) {
60+
if ($key < (count($userEmails) - 1)) {
61+
return [["field" => "email", "relation" => "=", "value" => $email], ['operator' => 'OR']];
62+
} else {
63+
return [["field" => "email", "relation" => "=", "value" => $email]];
64+
}
65+
})->flatten(1)->toArray();
66+
}
67+
68+
return [["field" => "email", "relation" => "=", "value" => $userEmails]];
69+
70+
}
4871
}

tests/ChannelTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification()
8282
$this->channel->send(new Notifiable(), new TestNotification());
8383
}
8484

85+
/**
86+
* @test
87+
*/
8588
public function it_can_send_a_notification_with_email()
8689
{
8790
$response = new Response(200);
@@ -98,10 +101,36 @@ public function it_can_send_a_notification_with_email()
98101
'chrome_icon' => 'Icon',
99102
'adm_small_icon' => 'Icon',
100103
'small_icon' => 'Icon',
101-
'filters' => collect(["field" => "email", "relation" => "=", "value" => '[email protected]']),
104+
'filters' => collect([["field" => "email", "relation" => "=", "value" => '[email protected]']]),
102105
])
103106
->andReturn($response);
104107

105108
$this->channel->send(new NotifiableEMail(), new TestNotification());
106109
}
110+
111+
/**
112+
* @test
113+
*/
114+
public function it_can_send_a_notification_with_multiple_email()
115+
{
116+
$response = new Response(200);
117+
118+
$this->oneSignal->shouldReceive('sendNotificationCustom')
119+
->once()
120+
->with([
121+
'contents' => ['en' => 'Body'],
122+
'headings' => ['en' => 'Subject'],
123+
'url' => 'URL',
124+
'buttons' => [],
125+
'web_buttons' => [],
126+
'chrome_web_icon' => 'Icon',
127+
'chrome_icon' => 'Icon',
128+
'adm_small_icon' => 'Icon',
129+
'small_icon' => 'Icon',
130+
'filters' => collect([["field" => "email", "relation" => "=", "value" => '[email protected]'], ['operator' => 'OR'], ["field" => "email", "relation" => "=", "value" => '[email protected]']]),
131+
])
132+
->andReturn($response);
133+
134+
$this->channel->send(new NotifiableMultipleEMail(), new TestNotification());
135+
}
107136
}

tests/NotifiableEMail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
class NotifiableEMail
66
{
7+
78
use \Illuminate\Notifications\Notifiable;
89

910
/**
10-
* @return int
11+
* @return array
1112
*/
1213
public function routeNotificationForOneSignal()
1314
{

tests/NotifiableMultipleEMail.php

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

0 commit comments

Comments
 (0)