diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 3ba14de..c53201c 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -185,7 +185,7 @@ public function toArray() { $message = [ 'contents' => ['en' => $this->body], - 'headings' => ['en' => $this->subject], + 'headings' => $this->subjectToArray(), 'url' => $this->url, 'buttons' => $this->buttons, 'web_buttons' => $this->webButtons, @@ -205,4 +205,13 @@ public function toArray() return $message; } + + protected function subjectToArray() + { + if ($this->subject === null) { + return []; + } + + return ['en' => $this->subject]; + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 050cf40..5c1ca3d 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -50,6 +50,12 @@ public function it_can_set_the_subject() $this->assertEquals('mySubject', Arr::get($this->message->toArray(), 'headings.en')); } + /** @test */ + public function it_does_not_append_empty_subject_value_when_subject_is_null() + { + $this->assertEquals([], Arr::get($this->message->toArray(), 'headings')); + } + /** @test */ public function it_can_set_the_url() {