Push.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if ($param && is_array($param)) {
  39. if ($source_type == 'ios' && isset($param[0])) {
  40. $param = $param[0];
  41. }
  42. if ($source_type == 'android' && isset($param[1])) {
  43. $param = $param[1];
  44. }
  45. }
  46. $message = $this->$method($name, $content, $param);
  47. $push = $this->get($source_type);
  48. if ($type == 1) {
  49. $method = 'PushAllDevices';
  50. $send = array(0, $message);
  51. } elseif ($type == 2) {
  52. $method = 'PushSingleAccount';
  53. $send = array(0, $account, $message);
  54. } elseif ($type == 3) {
  55. $method = 'PushAccountList';
  56. $send = array(0, $account, $message);
  57. }
  58. if ($source_type == 'ios') {
  59. $send[] = \XingeApp::IOSENV_DEV;
  60. }
  61. $result[$source_type]['send'] = $send;
  62. $result[$source_type]['message'] = array($name, $content, $param);
  63. $result[$source_type]['result'] = call_user_func_array(array($push, $method), $send);
  64. $log = Dever::json_encode($result[$source_type]);
  65. Dever::log($log, 'message_app_push');
  66. return $result;
  67. }
  68. private function sendMul($account, $name, $content, $param)
  69. {
  70. return $this->sendList($account, $name, $content, $param);
  71. # 大批量发送
  72. $result = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
  73. if (!($result['ret_code'] === 0)) {
  74. } else {
  75. $page = intval($total/$num)+1;
  76. for ($i = 0; $i <= $page; $i++) {
  77. $array = array();
  78. for($j = 0; $j <= $i+$num; $j++) {
  79. $k = $i + $j;
  80. if (isset($uid[$k])) {
  81. $array[$k] = $prefix . $uid[$k];
  82. }
  83. }
  84. array_push($result, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
  85. }
  86. }
  87. }
  88. private function sendList($account, $name, $content, $param)
  89. {
  90. $result = array();
  91. list($android, $ios) = $this->getUser($account);
  92. if ($android) {
  93. $result += $this->sendAction('android', 1, $android, $name, $content, $param);
  94. }
  95. if ($ios) {
  96. $result += $this->sendAction('ios', 1, $ios, $name, $content, $param);
  97. }
  98. return $result;
  99. }
  100. private function sendOne($account, $name, $content, $param)
  101. {
  102. $result = array();
  103. $info = Dever::db('passport/app')->one(array('uid' => $account));
  104. if (!$info || ($info && !$info['source_type'])) {
  105. $info = Dever::db('passport/user')->one(array('id' => $account));
  106. }
  107. //$info['source_type'] = 'ios';
  108. if ($info && $info['source_type'] && ($info['source_type'] == 'ios' || $info['source_type'] == 'android')) {
  109. $result = $this->sendAction($info['source_type'], 2, $this->prefix . $account, $name, $content, $param);
  110. } else {
  111. $result = '没有账户信息';
  112. }
  113. return $result;
  114. }
  115. private function sendAll($name, $content, $param)
  116. {
  117. $result = array();
  118. $result += $this->sendAction('android', 1, false, $name, $content, $param);
  119. $result += $this->sendAction('ios', 1, false, $name, $content, $param);
  120. return $result;
  121. }
  122. private function getUser($account)
  123. {
  124. $android = array();
  125. $ios = array();
  126. $info = Dever::db('passport/app')->getAllByUids(array('uid' => $account));
  127. if ($info) {
  128. foreach ($info as $k => $v) {
  129. if ($v['source_type'] == 'android') {
  130. $android[] = $this->prefix . $v['uid'];
  131. } elseif ($v['source_type'] == 'ios') {
  132. $ios[] = $this->prefix . $v['uid'];
  133. }
  134. }
  135. }
  136. return array($android, $ios);
  137. }
  138. private function android_message($name, $content, $param, $type = 1)
  139. {
  140. $message = new \Message();
  141. $message->setExpireTime(86400);
  142. $message->setTitle($name);
  143. $message->setContent($content);
  144. $message->setType($type);
  145. $action = new \ClickAction();
  146. //$action->setActionType(\ClickAction::TYPE_ACTIVITY);
  147. //$action->setActivity($param);
  148. $action->setActionType(\ClickAction::TYPE_INTENT);
  149. $action->setIntent($param);
  150. $message->setAction($action);
  151. return $message;
  152. }
  153. private function ios_message($name, $content, $param, $type = 1)
  154. {
  155. $message = new \MessageIOS();
  156. $message->setExpireTime(86400);
  157. $message->setAlert(array('title' => $name, 'content' => $content));
  158. $message->setBadge(-2);
  159. //$mess->setSound("beep.wav");
  160. $custom = Dever::json_decode($param);
  161. $message->setCustom($custom);
  162. //$message->setRaw($param);
  163. $time = new \TimeInterval(0, 0, 23, 59);
  164. $message->addAcceptTime($time);
  165. return $message;
  166. }
  167. private function get($type = 'android')
  168. {
  169. if (!isset($this->push[$type])) {
  170. $this->push[$type] = new \XingeApp($this->config[$type . '_appid'], $this->config[$type . '_appsecret']);
  171. }
  172. return $this->push[$type];
  173. }
  174. }