Skip to content

Commit 065321b

Browse files
JeffreyHyerLKaemmerling
authored andcommitted
Target external user IDs (#86)
A small change that allows you to send notifications to users by specifying an external user ID. See the [OneSignal docs](https://documentation.onesignal.com/reference#section-send-to-specific-devices) under 'include_external_user_ids' for all the details.
1 parent 84d6e05 commit 065321b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/OneSignalPayloadFactory.php

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public static function make($notifiable, Notification $notification, $targeting)
3333
$payload['included_segments'] = collect($targeting['included_segments']);
3434
} elseif (static::isTargetingExcludedSegments($targeting)) {
3535
$payload['excluded_segments'] = collect($targeting['excluded_segments']);
36+
} elseif (static::isTargetingExternalUserIds($targeting)) {
37+
$payload['include_external_user_ids'] = collect($targeting['include_external_user_ids']);
3638
} else {
3739
$payload['include_player_ids'] = collect($targeting);
3840
}
@@ -50,6 +52,16 @@ protected static function isTargetingIncludedSegments($targeting)
5052
return is_array($targeting) && array_key_exists('included_segments', $targeting);
5153
}
5254

55+
/**
56+
* @param mixed $targeting
57+
*
58+
* @return bool
59+
*/
60+
protected static function isTargetingExternalUserIds($targeting)
61+
{
62+
return is_array($targeting) && array_key_exists('include_external_user_ids', $targeting);
63+
}
64+
5365
/**
5466
* @param mixed $targeting
5567
*

tests/ChannelTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ public function it_can_send_a_notification_with_multiple_tags()
235235
$this->assertInstanceOf(ResponseInterface::class, $channel_response);
236236
}
237237

238+
/**
239+
* @test
240+
*/
241+
public function it_can_send_a_notification_with_external_ids()
242+
{
243+
$response = new Response(200);
244+
245+
$this->oneSignal->shouldReceive('sendNotificationCustom')
246+
->once()
247+
->with([
248+
'contents' => ['en' => 'Body'],
249+
'headings' => ['en' => 'Subject'],
250+
'url' => 'URL',
251+
'chrome_web_icon' => 'Icon',
252+
'chrome_icon' => 'Icon',
253+
'adm_small_icon' => 'Icon',
254+
'small_icon' => 'Icon',
255+
'include_external_user_ids' => collect(['external_id']),
256+
])
257+
->andReturn($response);
258+
259+
$channel_response = $this->channel->send(new NotifiableIncludesExternalIds(), new TestNotification());
260+
261+
$this->assertInstanceOf(ResponseInterface::class, $channel_response);
262+
}
263+
238264
/** @test */
239265
public function it_sends_nothing_and_returns_null_when_player_id_empty()
240266
{
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace NotificationChannels\OneSignal\Test;
4+
5+
class NotifiableIncludesExternalIds
6+
{
7+
use \Illuminate\Notifications\Notifiable;
8+
9+
/**
10+
* @return array
11+
*/
12+
public function routeNotificationForOneSignal()
13+
{
14+
return ['include_external_user_ids' => ['external_id']];
15+
}
16+
}

0 commit comments

Comments
 (0)