Builder.php 2.6 KB

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