Builder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * (android)消息体.
  4. * @author wangkuiwei
  5. * @name Builder
  6. * @desc 构建发送给Android设备的Message对象。
  7. *
  8. */
  9. namespace App\Libs\Push\XMPush;
  10. class Builder extends Message {
  11. const soundUri = 'sound_uri';
  12. const notifyForeground = 'notify_foreground';
  13. const notifyEffect = 'notify_effect';
  14. const intentUri = 'intent_uri';
  15. const webUri = 'web_uri';
  16. const flowControl = 'flow_control';
  17. const callback = 'callback';
  18. const instantNotify = 'instant_notify';
  19. public function __construct() {
  20. $this->notify_id = 0;
  21. $this->notify_type = -1;
  22. $this->payload = '';
  23. $this->restricted_package_name = Constants::$packageName;
  24. parent::__construct();
  25. }
  26. public function payload($payload) {
  27. $this->payload = $payload;
  28. }
  29. public function title($title) {
  30. $this->title = $title;
  31. }
  32. public function description($description) {
  33. $this->description = $description;
  34. }
  35. public function passThrough($passThrough) {
  36. $this->pass_through = $passThrough;
  37. }
  38. public function notifyType($type) {
  39. $this->notify_type = $type;
  40. }
  41. public function restrictedPackageNames($packageNameList) {
  42. $jointPackageNames = '';
  43. foreach ($packageNameList as $packageName) {
  44. if (isset($packageName)) {
  45. $jointPackageNames .= $packageName . Constants::$comma;
  46. }
  47. }
  48. $this->restricted_package_name = $jointPackageNames;
  49. }
  50. public function timeToLive($ttl) {
  51. $this->time_to_live = $ttl;
  52. }
  53. public function timeToSend($timeToSend) {
  54. $this->time_to_send = $timeToSend;
  55. }
  56. public function instantNotify($isInstantNotify) {
  57. if ($isInstantNotify) {
  58. $this->extra(self::instantNotify, "1");
  59. } else {
  60. unset($this->extra[self::instantNotify]);
  61. }
  62. }
  63. public function notifyId($notifyId) {
  64. $this->notify_id = $notifyId;
  65. }
  66. public function hybridPath($value) {
  67. $this->extra[self::HYBRID_PATH] = $value;
  68. }
  69. public function extra($key, $value) {
  70. $this->extra[$key] = $value;
  71. }
  72. public function build() {
  73. $keys = array(
  74. 'payload', 'title', 'description', 'pass_through', 'notify_type',
  75. 'restricted_package_name', 'time_to_live', 'time_to_send', 'notify_id'
  76. );
  77. foreach ($keys as $key) {
  78. if (isset($this->$key)) {
  79. $this->fields[$key] = $this->$key;
  80. $this->json_infos[$key] = $this->$key;
  81. }
  82. }
  83. //单独处理extra
  84. $JsonExtra = array();
  85. if (count($this->extra) > 0) {
  86. foreach ($this->extra as $extraKey => $extraValue) {
  87. $this->fields[Message::EXTRA_PREFIX . $extraKey] = $extraValue;
  88. $JsonExtra[$extraKey] = $extraValue;
  89. }
  90. }
  91. $this->json_infos['extra'] = $JsonExtra;
  92. }
  93. }
  94. ?>