AndroidNotification.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
  3. use App\Libs\Push\HuaWei\Admin\Constants;
  4. use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
  5. use App\Libs\Push\HuaWei\Admin\NotificationPriority;
  6. use App\Libs\Push\HuaWei\Admin\PushLogConfig;
  7. use App\Libs\Push\HuaWei\Admin\Visibility;
  8. use Exception;
  9. class AndroidNotification
  10. {
  11. private $title;
  12. private $body;
  13. private $icon;
  14. private $color;
  15. private $sound;
  16. private $tag;
  17. private $click_action;
  18. private $body_loc_key;
  19. private $body_loc_args;
  20. private $title_loc_key;
  21. private $title_loc_args;
  22. private $channel_id;
  23. private $notify_summary;
  24. private $image;
  25. private $notify_icon;
  26. /**
  27. * notification style:
  28. * 0:default
  29. * 1:big text
  30. * 2:big photo
  31. */
  32. private $style;
  33. private $big_title;
  34. private $big_body;
  35. private $auto_clear;
  36. private $notify_id;
  37. private $group;
  38. private $badge;
  39. private $ticker;
  40. private $auto_cancel;
  41. private $when;
  42. private $importance;
  43. private $use_default_vibrate;
  44. private $use_default_light;
  45. private $vibrate_config;
  46. private $visibility;
  47. private $light_settings;
  48. private $foreground_show;
  49. private $fields;
  50. public function __construct()
  51. {
  52. $this->click_action = array();
  53. $this->body_loc_args = array();
  54. $this->title_loc_args = array();
  55. $this->badge = array();
  56. $this->light_settings = array();
  57. $this->foreground_show = true;
  58. $this->fields = array();
  59. }
  60. public function title($value)
  61. {
  62. $this->title = $value;
  63. }
  64. public function body($value)
  65. {
  66. $this->body = $value;
  67. }
  68. public function icon($value)
  69. {
  70. $this->icon = $value;
  71. }
  72. public function color($value)
  73. {
  74. $this->color = $value;
  75. }
  76. public function sound($value)
  77. {
  78. $this->sound = $value;
  79. }
  80. public function tag($value)
  81. {
  82. $this->tag = $value;
  83. }
  84. public function click_action($value)
  85. {
  86. $this->click_action = $value;
  87. }
  88. public function body_loc_key($value)
  89. {
  90. $this->body_loc_key = $value;
  91. }
  92. public function body_loc_args($value)
  93. {
  94. $this->body_loc_args = $value;
  95. }
  96. public function title_loc_key($value)
  97. {
  98. $this->title_loc_key = $value;
  99. }
  100. public function title_loc_args($value)
  101. {
  102. $this->title_loc_args = $value;
  103. }
  104. public function channel_id($value)
  105. {
  106. $this->channel_id = $value;
  107. }
  108. public function notify_summary($value)
  109. {
  110. $this->notify_summary = $value;
  111. }
  112. // added
  113. public function image($value)
  114. {
  115. $this->image = $value;
  116. }
  117. public function notify_icon($value)
  118. {
  119. $this->notify_icon = $value;
  120. }
  121. public function style($value)
  122. {
  123. $this->style = $value;
  124. }
  125. public function big_title($value)
  126. {
  127. $this->big_title = $value;
  128. }
  129. public function big_body($value)
  130. {
  131. $this->big_body = $value;
  132. }
  133. public function auto_clear($value)
  134. {
  135. $this->auto_clear = $value;
  136. }
  137. public function notify_id($value)
  138. {
  139. $this->notify_id = $value;
  140. }
  141. public function group($value)
  142. {
  143. $this->group = $value;
  144. }
  145. public function badge($value)
  146. {
  147. $this->badge = $value;
  148. }
  149. public function ticker($value)
  150. {
  151. $this->ticker = $value;
  152. }
  153. public function auto_cancel($value)
  154. {
  155. $this->auto_cancel = $value;
  156. }
  157. public function when($value)
  158. {
  159. $this->when = $value;
  160. }
  161. public function importance($value)
  162. {
  163. $this->importance = $value;
  164. }
  165. public function use_default_vibrate($value)
  166. {
  167. $this->use_default_vibrate = $value;
  168. }
  169. public function use_default_light($value)
  170. {
  171. $this->use_default_light = $value;
  172. }
  173. public function vibrate_config($value)
  174. {
  175. $this->vibrate_config = $value;
  176. }
  177. public function visibility($value)
  178. {
  179. $this->visibility = $value;
  180. }
  181. public function light_settings($value)
  182. {
  183. $this->light_settings = $value;
  184. }
  185. public function foreground_show($value)
  186. {
  187. $this->foreground_show = $value;
  188. }
  189. public function getFields()
  190. {
  191. return $this->fields;
  192. }
  193. public function buildFields()
  194. {
  195. try {
  196. $this->check_parameter();
  197. } catch (Exception $e) {
  198. PushLogConfig::getSingleInstance()->LogMessage($e, Constants::HW_PUSH_LOG_ERROR_LEVEL);
  199. return;
  200. }
  201. $keys = array(
  202. 'title',
  203. 'body',
  204. 'image',
  205. 'icon',
  206. 'color',
  207. 'sound',
  208. 'tag',
  209. 'click_action',
  210. 'body_loc_key',
  211. 'body_loc_args',
  212. 'title_loc_key',
  213. 'title_loc_args',
  214. 'channel_id',
  215. 'notify_summary',
  216. 'notify_icon',
  217. 'style',
  218. 'big_title',
  219. 'big_body',
  220. 'auto_clear',
  221. 'notify_id',
  222. 'group',
  223. 'badge',
  224. 'ticker',
  225. 'auto_cancel',
  226. 'when',
  227. 'importance',
  228. 'use_default_vibrate',
  229. 'use_default_light',
  230. 'vibrate_config',
  231. 'visibility',
  232. 'light_settings',
  233. 'foreground_show'
  234. );
  235. foreach ($keys as $key) {
  236. if (isset($this->$key)) {
  237. $this->fields[$key] = $this->$key;
  238. }
  239. }
  240. }
  241. private function check_parameter()
  242. {
  243. if (empty($this->title)) {
  244. throw new Exception("title should be set");
  245. }
  246. if (empty($this->body)) {
  247. throw new Exception("body should be set");
  248. }
  249. if (!empty($this->color)) {
  250. if (FALSE == ValidatorUtil::validatePattern(Constants::COLOR_PATTERN, $this->color)) {
  251. throw new Exception("Wrong color format, color must be in the form #RRGGBB");
  252. }
  253. }
  254. if (!empty($this->body_loc_args) && empty($this->body_loc_key)) {
  255. throw new Exception("bodyLocKey is required when specifying bodyLocArgs");
  256. }
  257. if (!empty($this->title_loc_args) && empty($this->title_loc_key)) {
  258. throw new Exception("titleLocKey is required when specifying titleLocArgs");
  259. }
  260. if (!empty($this->image)) {
  261. if (FALSE == ValidatorUtil::validatePattern(Constants::URL_PATTERN, $this->image)) {
  262. throw new Exception("notifyIcon must start with https");
  263. }
  264. }
  265. if (($this->style) && (gettype($this->style) != "integer")) {
  266. throw new Exception("type of style is wrong.");
  267. }
  268. if (!in_array($this->style, array(
  269. 0,
  270. 1
  271. ))) {
  272. throw new Exception("style should be one of 0:default, 1: big text");
  273. }
  274. // importance
  275. if (!empty($this->importance) && !in_array($this->importance, array(
  276. NotificationPriority::NOTIFICATION_PRIORITY_LOW,
  277. NotificationPriority::NOTIFICATION_PRIORITY_NORMAL,
  278. NotificationPriority::NOTIFICATION_PRIORITY_HIGH,
  279. ))) {
  280. throw new Exception("notification priority shouid be [LOW,NORMAL,HIGH]");
  281. }
  282. // vibrate_config
  283. if (!empty($this->vibrate_config)) {
  284. foreach ($this->vibrate_config as $key) {
  285. if (FALSE == ValidatorUtil::validatePattern(Constants::VIBRATE_PATTERN, $key)) {
  286. throw new Exception("Wrong vibrate timing format");
  287. }
  288. }
  289. }
  290. // visibility
  291. if (!empty($this->visibility) && !in_array($this->visibility, array(
  292. Visibility::VISIBILITY_UNSPECIFIED,
  293. Visibility::PRIVATE_STR,
  294. Visibility::PUBLIC_STR,
  295. Visibility::SECRET_STR
  296. ))) {
  297. throw new Exception("visibility shouid be [VISIBILITY_UNSPECIFIED, PRIVATE, PUBLIC, SECRET]");
  298. }
  299. // auto_clear
  300. if (($this->auto_clear) && (gettype($this->auto_clear) != "integer")) {
  301. throw new Exception("type of auto_clear is wrong.");
  302. }
  303. // notify_id
  304. if (($this->notify_id) && (gettype($this->notify_id) != "integer")) {
  305. throw new Exception("type of notify_id is wrong.");
  306. }
  307. }
  308. }