Push.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Message\Lib;
  3. use Dever;
  4. Dever::apply('sdk/xg', 'message');
  5. class Push
  6. {
  7. private $prefix = 'dever_';
  8. public function send($id, $uid, $name, $content, $param)
  9. {
  10. $this->config = Dever::db('message/push')->one($id);
  11. if ($this->config) {
  12. # 现在就是腾讯信鸽
  13. if ($this->config['type'] == 1) {
  14. $total = count($uid);
  15. $num = 10;
  16. if ($total <= 0) {
  17. # 发全部
  18. $result = $this->sendAll($name, $content, $param);
  19. } elseif ($total == 1) {
  20. # 发单条
  21. $result = $this->sendOne($uid[0], $name, $content, $param);
  22. } elseif ($total <= $num) {
  23. # 发多条
  24. $result = $this->sendList($uid, $name, $content, $param);
  25. } else {
  26. # 批量发送
  27. $result = $this->sendMul($uid, $name, $content, $param);
  28. }
  29. return $result;
  30. }
  31. }
  32. return false;
  33. }
  34. private function sendAction($source_type, $type, $account, $name, $content, $param)
  35. {
  36. $result = array();
  37. $method = $source_type . '_message';
  38. $message = $this->$method($name, $content, $param);
  39. $push = $this->get($source_type);
  40. if ($type == 1) {
  41. $method = 'PushAllDevices';
  42. $send = array(0, $message);
  43. } elseif ($type == 2) {
  44. $method = 'PushSingleAccount';
  45. $send = array(0, $account, $message);
  46. } elseif ($type == 3) {
  47. $method = 'PushAccountList';
  48. $send = array(0, $account, $message);
  49. }
  50. if ($source_type == 'ios') {
  51. $send[] = \XingeApp::IOSENV_DEV;
  52. }
  53. $result[$source_type]['send'] = $send;
  54. $result[$source_type]['message'] = array($name, $content, $param);
  55. $result[$source_type]['result'] = call_user_func_array(array($push, $method), $send);
  56. $log = Dever::json_encode($result[$source_type]);
  57. Dever::log($log, 'message_app_push');
  58. return $result;
  59. }
  60. private function sendMul($account, $name, $content, $param)
  61. {
  62. return $this->sendList($account, $name, $content, $param);
  63. # 大批量发送
  64. $result = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
  65. if (!($result['ret_code'] === 0)) {
  66. } else {
  67. $page = intval($total/$num)+1;
  68. for ($i = 0; $i <= $page; $i++) {
  69. $array = array();
  70. for($j = 0; $j <= $i+$num; $j++) {
  71. $k = $i + $j;
  72. if (isset($uid[$k])) {
  73. $array[$k] = $prefix . $uid[$k];
  74. }
  75. }
  76. array_push($result, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
  77. }
  78. }
  79. }
  80. private function sendList($account, $name, $content, $param)
  81. {
  82. $result = array();
  83. list($android, $ios) = $this->getUser($account);
  84. if ($android) {
  85. $result += $this->sendAction('android', 1, $android, $name, $content, $param);
  86. }
  87. if ($ios) {
  88. $result += $this->sendAction('ios', 1, $ios, $name, $content, $param);
  89. }
  90. return $result;
  91. }
  92. private function sendOne($account, $name, $content, $param)
  93. {
  94. $result = array();
  95. $info = Dever::db('passport/app')->one(array('uid' => $account));
  96. //$info['source_type'] = 'ios';
  97. if ($info && $info['source_type']) {
  98. $result = $this->sendAction($info['source_type'], 2, $this->prefix . $account, $name, $content, $param);
  99. } else {
  100. $result = '没有账户信息';
  101. }
  102. return $result;
  103. }
  104. private function sendAll($name, $content, $param)
  105. {
  106. $result = array();
  107. $result += $this->sendAction('android', 1, false, $name, $content, $param);
  108. $result += $this->sendAction('ios', 1, false, $name, $content, $param);
  109. return $result;
  110. }
  111. private function getUser($account)
  112. {
  113. $android = array();
  114. $ios = array();
  115. $info = Dever::db('passport/app')->getAllByUids(array('uid' => $account));
  116. if ($info) {
  117. foreach ($info as $k => $v) {
  118. if ($v['source_type'] == 'android') {
  119. $android[] = $this->prefix . $v['uid'];
  120. } elseif ($v['source_type'] == 'ios') {
  121. $ios[] = $this->prefix . $v['uid'];
  122. }
  123. }
  124. }
  125. return array($android, $ios);
  126. }
  127. private function android_message($name, $content, $param, $type = 1)
  128. {
  129. $message = new \Message();
  130. $message->setExpireTime(86400);
  131. $message->setTitle($name);
  132. $message->setContent($content);
  133. $message->setType($type);
  134. $action = new \ClickAction();
  135. $action->setActionType(\ClickAction::TYPE_ACTIVITY);
  136. $action->setActivity($param);
  137. $message->setAction($action);
  138. return $message;
  139. }
  140. private function ios_message($name, $content, $param, $type = 1)
  141. {
  142. $message = new \MessageIOS();
  143. $message->setExpireTime(86400);
  144. $message->setAlert(array('title' => $name, 'content' => $content));
  145. $message->setBadge(-2);
  146. //$mess->setSound("beep.wav");
  147. $custom = Dever::json_decode($param);
  148. $message->setCustom($custom);
  149. //$message->setRaw($param);
  150. $time = new \TimeInterval(0, 0, 23, 59);
  151. $message->addAcceptTime($time);
  152. return $message;
  153. }
  154. private function get($type = 'android')
  155. {
  156. if (!isset($this->push[$type])) {
  157. $this->push[$type] = new \XingeApp($this->config[$type . '_appid'], $this->config[$type . '_appsecret']);
  158. }
  159. return $this->push[$type];
  160. }
  161. }