IOSBuilder.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * IOS设备的消息体.
  4. * @author wangkuiwei
  5. * @name IOSBuilder
  6. * @desc 构建发送给IOS设备的Message对象。
  7. *
  8. */
  9. namespace App\Libs\Push\XMPush;
  10. class IOSBuilder extends Message {
  11. const soundUrl = 'sound_url';
  12. const badge = 'badge';
  13. protected $apsProperFields; // 用于存储aps的属性,为的是支持新的扩展属性
  14. public function __construct() {
  15. parent::__construct();
  16. $this->apsProperFields = array();
  17. }
  18. public function description($description) {
  19. $this->description = $description;
  20. }
  21. public function timeToLive($ttl) {
  22. $this->time_to_live = $ttl;
  23. }
  24. public function timeToSend($timeToSend) {
  25. $this->time_to_send = $timeToSend;
  26. }
  27. public function soundUrl($url) {
  28. $this->extra(IOSBuilder::soundUrl, $url);
  29. }
  30. public function badge($badge) {
  31. $this->extra(IOSBuilder::badge, $badge);
  32. }
  33. public function contentAvailable($value) {
  34. $this->extra("content-available", $value);
  35. }
  36. public function showContent() {
  37. $this->extra("show-content", "1");
  38. }
  39. public function extra($key, $value) {
  40. $this->extra[$key] = $value;
  41. }
  42. public function title($title) {
  43. $this->apsProperFields["title"] = $title;
  44. }
  45. public function subtitle($subtitle) {
  46. $this->apsProperFields["subtitle"] = $subtitle;
  47. }
  48. public function body($body) {
  49. $this->apsProperFields["body"] = $body;
  50. }
  51. public function mutableContent($mutableContent) {
  52. $this->apsProperFields["mutable-content"] = $mutableContent;
  53. }
  54. public function apsProperFields($key, $value) {
  55. $this->apsProperFields[$key] = $value;
  56. }
  57. public function build() {
  58. $keys = array(
  59. 'description', 'time_to_live', 'time_to_send'
  60. );
  61. foreach ($keys as $key) {
  62. if (isset($this->$key)) {
  63. $this->fields[$key] = $this->$key;
  64. $this->json_infos[$key] = $this->$key;
  65. }
  66. }
  67. //单独处理extra
  68. $JsonExtra = array();
  69. if (count($this->extra) > 0) {
  70. foreach ($this->extra as $extraKey => $extraValue) {
  71. $this->fields[Message::EXTRA_PREFIX . $extraKey] = $extraValue;
  72. $JsonExtra[$extraKey] = $extraValue;
  73. }
  74. }
  75. $this->json_infos['extra'] = $JsonExtra;
  76. // 单独处理apsProperFields
  77. if (count($this->apsProperFields) > 0) {
  78. foreach ($this->apsProperFields as $key => $value) {
  79. $this->fields[Message::APS_PROPER_FIELDS_PREFIX . $key] = $value;
  80. }
  81. }
  82. }
  83. }
  84. ?>