123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace Message\Lib;
- use Dever;
- Dever::apply('sdk/xg', 'message');
- class Push
- {
- private $prefix = 'dever_';
- public function send($id, $uid, $name, $content, $param)
- {
- $this->config = Dever::db('message/push')->one($id);
- if ($this->config) {
- # 现在就是腾讯信鸽
- if ($this->config['type'] == 1) {
- $total = count($uid);
- $num = 10;
- if ($total <= 0) {
- # 发全部
- $result = $this->sendAll($name, $content, $param);
- } elseif ($total == 1) {
- # 发单条
- $result = $this->sendOne($uid[0], $name, $content, $param);
- } elseif ($total <= $num) {
- # 发多条
- $result = $this->sendList($uid, $name, $content, $param);
- } else {
- # 批量发送
- $result = $this->sendMul($uid, $name, $content, $param);
- }
- return $result;
- }
- }
- return false;
- }
- private function sendAction($source_type, $type, $account, $name, $content, $param)
- {
- $result = array();
- $method = $source_type . '_message';
- if ($param && is_array($param)) {
- if ($source_type == 'ios' && isset($param[0])) {
- $param = $param[0];
- }
- if ($source_type == 'android' && isset($param[1])) {
- $param = $param[1];
- }
- }
- $message = $this->$method($name, $content, $param);
- $push = $this->get($source_type);
- if ($type == 1) {
- $method = 'PushAllDevices';
- $send = array(0, $message);
- } elseif ($type == 2) {
- $method = 'PushSingleAccount';
- $send = array(0, $account, $message);
- } elseif ($type == 3) {
- $method = 'PushAccountList';
- $send = array(0, $account, $message);
- }
- if ($source_type == 'ios') {
- $send[] = \XingeApp::IOSENV_DEV;
- }
- $result[$source_type]['send'] = $send;
- $result[$source_type]['message'] = array($name, $content, $param);
- $result[$source_type]['result'] = call_user_func_array(array($push, $method), $send);
- $log = Dever::json_encode($result[$source_type]);
- Dever::log($log, 'message_app_push');
- return $result;
- }
- private function sendMul($account, $name, $content, $param)
- {
- return $this->sendList($account, $name, $content, $param);
- # 大批量发送
- $result = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
- if (!($result['ret_code'] === 0)) {
-
- } else {
- $page = intval($total/$num)+1;
- for ($i = 0; $i <= $page; $i++) {
- $array = array();
- for($j = 0; $j <= $i+$num; $j++) {
- $k = $i + $j;
- if (isset($uid[$k])) {
- $array[$k] = $prefix . $uid[$k];
- }
- }
- array_push($result, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
- }
- }
- }
- private function sendList($account, $name, $content, $param)
- {
- $result = array();
- list($android, $ios) = $this->getUser($account);
- if ($android) {
- $result += $this->sendAction('android', 1, $android, $name, $content, $param);
- }
- if ($ios) {
- $result += $this->sendAction('ios', 1, $ios, $name, $content, $param);
- }
- return $result;
- }
- private function sendOne($account, $name, $content, $param)
- {
- $result = array();
- $info = Dever::db('passport/app')->one(array('uid' => $account));
- if (!$info || ($info && !$info['source_type'])) {
- $info = Dever::db('passport/user')->one(array('id' => $account));
- }
- //$info['source_type'] = 'ios';
- if ($info && $info['source_type'] && ($info['source_type'] == 'ios' || $info['source_type'] == 'android')) {
- $result = $this->sendAction($info['source_type'], 2, $this->prefix . $account, $name, $content, $param);
- } else {
- $result = '没有账户信息';
- }
- return $result;
- }
- private function sendAll($name, $content, $param)
- {
- $result = array();
- $result += $this->sendAction('android', 1, false, $name, $content, $param);
- $result += $this->sendAction('ios', 1, false, $name, $content, $param);
- return $result;
- }
- private function getUser($account)
- {
- $android = array();
- $ios = array();
- $info = Dever::db('passport/app')->getAllByUids(array('uid' => $account));
- if ($info) {
- foreach ($info as $k => $v) {
- if ($v['source_type'] == 'android') {
- $android[] = $this->prefix . $v['uid'];
- } elseif ($v['source_type'] == 'ios') {
- $ios[] = $this->prefix . $v['uid'];
- }
- }
- }
- return array($android, $ios);
- }
- private function android_message($name, $content, $param, $type = 1)
- {
- $message = new \Message();
- $message->setExpireTime(86400);
- $message->setTitle($name);
- $message->setContent($content);
- $message->setType($type);
- $action = new \ClickAction();
- //$action->setActionType(\ClickAction::TYPE_ACTIVITY);
- //$action->setActivity($param);
- $action->setActionType(\ClickAction::TYPE_INTENT);
- $action->setIntent($param);
- $message->setAction($action);
- return $message;
- }
- private function ios_message($name, $content, $param, $type = 1)
- {
- $message = new \MessageIOS();
- $message->setExpireTime(86400);
- $message->setAlert(array('title' => $name, 'content' => $content));
- $message->setBadge(-2);
- //$mess->setSound("beep.wav");
- $custom = Dever::json_decode($param);
- $message->setCustom($custom);
- //$message->setRaw($param);
- $time = new \TimeInterval(0, 0, 23, 59);
- $message->addAcceptTime($time);
- return $message;
- }
- private function get($type = 'android')
- {
- if (!isset($this->push[$type])) {
- $this->push[$type] = new \XingeApp($this->config[$type . '_appid'], $this->config[$type . '_appsecret']);
- }
- return $this->push[$type];
- }
- }
|