From 715521e59a8af98c155d4ec14ce03d338c582238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 07:08:51 +0100 Subject: [PATCH 01/21] Refactor to Traits and add some little more Parameters --- src/OneSignalMessage.php | 59 +---------- src/Traits/Categories/AppearanceHelpers.php | 97 ++++++++++++++++++ src/Traits/Categories/AttachmentHelpers.php | 103 ++++++++++++++++++++ src/Traits/Categories/DeliveryHelpers.php | 69 +++++++++++++ src/Traits/OneSignalHelpers.php | 14 +++ 5 files changed, 288 insertions(+), 54 deletions(-) create mode 100644 src/Traits/Categories/AppearanceHelpers.php create mode 100644 src/Traits/Categories/AttachmentHelpers.php create mode 100644 src/Traits/Categories/DeliveryHelpers.php create mode 100644 src/Traits/OneSignalHelpers.php diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 31c5a55..63894d4 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,9 +3,11 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; +use NotificationChannels\OneSignal\Traits\OneSignalHelpers; class OneSignalMessage { + use OneSignalHelpers; /** @var string */ protected $body; @@ -104,44 +106,6 @@ public function url($value) return $this; } - /** - * Set the iOS badge increment count. - * - * @param int $count - * - * @return $this - */ - public function incrementIosBadgeCount($count = 1) - { - return $this->setParameter('ios_badgeType', 'Increase') - ->setParameter('ios_badgeCount', $count); - } - - /** - * Set the iOS badge decrement count. - * - * @param int $count - * - * @return $this - */ - public function decrementIosBadgeCount($count = 1) - { - return $this->setParameter('ios_badgeType', 'Increase') - ->setParameter('ios_badgeCount', -1 * $count); - } - - /** - * Set the iOS badge count. - * - * @param int $count - * - * @return $this - */ - public function setIosBadgeCount($count) - { - return $this->setParameter('ios_badgeType', 'SetTo') - ->setParameter('ios_badgeCount', $count); - } /** * Set additional data. @@ -201,22 +165,6 @@ public function button(OneSignalButton $button) return $this; } - /** - * Set an image to all possible attachment variables. - * @param string $imageUrl - * - * @return $this - */ - public function setImageAttachments($imageUrl) - { - $this->extraParameters['ios_attachments']['id1'] = $imageUrl; - $this->extraParameters['big_picture'] = $imageUrl; - $this->extraParameters['adm_big_picture'] = $imageUrl; - $this->extraParameters['chrome_big_picture'] = $imageUrl; - - return $this; - } - /** * @return array */ @@ -245,6 +193,9 @@ public function toArray() return $message; } + /** + * @return array + */ protected function subjectToArray() { if ($this->subject === null) { diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php new file mode 100644 index 0000000..e3dc13b --- /dev/null +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -0,0 +1,97 @@ +setParameter('ios_badgeType', 'Increase') + ->setParameter('ios_badgeCount', $count); + } + + /** + * Set the iOS badge decrement count. + * + * @param int $count + * + * @return $this + */ + public function decrementIosBadgeCount(int $count = 1) + { + return $this->setParameter('ios_badgeType', 'Increase') + ->setParameter('ios_badgeCount', -1 * $count); + } + + /** + * Set the iOS badge count. + * + * @param int $count + * + * @return $this + */ + public function setIosBadgeCount(int $count) + { + return $this->setParameter('ios_badgeType', 'SetTo') + ->setParameter('ios_badgeCount', $count); + } + + /** + * Set the iOS Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setIosSound(string $soundUrl) + { + return $this->setParameter('ios_sound', $soundUrl); + } + + /** + * Set the Android Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setAndroidSound(string $soundUrl) + { + return $this->setParameter('android_sound', $soundUrl); + } + + /** + * Set the Windows Sound. + * + * @param string $soundUrl + * + * @return $this + */ + public function setWindowsSound(string $soundUrl) + { + return $this->setParameter('wp_sound', $soundUrl)->setParameter('wp_wns_sound', $soundUrl); + } + + /** + * Set the Sound for all Systems + * + * @param string $soundUrl + * + * @return $this + */ + public function setSound(string $soundUrl) + { + return $this->setAndroidSound($soundUrl) + ->setIosSound($soundUrl) + ->setWindowsSound($soundUrl); + } +} \ No newline at end of file diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php new file mode 100644 index 0000000..631067b --- /dev/null +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -0,0 +1,103 @@ +setParameter('ios_attachments', ['id1' => $imageUrl]); + } + + /** + * Set multiple Images only for iOS + * + * @param array $images + * + * @return $this + */ + public function setIosAttachments(array $images) + { + return $this->setParameter('ios_attachments', $images); + } + + /** + * Set the Big Picture Image only for Android + * + * @param string $imageUrl + * + * @return $this + */ + public function setAndroidBigPicture(string $imageUrl) + { + return $this->setParameter('big_picture', $imageUrl); + } + + /** + * Set the Big Picture Image only for FireOS (Amazon) + * + * @param string $imageUrl + * + * @return $this + */ + public function setAmazonBigPicture(string $imageUrl) + { + return $this->setParameter('adm_big_picture', $imageUrl); + } + + /** + * Set the Big Picture Image only for Chrome + * + * @param string $imageUrl + * + * @return $this + */ + public function setChromeBigPicture(string $imageUrl) + { + return $this->setParameter('chrome_big_picture', $imageUrl); + } + + /** + * Set the additional Data for all Platforms + * @param array $data + * + * @return $this + */ + public function setData(array $data){ + return $this->setParameter('data', $data); + } + + /** + * Set the additional URL for all Platforms + * @param string $url + * + * @return $this + */ + public function setUrl(string $url){ + return $this->setParameter('url', $url); + } + + /** + * Set an image to all possible attachment variables. + * + * @param string $imageUrl + * + * @return $this + */ + public function setImageAttachments(string $imageUrl) + { + return $this->setIosAttachment($imageUrl) + ->setAndroidBigPicture($imageUrl) + ->setAmazonBigPicture($imageUrl) + ->setChromeBigPicture($imageUrl); + } +} \ No newline at end of file diff --git a/src/Traits/Categories/DeliveryHelpers.php b/src/Traits/Categories/DeliveryHelpers.php new file mode 100644 index 0000000..fe82e7d --- /dev/null +++ b/src/Traits/Categories/DeliveryHelpers.php @@ -0,0 +1,69 @@ +setParameter('send_after',$date); + } + + /** + * Set the deplayed option + * + * @param string $delayedOption + * + * @return $this + */ + public function setDelayedOption(string $delayedOption) + { + return $this->setParameter('delayed_option', $delayedOption); + } + + /** + * Set the delivery at time of the day. Use with delayed option = timezone + * + * @param string $timeOfDay + * + * @return $this + */ + public function setDeliveryTimeOfDay(string $timeOfDay) + { + return $this->setParameter('delivery_time_of_day', $timeOfDay); + } + + /** + * Set the Time to Live in Seconds + * + * @param int $ttl + * + * @return $this + */ + public function setTTL(int $ttl) + { + return $this->setParameter('ttl', $ttl); + } + + /** + * Set the Priority + * + * @param int $priority + * + * @return $this + */ + public function setPriority(string $priority) + { + return $this->setParameter('priority', $priority); + } + +} \ No newline at end of file diff --git a/src/Traits/OneSignalHelpers.php b/src/Traits/OneSignalHelpers.php new file mode 100644 index 0000000..76a4cb0 --- /dev/null +++ b/src/Traits/OneSignalHelpers.php @@ -0,0 +1,14 @@ + Date: Wed, 14 Mar 2018 07:16:48 +0100 Subject: [PATCH 02/21] Add Grouping Helpers --- src/Traits/Categories/GroupingHelpers.php | 47 +++++++++++++++++++++++ src/Traits/OneSignalHelpers.php | 3 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Traits/Categories/GroupingHelpers.php diff --git a/src/Traits/Categories/GroupingHelpers.php b/src/Traits/Categories/GroupingHelpers.php new file mode 100644 index 0000000..ab496d8 --- /dev/null +++ b/src/Traits/Categories/GroupingHelpers.php @@ -0,0 +1,47 @@ +setParameter('android_group',$group) + ->setParameter('android_group_message',$groupMessage); + } + + /** + * Set the Amazon (FireOS) Grouping Parameters + * @param string $group + * @param array $groupMessage + * + * @return $this + */ + public function setAmazonGroup(string $group, array $groupMessage) + { + return $this->setParameter('adm_group',$group) + ->setParameter('adm_group_message',$groupMessage); + } + + /** + * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)) + * @param string $group + * @param array $groupMessage + * + * @return $this + */ + public function setGroup(string $group, array $groupMessage) + { + return $this->setAndroidGroup($group,$groupMessage) + ->setAmazonGroup($group,$groupMessage); + } +} \ No newline at end of file diff --git a/src/Traits/OneSignalHelpers.php b/src/Traits/OneSignalHelpers.php index 76a4cb0..a8845d8 100644 --- a/src/Traits/OneSignalHelpers.php +++ b/src/Traits/OneSignalHelpers.php @@ -6,9 +6,10 @@ use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; +use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; trait OneSignalHelpers { - use AppearanceHelpers, AttachmentHelpers, DeliveryHelpers; + use AppearanceHelpers, AttachmentHelpers, DeliveryHelpers, GroupingHelpers; } \ No newline at end of file From 7d42ea16d00247bcbba05a8fda8553cebcfde156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 07:29:49 +0100 Subject: [PATCH 03/21] Add Button Helpers --- src/OneSignalMessage.php | 32 +------------- src/Traits/Categories/ButtonHelpers.php | 58 +++++++++++++++++++++++++ src/Traits/OneSignalHelpers.php | 3 +- tests/ChannelTest.php | 10 ----- 4 files changed, 62 insertions(+), 41 deletions(-) create mode 100644 src/Traits/Categories/ButtonHelpers.php diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 63894d4..0379b57 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -126,44 +126,18 @@ public function setData($key, $value) * Set additional parameters. * * @param string $key - * @param string $value + * @param mixed $value * * @return $this */ - public function setParameter($key, $value) + public function setParameter(string $key, $value) { $this->extraParameters[$key] = $value; return $this; } - /** - * Add a web button to the message. - * - * @param OneSignalWebButton $button - * - * @return $this - */ - public function webButton(OneSignalWebButton $button) - { - $this->webButtons[] = $button->toArray(); - - return $this; - } - /** - * Add a native button to the message. - * - * @param OneSignalButton $button - * - * @return $this - */ - public function button(OneSignalButton $button) - { - $this->buttons[] = $button->toArray(); - - return $this; - } /** * @return array @@ -174,8 +148,6 @@ public function toArray() 'contents' => ['en' => $this->body], 'headings' => $this->subjectToArray(), 'url' => $this->url, - 'buttons' => $this->buttons, - 'web_buttons' => $this->webButtons, 'chrome_web_icon' => $this->icon, 'chrome_icon' => $this->icon, 'adm_small_icon' => $this->icon, diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php new file mode 100644 index 0000000..4d61011 --- /dev/null +++ b/src/Traits/Categories/ButtonHelpers.php @@ -0,0 +1,58 @@ +setParameter('web_buttons',[$button->toArray()]); + + } + /** + * Adds more than one web button to the message. + * + * @param array[OnSignalWebButton] $buttons + * + * @return $this + */ + public function webButtons(array $buttons) + { + return $this->setParameter('web_buttons',collect($buttons)->map(function($button) { return $button->toArray();})); + + } + /** + * Add a native button to the message. + * + * @param OneSignalButton $button + * + * @return $this + */ + public function button(OneSignalButton $button) + { + return $this->setParameter('buttons',[$button->toArray()]); + } + + /** + * Adds more than one native button to the message. + * + * @param array $buttons + * + * @return $this + */ + public function buttons(array $buttons) + { + return $this->setParameter('buttons',collect($buttons)->map(function($button) { return $button->toArray();})); + } +} \ No newline at end of file diff --git a/src/Traits/OneSignalHelpers.php b/src/Traits/OneSignalHelpers.php index a8845d8..bbc1e27 100644 --- a/src/Traits/OneSignalHelpers.php +++ b/src/Traits/OneSignalHelpers.php @@ -5,11 +5,12 @@ use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; +use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; trait OneSignalHelpers { - use AppearanceHelpers, AttachmentHelpers, DeliveryHelpers, GroupingHelpers; + use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers; } \ No newline at end of file diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index a7da90d..68697e6 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -43,8 +43,6 @@ public function it_can_send_a_notification() 'contents' => ['en' => 'Body'], 'headings' => ['en' => 'Subject'], 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], 'chrome_web_icon' => 'Icon', 'chrome_icon' => 'Icon', 'adm_small_icon' => 'Icon', @@ -69,8 +67,6 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification() 'contents' => ['en' => 'Body'], 'headings' => ['en' => 'Subject'], 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], 'chrome_web_icon' => 'Icon', 'chrome_icon' => 'Icon', 'adm_small_icon' => 'Icon', @@ -97,8 +93,6 @@ public function it_can_send_a_notification_with_array() 'contents' => ['en' => 'Body'], 'headings' => ['en' => 'Subject'], 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], 'chrome_web_icon' => 'Icon', 'chrome_icon' => 'Icon', 'adm_small_icon' => 'Icon', @@ -125,8 +119,6 @@ public function it_can_send_a_notification_with_email() 'contents' => ['en' => 'Body'], 'headings' => ['en' => 'Subject'], 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], 'chrome_web_icon' => 'Icon', 'chrome_icon' => 'Icon', 'adm_small_icon' => 'Icon', @@ -151,8 +143,6 @@ public function it_can_send_a_notification_with_tags() 'contents' => ['en' => 'Body'], 'headings' => ['en' => 'Subject'], 'url' => 'URL', - 'buttons' => [], - 'web_buttons' => [], 'chrome_web_icon' => 'Icon', 'chrome_icon' => 'Icon', 'adm_small_icon' => 'Icon', From e00b7cc63629aea33c6235548cdcc161b65a8f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 07:40:27 +0100 Subject: [PATCH 04/21] Removed all unused protected variables Keep Backward Compatibility --- src/OneSignalMessage.php | 82 ++++----------------- src/Traits/Categories/AppearanceHelpers.php | 14 ++++ tests/MessageTest.php | 2 +- 3 files changed, 31 insertions(+), 67 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 0379b57..47dd77f 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -8,29 +8,14 @@ class OneSignalMessage { use OneSignalHelpers; - /** @var string */ - protected $body; - /** @var string */ - protected $subject; - - /** @var string */ - protected $url; - - /** @var string */ - protected $icon; /** @var array */ protected $data = []; - /** @var array */ - protected $buttons = []; - - /** @var array */ - protected $webButtons = []; /** @var array */ - protected $extraParameters = []; + protected $payload = []; /** * @param string $body @@ -47,7 +32,7 @@ public static function create($body = '') */ public function __construct($body = '') { - $this->body = $body; + $this->body($body); } /** @@ -59,22 +44,11 @@ public function __construct($body = '') */ public function body($value) { - $this->body = $value; - - return $this; - } - - /** - * Set the message icon. - * - * @param string $value - * - * @return $this - */ - public function icon($value) - { - $this->icon = $value; - + if(is_array($value)){ + $this->setParameter('contents',$value); + } else { + $this->setParameter('contents',['en' => $value]); + } return $this; } @@ -87,8 +61,11 @@ public function icon($value) */ public function subject($value) { - $this->subject = $value; - + if(is_array($value)){ + $this->setParameter('headings',$value); + } else { + $this->setParameter('headings',['en' => $value]); + } return $this; } @@ -101,9 +78,7 @@ public function subject($value) */ public function url($value) { - $this->url = $value; - - return $this; + return $this->setUrl($value); } @@ -132,7 +107,7 @@ public function setData($key, $value) */ public function setParameter(string $key, $value) { - $this->extraParameters[$key] = $value; + $this->payload[$key] = $value; return $this; } @@ -144,36 +119,11 @@ public function setParameter(string $key, $value) */ public function toArray() { - $message = [ - 'contents' => ['en' => $this->body], - 'headings' => $this->subjectToArray(), - 'url' => $this->url, - 'chrome_web_icon' => $this->icon, - 'chrome_icon' => $this->icon, - 'adm_small_icon' => $this->icon, - 'small_icon' => $this->icon, - ]; - - foreach ($this->extraParameters as $key => $value) { - Arr::set($message, $key, $value); - } foreach ($this->data as $data => $value) { - Arr::set($message, 'data.'.$data, $value); - } - - return $message; - } - - /** - * @return array - */ - protected function subjectToArray() - { - if ($this->subject === null) { - return []; + Arr::set($this->payload, 'data.'.$data, $value); } - return ['en' => $this->subject]; + return $this->payload; } } diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php index e3dc13b..a4ac706 100644 --- a/src/Traits/Categories/AppearanceHelpers.php +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -94,4 +94,18 @@ public function setSound(string $soundUrl) ->setIosSound($soundUrl) ->setWindowsSound($soundUrl); } + + /** + * Set the message icon. + * + * @param string $iconPath + * + * @return $this + */ + public function icon(string $iconPath){ + return $this->setParameter('chrome_web_icon', $iconPath) + ->setParameter('chrome_icon', $iconPath) + ->setParameter('adm_small_icon', $iconPath) + ->setParameter('small_icon', $iconPath); + } } \ No newline at end of file diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 9bec5d4..eb0ac32 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -53,7 +53,7 @@ public function it_can_set_the_subject() /** @test */ public function it_does_not_append_empty_subject_value_when_subject_is_null() { - $this->assertEquals([], Arr::get($this->message->toArray(), 'headings')); + $this->assertEquals(null, Arr::get($this->message->toArray(), 'headings')); } /** @test */ From 95fe2631ad0f58a47e85472de6a8f6f8eaf2bd5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 07:46:20 +0100 Subject: [PATCH 05/21] Make the setData Method call setParameter directly & allow the dot notation on setParameter --- src/OneSignalMessage.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 47dd77f..c8ba640 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -86,15 +86,13 @@ public function url($value) * Set additional data. * * @param string $key - * @param string $value + * @param mixed $value * * @return $this */ - public function setData($key, $value) + public function setData(string $key, $value) { - $this->data[$key] = $value; - - return $this; + return $this->setParameter("data.{$key}", $value); } /** @@ -107,8 +105,7 @@ public function setData($key, $value) */ public function setParameter(string $key, $value) { - $this->payload[$key] = $value; - + Arr::set($this->payload,$key,$value); return $this; } @@ -119,11 +116,6 @@ public function setParameter(string $key, $value) */ public function toArray() { - - foreach ($this->data as $data => $value) { - Arr::set($this->payload, 'data.'.$data, $value); - } - return $this->payload; } } From b926b131ff70c5fe39346cae0b393ae3cdc9d551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 07:48:35 +0100 Subject: [PATCH 06/21] Apply fixes from StyleCI (#59) --- src/OneSignalMessage.php | 26 ++++++++++----------- src/Traits/Categories/AppearanceHelpers.php | 9 ++++--- src/Traits/Categories/AttachmentHelpers.php | 24 +++++++++---------- src/Traits/Categories/ButtonHelpers.php | 21 ++++++++++------- src/Traits/Categories/DeliveryHelpers.php | 17 ++++++-------- src/Traits/Categories/GroupingHelpers.php | 22 ++++++++--------- src/Traits/OneSignalHelpers.php | 8 +++---- 7 files changed, 60 insertions(+), 67 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index c8ba640..17a4422 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -9,11 +9,9 @@ class OneSignalMessage { use OneSignalHelpers; - /** @var array */ protected $data = []; - /** @var array */ protected $payload = []; @@ -32,7 +30,7 @@ public static function create($body = '') */ public function __construct($body = '') { - $this->body($body); + $this->body($body); } /** @@ -44,11 +42,12 @@ public function __construct($body = '') */ public function body($value) { - if(is_array($value)){ - $this->setParameter('contents',$value); + if (is_array($value)) { + $this->setParameter('contents', $value); } else { - $this->setParameter('contents',['en' => $value]); + $this->setParameter('contents', ['en' => $value]); } + return $this; } @@ -61,11 +60,12 @@ public function body($value) */ public function subject($value) { - if(is_array($value)){ - $this->setParameter('headings',$value); + if (is_array($value)) { + $this->setParameter('headings', $value); } else { - $this->setParameter('headings',['en' => $value]); + $this->setParameter('headings', ['en' => $value]); } + return $this; } @@ -78,10 +78,9 @@ public function subject($value) */ public function url($value) { - return $this->setUrl($value); + return $this->setUrl($value); } - /** * Set additional data. * @@ -105,12 +104,11 @@ public function setData(string $key, $value) */ public function setParameter(string $key, $value) { - Arr::set($this->payload,$key,$value); + Arr::set($this->payload, $key, $value); + return $this; } - - /** * @return array */ diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php index a4ac706..27bf3a6 100644 --- a/src/Traits/Categories/AppearanceHelpers.php +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -2,10 +2,8 @@ namespace NotificationChannels\OneSignal\Traits\Categories; - trait AppearanceHelpers { - /** * Set the iOS badge increment count. * @@ -82,7 +80,7 @@ public function setWindowsSound(string $soundUrl) } /** - * Set the Sound for all Systems + * Set the Sound for all Systems. * * @param string $soundUrl * @@ -102,10 +100,11 @@ public function setSound(string $soundUrl) * * @return $this */ - public function icon(string $iconPath){ + public function icon(string $iconPath) + { return $this->setParameter('chrome_web_icon', $iconPath) ->setParameter('chrome_icon', $iconPath) ->setParameter('adm_small_icon', $iconPath) ->setParameter('small_icon', $iconPath); } -} \ No newline at end of file +} diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php index 631067b..69a5c67 100644 --- a/src/Traits/Categories/AttachmentHelpers.php +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -2,12 +2,10 @@ namespace NotificationChannels\OneSignal\Traits\Categories; - trait AttachmentHelpers { - /** - * Set an Image only for iOS + * Set an Image only for iOS. * * @param string $imageUrl * @@ -19,7 +17,7 @@ public function setIosAttachment(string $imageUrl) } /** - * Set multiple Images only for iOS + * Set multiple Images only for iOS. * * @param array $images * @@ -31,7 +29,7 @@ public function setIosAttachments(array $images) } /** - * Set the Big Picture Image only for Android + * Set the Big Picture Image only for Android. * * @param string $imageUrl * @@ -43,7 +41,7 @@ public function setAndroidBigPicture(string $imageUrl) } /** - * Set the Big Picture Image only for FireOS (Amazon) + * Set the Big Picture Image only for FireOS (Amazon). * * @param string $imageUrl * @@ -55,7 +53,7 @@ public function setAmazonBigPicture(string $imageUrl) } /** - * Set the Big Picture Image only for Chrome + * Set the Big Picture Image only for Chrome. * * @param string $imageUrl * @@ -67,22 +65,24 @@ public function setChromeBigPicture(string $imageUrl) } /** - * Set the additional Data for all Platforms + * Set the additional Data for all Platforms. * @param array $data * * @return $this */ - public function setData(array $data){ + public function setData(array $data) + { return $this->setParameter('data', $data); } /** - * Set the additional URL for all Platforms + * Set the additional URL for all Platforms. * @param string $url * * @return $this */ - public function setUrl(string $url){ + public function setUrl(string $url) + { return $this->setParameter('url', $url); } @@ -100,4 +100,4 @@ public function setImageAttachments(string $imageUrl) ->setAmazonBigPicture($imageUrl) ->setChromeBigPicture($imageUrl); } -} \ No newline at end of file +} diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php index 4d61011..5ff6a74 100644 --- a/src/Traits/Categories/ButtonHelpers.php +++ b/src/Traits/Categories/ButtonHelpers.php @@ -2,9 +2,8 @@ namespace NotificationChannels\OneSignal\Traits\Categories; - -use NotificationChannels\OneSignal\OneSignalWebButton; use NotificationChannels\OneSignal\OneSignalButton; +use NotificationChannels\OneSignal\OneSignalWebButton; trait ButtonHelpers { @@ -17,9 +16,9 @@ trait ButtonHelpers */ public function webButton(OneSignalWebButton $button) { - return $this->setParameter('web_buttons',[$button->toArray()]); - + return $this->setParameter('web_buttons', [$button->toArray()]); } + /** * Adds more than one web button to the message. * @@ -29,9 +28,11 @@ public function webButton(OneSignalWebButton $button) */ public function webButtons(array $buttons) { - return $this->setParameter('web_buttons',collect($buttons)->map(function($button) { return $button->toArray();})); - + return $this->setParameter('web_buttons', collect($buttons)->map(function ($button) { + return $button->toArray(); + })); } + /** * Add a native button to the message. * @@ -41,7 +42,7 @@ public function webButtons(array $buttons) */ public function button(OneSignalButton $button) { - return $this->setParameter('buttons',[$button->toArray()]); + return $this->setParameter('buttons', [$button->toArray()]); } /** @@ -53,6 +54,8 @@ public function button(OneSignalButton $button) */ public function buttons(array $buttons) { - return $this->setParameter('buttons',collect($buttons)->map(function($button) { return $button->toArray();})); + return $this->setParameter('buttons', collect($buttons)->map(function ($button) { + return $button->toArray(); + })); } -} \ No newline at end of file +} diff --git a/src/Traits/Categories/DeliveryHelpers.php b/src/Traits/Categories/DeliveryHelpers.php index fe82e7d..f927a75 100644 --- a/src/Traits/Categories/DeliveryHelpers.php +++ b/src/Traits/Categories/DeliveryHelpers.php @@ -2,12 +2,10 @@ namespace NotificationChannels\OneSignal\Traits\Categories; - trait DeliveryHelpers { - /** - * Set the send after + * Set the send after. * * @param string $date * @@ -15,11 +13,11 @@ trait DeliveryHelpers */ public function setSendAfter(string $date) { - return $this->setParameter('send_after',$date); + return $this->setParameter('send_after', $date); } /** - * Set the deplayed option + * Set the deplayed option. * * @param string $delayedOption * @@ -31,7 +29,7 @@ public function setDelayedOption(string $delayedOption) } /** - * Set the delivery at time of the day. Use with delayed option = timezone + * Set the delivery at time of the day. Use with delayed option = timezone. * * @param string $timeOfDay * @@ -43,7 +41,7 @@ public function setDeliveryTimeOfDay(string $timeOfDay) } /** - * Set the Time to Live in Seconds + * Set the Time to Live in Seconds. * * @param int $ttl * @@ -55,7 +53,7 @@ public function setTTL(int $ttl) } /** - * Set the Priority + * Set the Priority. * * @param int $priority * @@ -65,5 +63,4 @@ public function setPriority(string $priority) { return $this->setParameter('priority', $priority); } - -} \ No newline at end of file +} diff --git a/src/Traits/Categories/GroupingHelpers.php b/src/Traits/Categories/GroupingHelpers.php index ab496d8..a5a75f7 100644 --- a/src/Traits/Categories/GroupingHelpers.php +++ b/src/Traits/Categories/GroupingHelpers.php @@ -2,12 +2,10 @@ namespace NotificationChannels\OneSignal\Traits\Categories; - trait GroupingHelpers { - /** - * Set the Android Grouping Parameters + * Set the Android Grouping Parameters. * @param string $group * @param array $groupMessage * @@ -15,12 +13,12 @@ trait GroupingHelpers */ public function setAndroidGroup(string $group, array $groupMessage) { - return $this->setParameter('android_group',$group) - ->setParameter('android_group_message',$groupMessage); + return $this->setParameter('android_group', $group) + ->setParameter('android_group_message', $groupMessage); } /** - * Set the Amazon (FireOS) Grouping Parameters + * Set the Amazon (FireOS) Grouping Parameters. * @param string $group * @param array $groupMessage * @@ -28,12 +26,12 @@ public function setAndroidGroup(string $group, array $groupMessage) */ public function setAmazonGroup(string $group, array $groupMessage) { - return $this->setParameter('adm_group',$group) - ->setParameter('adm_group_message',$groupMessage); + return $this->setParameter('adm_group', $group) + ->setParameter('adm_group_message', $groupMessage); } /** - * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)) + * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)). * @param string $group * @param array $groupMessage * @@ -41,7 +39,7 @@ public function setAmazonGroup(string $group, array $groupMessage) */ public function setGroup(string $group, array $groupMessage) { - return $this->setAndroidGroup($group,$groupMessage) - ->setAmazonGroup($group,$groupMessage); + return $this->setAndroidGroup($group, $groupMessage) + ->setAmazonGroup($group, $groupMessage); } -} \ No newline at end of file +} diff --git a/src/Traits/OneSignalHelpers.php b/src/Traits/OneSignalHelpers.php index bbc1e27..2b7b7f9 100644 --- a/src/Traits/OneSignalHelpers.php +++ b/src/Traits/OneSignalHelpers.php @@ -2,15 +2,13 @@ namespace NotificationChannels\OneSignal\Traits; - -use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; -use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; trait OneSignalHelpers { - use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers; -} \ No newline at end of file +} From 71018956a66c394bc5c9bca81255e977a34bcb59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 09:04:44 +0100 Subject: [PATCH 07/21] Working on Code with suggestions from timacdonald --- src/OneSignalMessage.php | 33 ++++++++++----------- src/Traits/Categories/AppearanceHelpers.php | 3 +- src/Traits/Categories/AttachmentHelpers.php | 12 +------- src/Traits/Categories/DeliveryHelpers.php | 4 +-- src/Traits/Categories/GroupingHelpers.php | 3 ++ 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 17a4422..1fa6c32 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -7,6 +7,7 @@ class OneSignalMessage { + use OneSignalHelpers; /** @var array */ @@ -36,39 +37,35 @@ public function __construct($body = '') /** * Set the message body. * - * @param string $value + * @param mixed $value * * @return $this */ public function body($value) { - if (is_array($value)) { - $this->setParameter('contents', $value); - } else { - $this->setParameter('contents', ['en' => $value]); - } - - return $this; + return $this->setParameter('contents', $this->parseValueToArray($value)); } /** * Set the message subject. * - * @param string $value + * @param mixed $value * * @return $this */ public function subject($value) { - if (is_array($value)) { - $this->setParameter('headings', $value); - } else { - $this->setParameter('headings', ['en' => $value]); - } - - return $this; + return $this->setParameter('headings',$this->parseValueToArray($value)); } + /** + * @param mixed $value + * + * @return array + */ + protected function parseValueToArray($value){ + return (is_array($value)) ? $value : ['en' => $value]; + } /** * Set the message url. * @@ -85,7 +82,7 @@ public function url($value) * Set additional data. * * @param string $key - * @param mixed $value + * @param mixed $value * * @return $this */ @@ -98,7 +95,7 @@ public function setData(string $key, $value) * Set additional parameters. * * @param string $key - * @param mixed $value + * @param mixed $value * * @return $this */ diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php index 27bf3a6..17dc412 100644 --- a/src/Traits/Categories/AppearanceHelpers.php +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -76,7 +76,8 @@ public function setAndroidSound(string $soundUrl) */ public function setWindowsSound(string $soundUrl) { - return $this->setParameter('wp_sound', $soundUrl)->setParameter('wp_wns_sound', $soundUrl); + return $this->setParameter('wp_sound', $soundUrl) + ->setParameter('wp_wns_sound', $soundUrl); } /** diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php index 69a5c67..07208be 100644 --- a/src/Traits/Categories/AttachmentHelpers.php +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -64,19 +64,9 @@ public function setChromeBigPicture(string $imageUrl) return $this->setParameter('chrome_big_picture', $imageUrl); } - /** - * Set the additional Data for all Platforms. - * @param array $data - * - * @return $this - */ - public function setData(array $data) - { - return $this->setParameter('data', $data); - } - /** * Set the additional URL for all Platforms. + * * @param string $url * * @return $this diff --git a/src/Traits/Categories/DeliveryHelpers.php b/src/Traits/Categories/DeliveryHelpers.php index f927a75..a98dff9 100644 --- a/src/Traits/Categories/DeliveryHelpers.php +++ b/src/Traits/Categories/DeliveryHelpers.php @@ -47,7 +47,7 @@ public function setDeliveryTimeOfDay(string $timeOfDay) * * @return $this */ - public function setTTL(int $ttl) + public function setTtl(int $ttl) { return $this->setParameter('ttl', $ttl); } @@ -59,7 +59,7 @@ public function setTTL(int $ttl) * * @return $this */ - public function setPriority(string $priority) + public function setPriority(int $priority) { return $this->setParameter('priority', $priority); } diff --git a/src/Traits/Categories/GroupingHelpers.php b/src/Traits/Categories/GroupingHelpers.php index a5a75f7..37ffdb5 100644 --- a/src/Traits/Categories/GroupingHelpers.php +++ b/src/Traits/Categories/GroupingHelpers.php @@ -6,6 +6,7 @@ trait GroupingHelpers { /** * Set the Android Grouping Parameters. + * * @param string $group * @param array $groupMessage * @@ -19,6 +20,7 @@ public function setAndroidGroup(string $group, array $groupMessage) /** * Set the Amazon (FireOS) Grouping Parameters. + * * @param string $group * @param array $groupMessage * @@ -32,6 +34,7 @@ public function setAmazonGroup(string $group, array $groupMessage) /** * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)). + * * @param string $group * @param array $groupMessage * From 5095faf22242786184790b2b781e6e8e1a45fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 09:11:58 +0100 Subject: [PATCH 08/21] Working on Code with suggestions from timacdonald #2 --- src/Traits/Categories/AttachmentHelpers.php | 21 +++++---------------- src/Traits/Categories/GroupingHelpers.php | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php index 07208be..9a389fe 100644 --- a/src/Traits/Categories/AttachmentHelpers.php +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -5,27 +5,16 @@ trait AttachmentHelpers { /** - * Set an Image only for iOS. + * Set an Image/more than one Image only for iOS. * - * @param string $imageUrl - * - * @return $this - */ - public function setIosAttachment(string $imageUrl) - { - return $this->setParameter('ios_attachments', ['id1' => $imageUrl]); - } - - /** - * Set multiple Images only for iOS. - * - * @param array $images + * @param string|array $imageUrl * * @return $this */ - public function setIosAttachments(array $images) + public function setIosAttachment($imageUrl) { - return $this->setParameter('ios_attachments', $images); + $imageUrl = is_array($imageUrl) ? $imageUrl : ['id1' => $imageUrl]; + return $this->setParameter('ios_attachments', $imageUrl); } /** diff --git a/src/Traits/Categories/GroupingHelpers.php b/src/Traits/Categories/GroupingHelpers.php index 37ffdb5..c6586fa 100644 --- a/src/Traits/Categories/GroupingHelpers.php +++ b/src/Traits/Categories/GroupingHelpers.php @@ -6,7 +6,7 @@ trait GroupingHelpers { /** * Set the Android Grouping Parameters. - * + * * @param string $group * @param array $groupMessage * From 3be802c1e5b0dd0dfc9180f3409871503560cd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 14 Mar 2018 11:00:59 +0100 Subject: [PATCH 09/21] Add Deprecated DocTypes & remove Trait the contains only other Traits --- src/OneSignalMessage.php | 65 ++++++++++++++++++--- src/Traits/Categories/AppearanceHelpers.php | 15 +++++ src/Traits/Categories/ButtonHelpers.php | 60 ++++++++++++++++++- src/Traits/OneSignalHelpers.php | 14 ----- 4 files changed, 129 insertions(+), 25 deletions(-) delete mode 100644 src/Traits/OneSignalHelpers.php diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 1fa6c32..8febf38 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,15 +3,17 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; -use NotificationChannels\OneSignal\Traits\OneSignalHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; +use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; +use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; +use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; + class OneSignalMessage { - use OneSignalHelpers; - - /** @var array */ - protected $data = []; + use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers; /** @var array */ protected $payload = []; @@ -31,7 +33,7 @@ public static function create($body = '') */ public function __construct($body = '') { - $this->body($body); + $this->setBody($body); } /** @@ -39,9 +41,23 @@ public function __construct($body = '') * * @param mixed $value * + * @deprecated use setBody instead + * * @return $this */ public function body($value) + { + return $this->setBody($value); + } + + /** + * Set the message body. + * + * @param mixed $value + * + * @return $this + */ + public function setBody($value) { return $this->setParameter('contents', $this->parseValueToArray($value)); } @@ -51,11 +67,25 @@ public function body($value) * * @param mixed $value * + * @deprecated Use setSubject instead + * * @return $this */ public function subject($value) { - return $this->setParameter('headings',$this->parseValueToArray($value)); + return $this->setParameter('headings', $this->parseValueToArray($value)); + } + + /** + * Set the message subject. + * + * @param mixed $value + * + * @return $this + */ + public function setSubject($value) + { + return $this->setSubject($value); } /** @@ -63,14 +93,18 @@ public function subject($value) * * @return array */ - protected function parseValueToArray($value){ + protected function parseValueToArray($value) + { return (is_array($value)) ? $value : ['en' => $value]; } + /** * Set the message url. * * @param string $value * + * @deprecated use setUrl Instead + * * @return $this */ public function url($value) @@ -92,7 +126,7 @@ public function setData(string $key, $value) } /** - * Set additional parameters. + * Set parameters. * * @param string $key * @param mixed $value @@ -106,6 +140,19 @@ public function setParameter(string $key, $value) return $this; } + /** + * Get parameters. + * + * @param string $key + * @param mixed $default + * + * @return mixed + */ + public function getParameter(string $key, $default = null) + { + return Arr::get($this->payload, $key, $default); + } + /** * @return array */ diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php index 17dc412..ecab988 100644 --- a/src/Traits/Categories/AppearanceHelpers.php +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -4,6 +4,7 @@ trait AppearanceHelpers { + /** * Set the iOS badge increment count. * @@ -99,9 +100,23 @@ public function setSound(string $soundUrl) * * @param string $iconPath * + * @deprecated use setIcon instead + * * @return $this */ public function icon(string $iconPath) + { + return $this->setIcon($iconPath); + } + + /** + * Set the message icon. + * + * @param string $iconPath + * + * @return $this + */ + public function setIcon(string $iconPath) { return $this->setParameter('chrome_web_icon', $iconPath) ->setParameter('chrome_icon', $iconPath) diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php index 5ff6a74..13ae4a8 100644 --- a/src/Traits/Categories/ButtonHelpers.php +++ b/src/Traits/Categories/ButtonHelpers.php @@ -7,16 +7,19 @@ trait ButtonHelpers { + /** * Add a web button to the message. * * @param OneSignalWebButton $button * + * @deprecated use setWebButton instead + * * @return $this */ public function webButton(OneSignalWebButton $button) { - return $this->setParameter('web_buttons', [$button->toArray()]); + return $this->setWebButton($button); } /** @@ -24,9 +27,35 @@ public function webButton(OneSignalWebButton $button) * * @param array[OnSignalWebButton] $buttons * + * @deprecated use setWebButtons instead + * * @return $this */ public function webButtons(array $buttons) + { + return $this->setWebButtons($buttons); + } + + /** + * Add a web button to the message. + * + * @param OneSignalWebButton $button + * + * @return $this + */ + public function setWebButton(OneSignalWebButton $button) + { + return $this->setParameter('web_buttons', array_merge($this->getParameter('web_buttons', []), [$button->toArray()])); + } + + /** + * Adds more than one web button to the message. + * + * @param array[OnSignalWebButton] $buttons + * + * @return $this + */ + public function setWebButtons(array $buttons) { return $this->setParameter('web_buttons', collect($buttons)->map(function ($button) { return $button->toArray(); @@ -38,11 +67,24 @@ public function webButtons(array $buttons) * * @param OneSignalButton $button * + * @deprecated use setButton instead * @return $this */ public function button(OneSignalButton $button) { - return $this->setParameter('buttons', [$button->toArray()]); + return $this->setButton($button); + } + + /** + * Add a native button to the message. + * + * @param OneSignalButton $button + * + * @return $this + */ + public function setButton(OneSignalButton $button) + { + return $this->setParameter('buttons', array_merge($this->getParameter('buttons', []), [$button->toArray()])); } /** @@ -50,9 +92,23 @@ public function button(OneSignalButton $button) * * @param array $buttons * + * @deprecated use setButtons instead + * * @return $this */ public function buttons(array $buttons) + { + return $this->setButtons($buttons); + } + + /** + * Adds more than one native button to the message. + * + * @param array $buttons + * + * @return $this + */ + public function setButtons(array $buttons) { return $this->setParameter('buttons', collect($buttons)->map(function ($button) { return $button->toArray(); diff --git a/src/Traits/OneSignalHelpers.php b/src/Traits/OneSignalHelpers.php deleted file mode 100644 index 2b7b7f9..0000000 --- a/src/Traits/OneSignalHelpers.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Thu, 15 Mar 2018 09:09:31 +0100 Subject: [PATCH 10/21] Add Deprecated Trait that contains all deprecated methods Add Test for Chaning Buttons (close #60) --- src/OneSignalMessage.php | 49 +--------- src/Traits/Categories/ButtonHelpers.php | 69 +++------------ src/Traits/Deprecated.php | 113 ++++++++++++++++++++++++ tests/MessageTest.php | 34 +++++-- 4 files changed, 157 insertions(+), 108 deletions(-) create mode 100644 src/Traits/Deprecated.php diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 8febf38..90ba5cd 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,17 +3,15 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; -use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; -use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; -use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; -use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; -use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; +use NotificationChannels\OneSignal\Traits\{ + Categories\AppearanceHelpers, Categories\AttachmentHelpers, Categories\ButtonHelpers, Categories\DeliveryHelpers, Categories\GroupingHelpers, Deprecated +}; class OneSignalMessage { - use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers; + use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, Deprecated; /** @var array */ protected $payload = []; @@ -36,19 +34,6 @@ public function __construct($body = '') $this->setBody($body); } - /** - * Set the message body. - * - * @param mixed $value - * - * @deprecated use setBody instead - * - * @return $this - */ - public function body($value) - { - return $this->setBody($value); - } /** * Set the message body. @@ -62,19 +47,6 @@ public function setBody($value) return $this->setParameter('contents', $this->parseValueToArray($value)); } - /** - * Set the message subject. - * - * @param mixed $value - * - * @deprecated Use setSubject instead - * - * @return $this - */ - public function subject($value) - { - return $this->setParameter('headings', $this->parseValueToArray($value)); - } /** * Set the message subject. @@ -98,19 +70,6 @@ protected function parseValueToArray($value) return (is_array($value)) ? $value : ['en' => $value]; } - /** - * Set the message url. - * - * @param string $value - * - * @deprecated use setUrl Instead - * - * @return $this - */ - public function url($value) - { - return $this->setUrl($value); - } /** * Set additional data. diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php index 13ae4a8..e6d3291 100644 --- a/src/Traits/Categories/ButtonHelpers.php +++ b/src/Traits/Categories/ButtonHelpers.php @@ -8,33 +8,6 @@ trait ButtonHelpers { - /** - * Add a web button to the message. - * - * @param OneSignalWebButton $button - * - * @deprecated use setWebButton instead - * - * @return $this - */ - public function webButton(OneSignalWebButton $button) - { - return $this->setWebButton($button); - } - - /** - * Adds more than one web button to the message. - * - * @param array[OnSignalWebButton] $buttons - * - * @deprecated use setWebButtons instead - * - * @return $this - */ - public function webButtons(array $buttons) - { - return $this->setWebButtons($buttons); - } /** * Add a web button to the message. @@ -57,24 +30,15 @@ public function setWebButton(OneSignalWebButton $button) */ public function setWebButtons(array $buttons) { - return $this->setParameter('web_buttons', collect($buttons)->map(function ($button) { - return $button->toArray(); - })); - } + collect($buttons)->map(function ($button) { + $this->setWebButton($button); + }); - /** - * Add a native button to the message. - * - * @param OneSignalButton $button - * - * @deprecated use setButton instead - * @return $this - */ - public function button(OneSignalButton $button) - { - return $this->setButton($button); + return $this; } + + /** * Add a native button to the message. * @@ -87,19 +51,6 @@ public function setButton(OneSignalButton $button) return $this->setParameter('buttons', array_merge($this->getParameter('buttons', []), [$button->toArray()])); } - /** - * Adds more than one native button to the message. - * - * @param array $buttons - * - * @deprecated use setButtons instead - * - * @return $this - */ - public function buttons(array $buttons) - { - return $this->setButtons($buttons); - } /** * Adds more than one native button to the message. @@ -110,8 +61,10 @@ public function buttons(array $buttons) */ public function setButtons(array $buttons) { - return $this->setParameter('buttons', collect($buttons)->map(function ($button) { - return $button->toArray(); - })); + collect($buttons)->map(function ($button) { + $this->setButton($button); + }); + + return $this; } } diff --git a/src/Traits/Deprecated.php b/src/Traits/Deprecated.php new file mode 100644 index 0000000..6eb5c3e --- /dev/null +++ b/src/Traits/Deprecated.php @@ -0,0 +1,113 @@ +setBody($value); + } + + /** + * Set the message subject. + * + * @param mixed $value + * + * @deprecated Use setSubject instead + * + * @return $this + */ + public function subject($value) + { + return $this->setParameter('headings', $this->parseValueToArray($value)); + } + + /** + * Set the message url. + * + * @param string $value + * + * @deprecated use setUrl Instead + * + * @return $this + */ + public function url($value) + { + return $this->setUrl($value); + } + + /** + * Add a web button to the message. + * + * @param OneSignalWebButton $button + * + * @deprecated use setWebButton instead + * + * @return $this + */ + public function webButton(OneSignalWebButton $button) + { + return $this->setWebButton($button); + } + + /** + * Adds more than one web button to the message. + * + * @param array[OnSignalWebButton] $buttons + * + * @deprecated use setWebButtons instead + * + * @return $this + */ + public function webButtons(array $buttons) + { + return $this->setWebButtons($buttons); + } + + /** + * Add a native button to the message. + * + * @param OneSignalButton $button + * + * @deprecated use setButton instead + * @return $this + */ + public function button(OneSignalButton $button) + { + return $this->setButton($button); + } + + /** + * Adds more than one native button to the message. + * + * @param array $buttons + * + * @deprecated use setButtons instead + * + * @return $this + */ + public function buttons(array $buttons) + { + return $this->setButtons($buttons); + } +} \ No newline at end of file diff --git a/tests/MessageTest.php b/tests/MessageTest.php index eb0ac32..9d164fe 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -37,7 +37,7 @@ public function it_provides_a_create_method() /** @test */ public function it_can_set_the_body() { - $this->message->body('myBody'); + $this->message->setBody('myBody'); $this->assertEquals('myBody', Arr::get($this->message->toArray(), 'contents.en')); } @@ -45,7 +45,7 @@ public function it_can_set_the_body() /** @test */ public function it_can_set_the_subject() { - $this->message->subject('mySubject'); + $this->message->setSubject('mySubject'); $this->assertEquals('mySubject', Arr::get($this->message->toArray(), 'headings.en')); } @@ -59,7 +59,7 @@ public function it_does_not_append_empty_subject_value_when_subject_is_null() /** @test */ public function it_can_set_the_url() { - $this->message->url('myURL'); + $this->message->setUrl('myURL'); $this->assertEquals('myURL', Arr::get($this->message->toArray(), 'url')); } @@ -114,7 +114,7 @@ public function it_can_set_additional_parameter() /** @test */ public function it_can_set_the_icon() { - $this->message->icon('myIcon'); + $this->message->setIcon('myIcon'); $this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_web_icon')); $this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_icon')); @@ -141,7 +141,7 @@ public function it_can_set_a_web_button() /** @test */ public function it_can_set_a_button() { - $this->message->button( + $this->message->setButton( OneSignalButton::create('buttonID') ->text('buttonText') ->icon('buttonIcon') @@ -151,6 +151,30 @@ public function it_can_set_a_button() $this->assertEquals('buttonText', Arr::get($this->message->toArray(), 'buttons.0.text')); $this->assertEquals('buttonIcon', Arr::get($this->message->toArray(), 'buttons.0.icon')); } + /** @test */ + public function it_can_set_a_web_buttons_with_chain() + { + $this->message->setWebButton( + OneSignalWebButton::create('buttonID_1') + ->text('buttonText_1') + ->url('buttonURL_1') + ->icon('buttonIcon_1') + )->setWebButton( + OneSignalWebButton::create('buttonID_2') + ->text('buttonText_2') + ->url('buttonURL_2') + ->icon('buttonIcon_2') + ); + + $this->assertEquals('buttonID_1', Arr::get($this->message->toArray(), 'web_buttons.0.id')); + $this->assertEquals('buttonText_1', Arr::get($this->message->toArray(), 'web_buttons.0.text')); + $this->assertEquals('buttonURL_1', Arr::get($this->message->toArray(), 'web_buttons.0.url')); + $this->assertEquals('buttonIcon_1', Arr::get($this->message->toArray(), 'web_buttons.0.icon')); + $this->assertEquals('buttonID_2', Arr::get($this->message->toArray(), 'web_buttons.1.id')); + $this->assertEquals('buttonText_2', Arr::get($this->message->toArray(), 'web_buttons.1.text')); + $this->assertEquals('buttonURL_2', Arr::get($this->message->toArray(), 'web_buttons.1.url')); + $this->assertEquals('buttonIcon_2', Arr::get($this->message->toArray(), 'web_buttons.1.icon')); + } /** @test */ public function it_can_set_a_image() From d14ac9a0d765003e7b8852a58cb12dd08b8be1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 15 Mar 2018 09:16:48 +0100 Subject: [PATCH 11/21] i really love travis :D Fixed Maximum function nesting level of '256' reached, aborting! --- src/OneSignalMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 90ba5cd..305a81e 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -57,7 +57,7 @@ public function setBody($value) */ public function setSubject($value) { - return $this->setSubject($value); + return $this->setParameter('headings', $this->parseValueToArray($value)); } /** From 52bfc9ed95c7224d5f37b09c6a28d93c8a6641f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20=C3=96stling?= Date: Tue, 22 May 2018 10:18:08 +0200 Subject: [PATCH 12/21] Add support for silent messages (#67) A defensive change in message class adding support for silent push notifications (in the traits branch now). --- src/OneSignalMessage.php | 4 ++-- src/Traits/Categories/SilentHelpers.php | 18 ++++++++++++++++++ tests/ChannelTest.php | 19 +++++++++++++++++++ tests/TestSilentNotification.php | 17 +++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/Traits/Categories/SilentHelpers.php create mode 100644 tests/TestSilentNotification.php diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 305a81e..af21d72 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -4,14 +4,14 @@ use Illuminate\Support\Arr; use NotificationChannels\OneSignal\Traits\{ - Categories\AppearanceHelpers, Categories\AttachmentHelpers, Categories\ButtonHelpers, Categories\DeliveryHelpers, Categories\GroupingHelpers, Deprecated + Categories\AppearanceHelpers, Categories\AttachmentHelpers, Categories\ButtonHelpers, Categories\DeliveryHelpers, Categories\GroupingHelpers, Categories\SilentHelpers, Deprecated }; class OneSignalMessage { - use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, Deprecated; + use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated; /** @var array */ protected $payload = []; diff --git a/src/Traits/Categories/SilentHelpers.php b/src/Traits/Categories/SilentHelpers.php new file mode 100644 index 0000000..384e897 --- /dev/null +++ b/src/Traits/Categories/SilentHelpers.php @@ -0,0 +1,18 @@ +payload, 'contents'); //removes any contents that are set by constructor. + return $this->setParameter('content_available', 1); + } +} diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 68697e6..6f70008 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -165,4 +165,23 @@ public function it_sends_nothing_and_returns_null_when_player_id_empty() $channel_response = $this->channel->send(new EmptyNotifiable(), new TestNotification()); $this->assertNull($channel_response); } + + public function it_can_send_a_silent_notification() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'content_available' => 1, + 'data.action' => 'reload', + 'data.target' => 'inbox', + 'include_player_ids' => collect('player_id'), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new Notifiable(), new TestSilentNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } } diff --git a/tests/TestSilentNotification.php b/tests/TestSilentNotification.php new file mode 100644 index 0000000..185ddc1 --- /dev/null +++ b/tests/TestSilentNotification.php @@ -0,0 +1,17 @@ +setSilent() + ->setData('action', 'reload') + ->setData('target', 'inbox'); + } +} From b1e9d1b644ffa1163da7bc1466ef405dfda9af17 Mon Sep 17 00:00:00 2001 From: Dyego Nery <4934602+dyegonery@users.noreply.github.com> Date: Thu, 9 Aug 2018 05:13:52 -0300 Subject: [PATCH 13/21] Add method to set the template_id of the message (#77) * Add method to set the template_id of the message * Fix typo on method for setting template * Remove contents when using a template Remove any contents set by the constructor when using the `setTemplate()` method. --- src/OneSignalMessage.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index af21d72..b548d53 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -60,6 +60,20 @@ public function setSubject($value) return $this->setParameter('headings', $this->parseValueToArray($value)); } + + /** + * Set the message template_id + * + * @param string $value + * + * @return $this + */ + public function setTemplate($value) + { + Arr::forget($this->payload, 'contents'); + return $this->setParameter('template_id', $value); + } + /** * @param mixed $value * From a14695b4e0d530d28dc51d52cba68b5816883737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 09:58:19 +0200 Subject: [PATCH 14/21] Fix Style CI --- .styleci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index 916d27e..0285f17 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,3 +1 @@ preset: laravel - -linting: true From 860ce55fdc1b98191a7e6aa522d1771c44f08bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 10:02:38 +0200 Subject: [PATCH 15/21] Fix Style CI Issues --- src/OneSignalMessage.php | 16 ++++++++-------- src/Traits/Categories/AttachmentHelpers.php | 1 + src/Traits/Categories/ButtonHelpers.php | 4 ---- src/Traits/Categories/SilentHelpers.php | 2 +- src/Traits/Deprecated.php | 10 +--------- tests/MessageTest.php | 1 + 6 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index b548d53..c461ea8 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,14 +3,17 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; -use NotificationChannels\OneSignal\Traits\{ - Categories\AppearanceHelpers, Categories\AttachmentHelpers, Categories\ButtonHelpers, Categories\DeliveryHelpers, Categories\GroupingHelpers, Categories\SilentHelpers, Deprecated -}; +use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; +use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; +use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; +use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; +use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers; +use NotificationChannels\OneSignal\Traits\Deprecated; class OneSignalMessage { - use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated; /** @var array */ @@ -34,7 +37,6 @@ public function __construct($body = '') $this->setBody($body); } - /** * Set the message body. * @@ -47,7 +49,6 @@ public function setBody($value) return $this->setParameter('contents', $this->parseValueToArray($value)); } - /** * Set the message subject. * @@ -60,9 +61,8 @@ public function setSubject($value) return $this->setParameter('headings', $this->parseValueToArray($value)); } - /** - * Set the message template_id + * Set the message template_id. * * @param string $value * diff --git a/src/Traits/Categories/AttachmentHelpers.php b/src/Traits/Categories/AttachmentHelpers.php index 9a389fe..3bfda1a 100644 --- a/src/Traits/Categories/AttachmentHelpers.php +++ b/src/Traits/Categories/AttachmentHelpers.php @@ -14,6 +14,7 @@ trait AttachmentHelpers public function setIosAttachment($imageUrl) { $imageUrl = is_array($imageUrl) ? $imageUrl : ['id1' => $imageUrl]; + return $this->setParameter('ios_attachments', $imageUrl); } diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php index e6d3291..11bea79 100644 --- a/src/Traits/Categories/ButtonHelpers.php +++ b/src/Traits/Categories/ButtonHelpers.php @@ -7,8 +7,6 @@ trait ButtonHelpers { - - /** * Add a web button to the message. * @@ -37,8 +35,6 @@ public function setWebButtons(array $buttons) return $this; } - - /** * Add a native button to the message. * diff --git a/src/Traits/Categories/SilentHelpers.php b/src/Traits/Categories/SilentHelpers.php index 384e897..221dce3 100644 --- a/src/Traits/Categories/SilentHelpers.php +++ b/src/Traits/Categories/SilentHelpers.php @@ -6,7 +6,7 @@ trait SilentHelpers { /** - * Enables silent mode + * Enables silent mode. * * @return $this */ diff --git a/src/Traits/Deprecated.php b/src/Traits/Deprecated.php index 6eb5c3e..75d9c43 100644 --- a/src/Traits/Deprecated.php +++ b/src/Traits/Deprecated.php @@ -1,14 +1,6 @@ setButtons($buttons); } -} \ No newline at end of file +} diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 9d164fe..0b0a145 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -151,6 +151,7 @@ public function it_can_set_a_button() $this->assertEquals('buttonText', Arr::get($this->message->toArray(), 'buttons.0.text')); $this->assertEquals('buttonIcon', Arr::get($this->message->toArray(), 'buttons.0.icon')); } + /** @test */ public function it_can_set_a_web_buttons_with_chain() { From 64bb5294b822ea6a9ac3f211a9a79c46baa10fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 10:05:13 +0200 Subject: [PATCH 16/21] Fix Style CI Issues --- src/OneSignalMessage.php | 11 +++++------ src/Traits/Categories/AppearanceHelpers.php | 1 - src/Traits/Categories/ButtonHelpers.php | 1 - src/Traits/Categories/SilentHelpers.php | 2 ++ src/Traits/Deprecated.php | 1 + 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index c461ea8..79ad6cf 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -3,14 +3,13 @@ namespace NotificationChannels\OneSignal; use Illuminate\Support\Arr; -use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; -use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; +use NotificationChannels\OneSignal\Traits\Deprecated; use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers; +use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers; use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers; use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers; -use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers; -use NotificationChannels\OneSignal\Traits\Deprecated; - +use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers; +use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers; class OneSignalMessage { @@ -71,6 +70,7 @@ public function setSubject($value) public function setTemplate($value) { Arr::forget($this->payload, 'contents'); + return $this->setParameter('template_id', $value); } @@ -84,7 +84,6 @@ protected function parseValueToArray($value) return (is_array($value)) ? $value : ['en' => $value]; } - /** * Set additional data. * diff --git a/src/Traits/Categories/AppearanceHelpers.php b/src/Traits/Categories/AppearanceHelpers.php index ecab988..d685f03 100644 --- a/src/Traits/Categories/AppearanceHelpers.php +++ b/src/Traits/Categories/AppearanceHelpers.php @@ -4,7 +4,6 @@ trait AppearanceHelpers { - /** * Set the iOS badge increment count. * diff --git a/src/Traits/Categories/ButtonHelpers.php b/src/Traits/Categories/ButtonHelpers.php index 11bea79..7b3dd06 100644 --- a/src/Traits/Categories/ButtonHelpers.php +++ b/src/Traits/Categories/ButtonHelpers.php @@ -47,7 +47,6 @@ public function setButton(OneSignalButton $button) return $this->setParameter('buttons', array_merge($this->getParameter('buttons', []), [$button->toArray()])); } - /** * Adds more than one native button to the message. * diff --git a/src/Traits/Categories/SilentHelpers.php b/src/Traits/Categories/SilentHelpers.php index 221dce3..5ba33a0 100644 --- a/src/Traits/Categories/SilentHelpers.php +++ b/src/Traits/Categories/SilentHelpers.php @@ -1,6 +1,7 @@ payload, 'contents'); //removes any contents that are set by constructor. + return $this->setParameter('content_available', 1); } } diff --git a/src/Traits/Deprecated.php b/src/Traits/Deprecated.php index 75d9c43..ee78b9d 100644 --- a/src/Traits/Deprecated.php +++ b/src/Traits/Deprecated.php @@ -1,4 +1,5 @@ Date: Thu, 16 Aug 2018 09:06:36 +0100 Subject: [PATCH 17/21] implemented sending of notifications based on segments (included/excluded) (#72) * implemented sending of notifications based on segments (included and excluded) * styleci linting * Apply fixes from StyleCI --- src/OneSignalButton.php | 2 +- src/OneSignalPayloadFactory.php | 26 +++++- src/OneSignalWebButton.php | 4 +- tests/ChannelTest.php | 128 +++++++++++++++++++-------- tests/NotifiableExcludedSegments.php | 16 ++++ tests/NotifiableIncludedSegments.php | 16 ++++ 6 files changed, 150 insertions(+), 42 deletions(-) create mode 100644 tests/NotifiableExcludedSegments.php create mode 100644 tests/NotifiableIncludedSegments.php diff --git a/src/OneSignalButton.php b/src/OneSignalButton.php index 28bea10..93fe450 100644 --- a/src/OneSignalButton.php +++ b/src/OneSignalButton.php @@ -65,7 +65,7 @@ public function text($value) public function toArray() { return [ - 'id' => $this->id, + 'id' => $this->id, 'text' => $this->text, 'icon' => $this->icon, ]; diff --git a/src/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index d2b7855..5d4b47b 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -15,7 +15,7 @@ class OneSignalPayloadFactory * * @return array */ - public static function make($notifiable, Notification $notification, $targeting) : array + public static function make($notifiable, Notification $notification, $targeting): array { $payload = $notification->toOneSignal($notifiable)->toArray(); @@ -23,6 +23,10 @@ public static function make($notifiable, Notification $notification, $targeting) $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); } elseif (static::isTargetingTags($targeting)) { $payload['tags'] = collect([$targeting['tags']]); + } elseif (static::isTargetingIncludedSegments($targeting)) { + $payload['included_segments'] = collect($targeting['included_segments']); + } elseif (static::isTargetingExcludedSegments($targeting)) { + $payload['excluded_segments'] = collect($targeting['excluded_segments']); } else { $payload['include_player_ids'] = collect($targeting); } @@ -30,6 +34,26 @@ public static function make($notifiable, Notification $notification, $targeting) return $payload; } + /** + * @param mixed $targeting + * + * @return bool + */ + protected static function isTargetingIncludedSegments($targeting) + { + return is_array($targeting) && array_key_exists('included_segments', $targeting); + } + + /** + * @param mixed $targeting + * + * @return bool + */ + protected static function isTargetingExcludedSegments($targeting) + { + return is_array($targeting) && array_key_exists('excluded_segments', $targeting); + } + /** * @param mixed $targeting * diff --git a/src/OneSignalWebButton.php b/src/OneSignalWebButton.php index 49b53e5..8c4bb44 100644 --- a/src/OneSignalWebButton.php +++ b/src/OneSignalWebButton.php @@ -82,10 +82,10 @@ public function url($value) public function toArray() { return [ - 'id' => $this->id, + 'id' => $this->id, 'text' => $this->text, 'icon' => $this->icon, - 'url' => $this->url, + 'url' => $this->url, ]; } } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 6f70008..00e49e4 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -40,13 +40,13 @@ public function it_can_send_a_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); @@ -64,13 +64,13 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); @@ -90,13 +90,13 @@ public function it_can_send_a_notification_with_array() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', - 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', 'include_player_ids' => collect(['player_id_1', 'player_id_2']), ]) ->andReturn($response); @@ -116,14 +116,14 @@ public function it_can_send_a_notification_with_email() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', - 'filters' => collect([['field' => 'email', 'value' => 'test@example.com']]), + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'filters' => collect([['field' => 'email', 'value' => 'test@example.com']]), ]) ->andReturn($response); @@ -132,6 +132,58 @@ public function it_can_send_a_notification_with_email() $this->assertInstanceOf(ResponseInterface::class, $channel_response); } + /** + * @test + */ + public function it_can_send_a_notification_with_included_segments() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'included_segments' => collect(['included segments']), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableIncludedSegments(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + + /** + * @test + */ + public function it_can_send_a_notification_with_excluded_segments() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'excluded_segments' => collect(['excluded segments']), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableExcludedSegments(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + /** @test */ public function it_can_send_a_notification_with_tags() { @@ -140,14 +192,14 @@ public function it_can_send_a_notification_with_tags() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'contents' => ['en' => 'Body'], - 'headings' => ['en' => 'Subject'], - 'url' => 'URL', + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', 'chrome_web_icon' => 'Icon', - 'chrome_icon' => 'Icon', - 'adm_small_icon' => 'Icon', - 'small_icon' => 'Icon', - 'tags' => collect([['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000']]), + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'tags' => collect([['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000']]), ]) ->andReturn($response); @@ -173,9 +225,9 @@ public function it_can_send_a_silent_notification() $this->oneSignal->shouldReceive('sendNotificationCustom') ->once() ->with([ - 'content_available' => 1, - 'data.action' => 'reload', - 'data.target' => 'inbox', + 'content_available' => 1, + 'data.action' => 'reload', + 'data.target' => 'inbox', 'include_player_ids' => collect('player_id'), ]) ->andReturn($response); diff --git a/tests/NotifiableExcludedSegments.php b/tests/NotifiableExcludedSegments.php new file mode 100644 index 0000000..0ef435c --- /dev/null +++ b/tests/NotifiableExcludedSegments.php @@ -0,0 +1,16 @@ + ['excluded segments']]; + } +} diff --git a/tests/NotifiableIncludedSegments.php b/tests/NotifiableIncludedSegments.php new file mode 100644 index 0000000..4235ffb --- /dev/null +++ b/tests/NotifiableIncludedSegments.php @@ -0,0 +1,16 @@ + ['included segments']]; + } +} From 83c1c00c9c77008f2893ec9a48e5abb8144e03b2 Mon Sep 17 00:00:00 2001 From: Agboola Sherriff Date: Thu, 16 Aug 2018 09:09:27 +0100 Subject: [PATCH 18/21] ability of user to send notification , filtering for multiple tags (#73) * ability of user to send notification , filtering for multiple tags * added linting * Apply fixes from StyleCI --- src/OneSignalPayloadFactory.php | 9 ++++++++- tests/ChannelTest.php | 27 +++++++++++++++++++++++++++ tests/NotifiableMultipleTags.php | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/NotifiableMultipleTags.php diff --git a/src/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index 5d4b47b..f44a1ce 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -22,11 +22,18 @@ public static function make($notifiable, Notification $notification, $targeting) if (static::isTargetingEmail($targeting)) { $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); } elseif (static::isTargetingTags($targeting)) { - $payload['tags'] = collect([$targeting['tags']]); + $array = $targeting['tags']; + $res = count($array) == count($array, COUNT_RECURSIVE); + if ($res) { + $payload['tags'] = collect([$targeting['tags']]); + } else { + $payload['tags'] = collect($targeting['tags']); + } } elseif (static::isTargetingIncludedSegments($targeting)) { $payload['included_segments'] = collect($targeting['included_segments']); } elseif (static::isTargetingExcludedSegments($targeting)) { $payload['excluded_segments'] = collect($targeting['excluded_segments']); + } else { $payload['include_player_ids'] = collect($targeting); } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 00e49e4..ada7e4c 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -208,6 +208,33 @@ public function it_can_send_a_notification_with_tags() $this->assertInstanceOf(ResponseInterface::class, $channel_response); } + /** @test */ + public function it_can_send_a_notification_with_multiple_tags() + { + $response = new Response(200); + + $this->oneSignal->shouldReceive('sendNotificationCustom') + ->once() + ->with([ + 'contents' => ['en' => 'Body'], + 'headings' => ['en' => 'Subject'], + 'url' => 'URL', + 'chrome_web_icon' => 'Icon', + 'chrome_icon' => 'Icon', + 'adm_small_icon' => 'Icon', + 'small_icon' => 'Icon', + 'tags' => collect([ + ['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000'], + ['key' => 'role', 'relation' => '=', 'value' => 'admin'], + ]), + ]) + ->andReturn($response); + + $channel_response = $this->channel->send(new NotifiableMultipleTags(), new TestNotification()); + + $this->assertInstanceOf(ResponseInterface::class, $channel_response); + } + /** @test */ public function it_sends_nothing_and_returns_null_when_player_id_empty() { diff --git a/tests/NotifiableMultipleTags.php b/tests/NotifiableMultipleTags.php new file mode 100644 index 0000000..5779314 --- /dev/null +++ b/tests/NotifiableMultipleTags.php @@ -0,0 +1,20 @@ + [ + ['key' => 'device_uuid', 'relation' => '=', 'value' => '123e4567-e89b-12d3-a456-426655440000'], + ['key' => 'role', 'relation' => '=', 'value' => 'admin'], + ], + ]; + } +} From 7d7edcb36fa72904a6b518b77b5b0584932da44b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 10:13:05 +0200 Subject: [PATCH 19/21] Pass Notification Object to Route --- src/OneSignalChannel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OneSignalChannel.php b/src/OneSignalChannel.php index 8453430..9622af7 100644 --- a/src/OneSignalChannel.php +++ b/src/OneSignalChannel.php @@ -28,7 +28,7 @@ public function __construct(OneSignalClient $oneSignal) */ public function send($notifiable, Notification $notification) { - if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) { + if (! $userIds = $notifiable->routeNotificationFor('OneSignal', $notification)) { return; } From 5431c235e55adb796f53accfc3207ed5ab1ff84e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 10:14:05 +0200 Subject: [PATCH 20/21] Fix Style CI Issue --- src/OneSignalPayloadFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index f44a1ce..ac51889 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -23,7 +23,7 @@ public static function make($notifiable, Notification $notification, $targeting) $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); } elseif (static::isTargetingTags($targeting)) { $array = $targeting['tags']; - $res = count($array) == count($array, COUNT_RECURSIVE); + $res = count($array) == count($array, COUNT_RECURSIVE); if ($res) { $payload['tags'] = collect([$targeting['tags']]); } else { From d5a94abf79c2eb0e90466e14304d5205aea3f654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 16 Aug 2018 10:16:23 +0200 Subject: [PATCH 21/21] Fix Style CI Issue --- src/OneSignalPayloadFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OneSignalPayloadFactory.php b/src/OneSignalPayloadFactory.php index ac51889..6367b8e 100644 --- a/src/OneSignalPayloadFactory.php +++ b/src/OneSignalPayloadFactory.php @@ -33,7 +33,6 @@ public static function make($notifiable, Notification $notification, $targeting) $payload['included_segments'] = collect($targeting['included_segments']); } elseif (static::isTargetingExcludedSegments($targeting)) { $payload['excluded_segments'] = collect($targeting['excluded_segments']); - } else { $payload['include_player_ids'] = collect($targeting); }