WebPushNotification.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\WebPush;
  3. use Exception;
  4. class WebPushNotification
  5. {
  6. private $title;
  7. private $body;
  8. private $icon;
  9. private $image;
  10. private $lang;
  11. private $tag;
  12. private $badge;
  13. private $dir;
  14. private $vibrate;
  15. private $renotify;
  16. private $require_interaction;
  17. private $silent;
  18. private $timestamp;
  19. private $actions;
  20. private $fields;
  21. public function __construct()
  22. {
  23. $this->actions = array();
  24. $this->fields = array();
  25. }
  26. public function title($value)
  27. {
  28. $this->title = $value;
  29. }
  30. public function body($value)
  31. {
  32. $this->body = $value;
  33. }
  34. public function icon($value)
  35. {
  36. $this->icon = $value;
  37. }
  38. public function image($value)
  39. {
  40. $this->image = $value;
  41. }
  42. public function lang($value)
  43. {
  44. $this->lang = $value;
  45. }
  46. public function tag($value)
  47. {
  48. $this->tag = $value;
  49. }
  50. public function badge($value)
  51. {
  52. $this->badge = $value;
  53. }
  54. public function dir($value)
  55. {
  56. $this->dir = $value;
  57. }
  58. public function vibrate($value)
  59. {
  60. $this->vibrate = $value;
  61. }
  62. public function renotify($value)
  63. {
  64. $this->renotify = $value;
  65. }
  66. public function require_interaction($value)
  67. {
  68. $this->require_interaction = $value;
  69. }
  70. public function silent($value)
  71. {
  72. $this->silent = $value;
  73. }
  74. public function timestamp($value)
  75. {
  76. $this->timestamp = $value;
  77. }
  78. public function actions($value)
  79. {
  80. $this->actions = $value;
  81. }
  82. public function getFields()
  83. {
  84. return $this->fields;
  85. }
  86. public function buildFields()
  87. {
  88. try {
  89. $this->check_parameter();
  90. } catch (Exception $e) {
  91. echo $e;
  92. }
  93. $keys = array(
  94. 'title',
  95. 'body',
  96. 'icon',
  97. 'image',
  98. 'lang',
  99. 'tag',
  100. 'badge',
  101. 'dir',
  102. 'vibrate',
  103. 'renotify',
  104. 'require_interaction',
  105. 'silent',
  106. 'timestamp',
  107. 'actions'
  108. );
  109. foreach ($keys as $key) {
  110. if (isset($this->$key)) {
  111. $this->fields[$key] = $this->$key;
  112. }
  113. }
  114. }
  115. private function check_parameter()
  116. {
  117. if (($this->renotify) && (gettype($this->renotify) != "boolean")) {
  118. throw new Exception("type of renotify is wrong.");
  119. }
  120. if (($this->require_interaction) && (gettype($this->require_interaction) != "boolean")) {
  121. throw new Exception("type of requireInteraction is wrong.");
  122. }
  123. if (($this->silent) && (gettype($this->silent) != "boolean")) {
  124. throw new Exception("type of silent is wrong.");
  125. }
  126. }
  127. }