diff --git a/src/Helper/ArrayHelper.php b/src/Helper/ArrayHelper.php new file mode 100644 index 0000000..12c7416 --- /dev/null +++ b/src/Helper/ArrayHelper.php @@ -0,0 +1,15 @@ + $this->collapse_state, 'urgency' => self::$urgencyName[$this->urgency] ?? null, 'category' => self::$categoryName[$this->category] ?? null, 'ttl' => "{$this->timeToLive}s", 'bi_tag' => $this->tags, 'fast_app_target' => $this->staging ? 1 : 2, - 'data' => $this->data, + 'data' => $this->data ? json_encode($this->data) : null, 'notification' => $this->notification ? $this->notification->toArray() : null, - ]; + ]); } public function validate() diff --git a/src/Structure/Android/Notification.php b/src/Structure/Android/Notification.php index 1fe4f09..aefd6b0 100644 --- a/src/Structure/Android/Notification.php +++ b/src/Structure/Android/Notification.php @@ -4,6 +4,7 @@ namespace Afiqiqmal\HuaweiPush\Structure\Android; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; use Afiqiqmal\HuaweiPush\Structure\Common\Badge; use Afiqiqmal\HuaweiPush\Structure\Common\Button; use Afiqiqmal\HuaweiPush\Structure\Common\ClickAction; @@ -689,7 +690,7 @@ public function setClickAction(ClickAction $click_action) public function toArray() { - return [ + return ArrayHelper::filter([ 'title' => $this->title, 'body' => $this->body, 'icon' => $this->icon, @@ -727,7 +728,7 @@ public function toArray() 'buttons' => collect($this->buttons)->map(function ($item) { return $item->toArray(); })->toArray() - ]; + ]); } public function validate() diff --git a/src/Structure/Common/Button.php b/src/Structure/Common/Button.php index 546b6e8..700bcf2 100644 --- a/src/Structure/Common/Button.php +++ b/src/Structure/Common/Button.php @@ -4,6 +4,7 @@ namespace Afiqiqmal\HuaweiPush\Structure\Common; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; use Afiqiqmal\HuaweiPush\Structure\Validation\Extras; class Button implements Extras @@ -25,7 +26,7 @@ class Button implements Extras * * @var integer */ - private $action_type; + private $action_type = 0; /** * Method of opening a custom app page. The options are as follows: @@ -120,13 +121,13 @@ public function toArray() { $this->validate(); - return [ + return ArrayHelper::filter([ 'name' => $this->name, 'action_type' => $this->action_type, 'intent_type' => $this->intent_type, 'intent' => $this->intent, - 'data' => $this->data, - ]; + 'data' => $this->data ? json_encode($this->data) : null, + ]); } public function validate() diff --git a/src/Structure/Common/LightSetting.php b/src/Structure/Common/LightSetting.php index 5bb370c..17792e7 100644 --- a/src/Structure/Common/LightSetting.php +++ b/src/Structure/Common/LightSetting.php @@ -4,6 +4,7 @@ namespace Afiqiqmal\HuaweiPush\Structure\Common; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; use Afiqiqmal\HuaweiPush\Structure\Validation\Extras; class LightSetting implements Extras @@ -77,11 +78,11 @@ public function toArray() { $this->validate(); - return [ + return ArrayHelper::filter([ 'color' => $this->color, 'light_on_duration' => $this->light_on_duration, 'light_off_duration' => $this->light_off_duration - ]; + ]); } public function validate() diff --git a/src/Structure/Message.php b/src/Structure/Message.php index 0e25a5d..25d862d 100644 --- a/src/Structure/Message.php +++ b/src/Structure/Message.php @@ -4,6 +4,8 @@ namespace Afiqiqmal\HuaweiPush\Structure; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; + class Message { /** @@ -130,13 +132,13 @@ public function setCondition(string $condition) public function toArray() { - return [ - 'data' => $this->data ?? null, + return ArrayHelper::filter([ + 'data' => $this->data ? json_encode($this->data) : null, 'notification' => $this->notification ? $this->notification->toArray() : null, 'android' => $this->android ? $this->android->toArray() : null, 'token' => $this->targetDeviceIds, 'condition' => $this->condition, 'topic' => $this->topic, - ]; + ]); } } \ No newline at end of file diff --git a/src/Structure/Notification.php b/src/Structure/Notification.php index bb9606f..f835505 100644 --- a/src/Structure/Notification.php +++ b/src/Structure/Notification.php @@ -4,6 +4,8 @@ namespace Afiqiqmal\HuaweiPush\Structure; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; + class Notification { /** @@ -74,10 +76,10 @@ public function setImage(string $image) public function toArray() { - return [ + return ArrayHelper::filter([ 'title' => $this->title, 'body' => $this->body, 'image' => $this->image, - ]; + ]); } } \ No newline at end of file diff --git a/src/Structure/NotificationPayload.php b/src/Structure/NotificationPayload.php index 6fbfea2..e6287a7 100644 --- a/src/Structure/NotificationPayload.php +++ b/src/Structure/NotificationPayload.php @@ -4,6 +4,8 @@ namespace Afiqiqmal\HuaweiPush\Structure; +use Afiqiqmal\HuaweiPush\Helper\ArrayHelper; + class NotificationPayload { /** @@ -46,9 +48,9 @@ public function setMessage(Message $message): NotificationPayload public function toArray() { - return [ + return ArrayHelper::filter([ 'validate_only' => $this->validate_only, 'message' => $this->message->toArray() ?? null - ]; + ]); } } \ No newline at end of file