123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
- use App\Libs\Push\HuaWei\Admin\Constants;
- use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
- use App\Libs\Push\HuaWei\Admin\NotificationPriority;
- use App\Libs\Push\HuaWei\Admin\PushLogConfig;
- use App\Libs\Push\HuaWei\Admin\Visibility;
- use Exception;
- class AndroidNotification
- {
- private $title;
- private $body;
- private $icon;
- private $color;
- private $sound;
- private $tag;
- private $click_action;
- private $body_loc_key;
- private $body_loc_args;
- private $title_loc_key;
- private $title_loc_args;
- private $channel_id;
- private $notify_summary;
- private $image;
- private $notify_icon;
- /**
- * notification style:
- * 0:default
- * 1:big text
- * 2:big photo
- */
- private $style;
- private $big_title;
- private $big_body;
- private $auto_clear;
- private $notify_id;
- private $group;
- private $badge;
- private $ticker;
- private $auto_cancel;
- private $when;
- private $importance;
- private $use_default_vibrate;
- private $use_default_light;
- private $vibrate_config;
- private $visibility;
- private $light_settings;
- private $foreground_show;
- private $fields;
- public function __construct()
- {
- $this->click_action = array();
- $this->body_loc_args = array();
- $this->title_loc_args = array();
- $this->badge = array();
- $this->light_settings = array();
- $this->foreground_show = true;
- $this->fields = array();
- }
- public function title($value)
- {
- $this->title = $value;
- }
- public function body($value)
- {
- $this->body = $value;
- }
- public function icon($value)
- {
- $this->icon = $value;
- }
- public function color($value)
- {
- $this->color = $value;
- }
- public function sound($value)
- {
- $this->sound = $value;
- }
- public function tag($value)
- {
- $this->tag = $value;
- }
- public function click_action($value)
- {
- $this->click_action = $value;
- }
- public function body_loc_key($value)
- {
- $this->body_loc_key = $value;
- }
- public function body_loc_args($value)
- {
- $this->body_loc_args = $value;
- }
- public function title_loc_key($value)
- {
- $this->title_loc_key = $value;
- }
- public function title_loc_args($value)
- {
- $this->title_loc_args = $value;
- }
- public function channel_id($value)
- {
- $this->channel_id = $value;
- }
- public function notify_summary($value)
- {
- $this->notify_summary = $value;
- }
- // added
- public function image($value)
- {
- $this->image = $value;
- }
- public function notify_icon($value)
- {
- $this->notify_icon = $value;
- }
- public function style($value)
- {
- $this->style = $value;
- }
- public function big_title($value)
- {
- $this->big_title = $value;
- }
- public function big_body($value)
- {
- $this->big_body = $value;
- }
- public function auto_clear($value)
- {
- $this->auto_clear = $value;
- }
- public function notify_id($value)
- {
- $this->notify_id = $value;
- }
- public function group($value)
- {
- $this->group = $value;
- }
- public function badge($value)
- {
- $this->badge = $value;
- }
- public function ticker($value)
- {
- $this->ticker = $value;
- }
- public function auto_cancel($value)
- {
- $this->auto_cancel = $value;
- }
- public function when($value)
- {
- $this->when = $value;
- }
- public function importance($value)
- {
- $this->importance = $value;
- }
- public function use_default_vibrate($value)
- {
- $this->use_default_vibrate = $value;
- }
- public function use_default_light($value)
- {
- $this->use_default_light = $value;
- }
- public function vibrate_config($value)
- {
- $this->vibrate_config = $value;
- }
- public function visibility($value)
- {
- $this->visibility = $value;
- }
- public function light_settings($value)
- {
- $this->light_settings = $value;
- }
- public function foreground_show($value)
- {
- $this->foreground_show = $value;
- }
- public function getFields()
- {
- return $this->fields;
- }
- public function buildFields()
- {
- try {
- $this->check_parameter();
- } catch (Exception $e) {
- PushLogConfig::getSingleInstance()->LogMessage($e, Constants::HW_PUSH_LOG_ERROR_LEVEL);
- return;
- }
- $keys = array(
- 'title',
- 'body',
- 'image',
- 'icon',
- 'color',
- 'sound',
- 'tag',
- 'click_action',
- 'body_loc_key',
- 'body_loc_args',
- 'title_loc_key',
- 'title_loc_args',
- 'channel_id',
- 'notify_summary',
- 'notify_icon',
- 'style',
- 'big_title',
- 'big_body',
- 'auto_clear',
- 'notify_id',
- 'group',
- 'badge',
- 'ticker',
- 'auto_cancel',
- 'when',
- 'importance',
- 'use_default_vibrate',
- 'use_default_light',
- 'vibrate_config',
- 'visibility',
- 'light_settings',
- 'foreground_show'
- );
- foreach ($keys as $key) {
- if (isset($this->$key)) {
- $this->fields[$key] = $this->$key;
- }
- }
- }
- private function check_parameter()
- {
- if (empty($this->title)) {
- throw new Exception("title should be set");
- }
- if (empty($this->body)) {
- throw new Exception("body should be set");
- }
- if (!empty($this->color)) {
- if (FALSE == ValidatorUtil::validatePattern(Constants::COLOR_PATTERN, $this->color)) {
- throw new Exception("Wrong color format, color must be in the form #RRGGBB");
- }
- }
- if (!empty($this->body_loc_args) && empty($this->body_loc_key)) {
- throw new Exception("bodyLocKey is required when specifying bodyLocArgs");
- }
- if (!empty($this->title_loc_args) && empty($this->title_loc_key)) {
- throw new Exception("titleLocKey is required when specifying titleLocArgs");
- }
- if (!empty($this->image)) {
- if (FALSE == ValidatorUtil::validatePattern(Constants::URL_PATTERN, $this->image)) {
- throw new Exception("notifyIcon must start with https");
- }
- }
- if (($this->style) && (gettype($this->style) != "integer")) {
- throw new Exception("type of style is wrong.");
- }
- if (!in_array($this->style, array(
- 0,
- 1
- ))) {
- throw new Exception("style should be one of 0:default, 1: big text");
- }
- // importance
- if (!empty($this->importance) && !in_array($this->importance, array(
- NotificationPriority::NOTIFICATION_PRIORITY_LOW,
- NotificationPriority::NOTIFICATION_PRIORITY_NORMAL,
- NotificationPriority::NOTIFICATION_PRIORITY_HIGH,
- ))) {
- throw new Exception("notification priority shouid be [LOW,NORMAL,HIGH]");
- }
- // vibrate_config
- if (!empty($this->vibrate_config)) {
- foreach ($this->vibrate_config as $key) {
- if (FALSE == ValidatorUtil::validatePattern(Constants::VIBRATE_PATTERN, $key)) {
- throw new Exception("Wrong vibrate timing format");
- }
- }
- }
- // visibility
- if (!empty($this->visibility) && !in_array($this->visibility, array(
- Visibility::VISIBILITY_UNSPECIFIED,
- Visibility::PRIVATE_STR,
- Visibility::PUBLIC_STR,
- Visibility::SECRET_STR
- ))) {
- throw new Exception("visibility shouid be [VISIBILITY_UNSPECIFIED, PRIVATE, PUBLIC, SECRET]");
- }
- // auto_clear
- if (($this->auto_clear) && (gettype($this->auto_clear) != "integer")) {
- throw new Exception("type of auto_clear is wrong.");
- }
- // notify_id
- if (($this->notify_id) && (gettype($this->notify_id) != "integer")) {
- throw new Exception("type of notify_id is wrong.");
- }
- }
- }
|