Sin descripción

Sender.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * MiPush消息发送类.
  4. * @author wangkuiwei
  5. * @name Sender
  6. * @desc MiPush消息发送
  7. *
  8. */
  9. namespace xmpush;
  10. class Sender extends HttpBase {
  11. public function __construct() {
  12. parent::__construct();
  13. }
  14. //指定regId单发消息
  15. public function send(Message $message, $regId, $retries = 1) {
  16. $fields = $message->getFields();
  17. $fields['registration_id'] = $regId;
  18. return $this->postResult(PushRequestPath::V3_REGID_MESSAGE(), $fields, $retries);
  19. }
  20. //指定regId列表群发
  21. public function sendToIds(Message $message, $regIdList, $retries = 1) {
  22. $fields = $message->getFields();
  23. $jointRegIds = '';
  24. foreach ($regIdList as $regId) {
  25. if (isset($regId)) {
  26. $jointRegIds .= $regId . Constants::$comma;
  27. }
  28. }
  29. $fields['registration_id'] = $jointRegIds;
  30. return $this->postResult(PushRequestPath::V3_REGID_MESSAGE(), $fields, $retries);
  31. }
  32. //多条发送
  33. public function multiSend($targetMessages, $type, $retries = 1) {
  34. $requestPath = $this->multiSendRequestPath($type);
  35. $data = array();
  36. foreach ($targetMessages as $targetMsg) {
  37. array_push($data, $targetMsg->getFields());
  38. }
  39. $fields = array('messages' => json_encode($data));
  40. return $this->postResult($requestPath, $fields, $retries);
  41. }
  42. //多条发送
  43. public function multiSendAtTime($targetMessages, $type, $timeToSend, $retries = 1) {
  44. $requestPath = $this->multiSendRequestPath($type);
  45. $data = array();
  46. foreach ($targetMessages as $targetMsg) {
  47. array_push($data, $targetMsg->getFields());
  48. }
  49. $fields = array('messages' => json_encode($data), 'time_to_send' => $timeToSend);
  50. return $this->postResult($requestPath, $fields, $retries);
  51. }
  52. //指定别名单发
  53. public function sendToAlias(Message $message, $alias, $retries = 1) {
  54. $fields = $message->getFields();
  55. $fields['alias'] = $alias;
  56. return $this->postResult(PushRequestPath::V3_ALIAS_MESSAGE(), $fields, $retries);
  57. }
  58. //指定别名列表群发
  59. public function sendToAliases(Message $message, $aliasList, $retries = 1) {
  60. $fields = $message->getFields();
  61. $jointAliases = '';
  62. foreach ($aliasList as $alias) {
  63. if (strlen($jointAliases) > 0) {
  64. $jointAliases = $jointAliases . Constants::$comma;
  65. }
  66. $jointAliases = $jointAliases . $alias;
  67. }
  68. $fields['alias'] = $jointAliases;
  69. return $this->postResult(PushRequestPath::V3_ALIAS_MESSAGE(), $fields, $retries);
  70. }
  71. //指定userAccount群发
  72. public function sendToUserAccount(Message $message, $userAccount, $retries = 1) {
  73. $fields = $message->getFields();
  74. $fields['user_account'] = $userAccount;
  75. return $this->postResult(PushRequestPath::V2_USER_ACCOUNT_MESSAGE(), $fields, $retries);
  76. }
  77. //指定userAccount列表群发
  78. public function sendToUserAccounts(Message $message, $userAccountList, $retries = 1) {
  79. $fields = $message->getFields();
  80. $jointUserAccounts = '';
  81. foreach ($userAccountList as $userAccount) {
  82. if (strlen($jointUserAccounts) > 0) {
  83. $jointUserAccounts = $jointUserAccounts . Constants::$comma;
  84. }
  85. $jointUserAccounts = $jointUserAccounts . $userAccount;
  86. }
  87. $fields['user_account'] = $jointUserAccounts;
  88. return $this->postResult(PushRequestPath::V2_USER_ACCOUNT_MESSAGE(), $fields, $retries);
  89. }
  90. //指定topic群发
  91. public function broadcast(Message $message, $topic, $retries = 1) {
  92. $fields = $message->getFields();
  93. $fields['topic'] = $topic;
  94. return $this->postResult(PushRequestPath::V3_BROADCAST(), $fields, $retries);
  95. }
  96. //向所有设备发送消息
  97. public function broadcastAll(Message $message, $retries = 1) {
  98. $fields = $message->getFields();
  99. return $this->postResult(PushRequestPath::V3_BROADCAST_TO_ALL(), $fields, $retries);
  100. }
  101. //广播消息,多个topic,支持topic间的交集、并集或差集
  102. public function multiTopicBroadcast(Message $message, $topicList, $topicOp, $retries = 1) {
  103. if (count($topicList) == 1) {
  104. return $this->broadcast($message, $topicList[0], $retries);
  105. }
  106. $fields = $message->getFields();
  107. $jointTopics = '';
  108. foreach ($topicList as $topic) {
  109. if (strlen($jointTopics) > 0) {
  110. $jointTopics = $jointTopics . Constants::$multi_topic_split;
  111. }
  112. $jointTopics = $jointTopics . $topic;
  113. }
  114. $fields['topics'] = $jointTopics;
  115. $fields['topic_op'] = $topicOp;
  116. return $this->postResult(PushRequestPath::V3_MILTI_TOPIC_BROADCAST(), $fields, $retries);
  117. }
  118. // 检测定时任务是否存在
  119. public function checkScheduleJobExist($msgId, $retries = 1) {
  120. $fields = array('job_id' => $msgId);
  121. return $this->postResult(PushRequestPath::V2_CHECK_SCHEDULE_JOB_EXIST(), $fields, $retries);
  122. }
  123. // 删除定时任务
  124. public function deleteScheduleJob($msgId, $retries = 1) {
  125. $fields = array('job_id' => $msgId);
  126. return $this->postResult(PushRequestPath::V2_DELETE_SCHEDULE_JOB(), $fields, $retries);
  127. }
  128. /**
  129. * @param $type
  130. * @return PushRequestPath
  131. */
  132. private function multiSendRequestPath($type) {
  133. if ($type == TargetedMessage::TARGET_TYPE_ALIAS) {
  134. $requestPath = PushRequestPath::V2_SEND_MULTI_MESSAGE_WITH_ALIAS();
  135. return $requestPath;
  136. } else if ($type == TargetedMessage::TARGET_TYPE_USER_ACCOUNT) {
  137. $requestPath = PushRequestPath::V2_SEND_MULTI_MESSAGE_WITH_ACCOUNT();
  138. return $requestPath;
  139. } else {
  140. $requestPath = PushRequestPath::V2_SEND_MULTI_MESSAGE_WITH_REGID();
  141. return $requestPath;
  142. }
  143. }
  144. }
  145. ?>