Exemple #1
0
 public function show()
 {
     $this->input['text'] = trim($this->input['text']);
     if (!$this->input['text']) {
         echo 'no text';
         exit;
     }
     $appid = $this->input['appid'];
     $pushNotify = new pushNotify();
     if (!$appid) {
         $pushNotify->setCert(ZS_DIR . 'apply_push.pems');
         $pushNotify->setAPNsHost(0);
     } else {
         $sql = 'SELECT * FROM ' . DB_PREFIX . 'certificate WHERE  appid=' . $appid;
         $cert = $this->db->query_first($sql);
         $pushNotify->setCert(ZS_DIR . $cert['apply']);
         $pushNotify->setAPNsHost(0);
     }
     $connect = $pushNotify->connectToAPNS();
     if (!$connect) {
         $this->errorOutput('CONNECT_APPLE_FAILED');
     }
     $message = array('badge' => 1, 'sound' => 'default', 'text' => $this->input['text'], 'module_id' => $this->input['module_id'], 'content_id' => $this->input['id']);
     $token = $this->input['token'];
     if (!$token) {
         $token = 'f03a63a9 ba2d44aa f17f5afa 32240d46 0216dd17 b96e00ad d3972c78 fd29eacf';
     }
     $suc = $pushNotify->send($token, $message);
     if ($suc) {
         echo 'send ' . $suc . '<pre>';
         print_r($message);
     }
     $pushNotify->closeConnections();
 }
Exemple #2
0
 public function show()
 {
     $this->input['message'] = trim($this->input['message']);
     if (!$this->input['message']) {
         $this->errorOutput('NO_MESSAGE');
     }
     $token = trim($this->input['device_token']);
     if (!$token) {
         $this->errorOutput('NO_DEVICE_TOKEN');
     }
     if (strlen($token) > 60) {
         $appidkey = 'iosAppid';
     } else {
         $appidkey = 'androidAppid';
     }
     $appid = intval($this->input['appid']);
     $appid = $appid ? $appid : $this->settings[$appidkey];
     if (!$appid) {
         $this->errorOutput('NO_APPID');
     }
     if ($appidkey == 'iosAppid') {
         $pushNotify = new pushNotify();
         $sql = 'SELECT * FROM ' . DB_PREFIX . 'certificate WHERE  appid=' . $appid;
         $cert = $this->db->query_first($sql);
         $pushNotify->setCert(ZS_DIR . $cert['apply']);
         $pushNotify->setAPNsHost(0);
         $connect = $pushNotify->connectToAPNS();
         if (!$connect) {
             $this->errorOutput('CONNECT_APPLE_FAILED');
         }
         $message = array('badge' => 1, 'sound' => 'default', 'text' => $this->input['message'], 'module_id' => $this->input['module'], 'content_id' => $this->input['content_id']);
         $suc = $pushNotify->send($token, $message);
         $pushNotify->closeConnections();
     }
     $this->addItem_withkey('result', $suc ? 1 : 0);
     $this->output();
 }
Exemple #3
0
 /**
  * 消息推送(支持失败设备重新推送)
  * Enter description here ...
  */
 public function show()
 {
     //查询需要推送的全局信息
     $push_sql = "SELECT a.*\r\n\t\t\t\t\tFROM " . DB_PREFIX . "advices a \r\n\t\t\t\t\tWHERE a.is_send=0 \r\n\t\t\t\t\tAND a.send_way=1 \r\n\t\t\t\t\tAND a.state=1 \r\n\t\t\t\t\tAND a.send_time <=" . TIMENOW . " \r\n\t\t\t\t\tAND a.is_global=1  \r\n\t\t\t\t\tORDER BY a.send_time ASC \r\n\t\t\t\t\tLIMIT 1";
     $message = $this->db->query_first($push_sql);
     $fail_id = '';
     //全局推送的消息不存在时,查询发送失败表中记录
     if (!$message['id'] || !$message['message']) {
         $start_time = strtotime(date('Y-m-d', TIMENOW));
         $end_time = $start_time + 86400;
         //重复发送次数
         $repeat_push_num = defined('REPEAT_PUSH_NUM') ? REPEAT_PUSH_NUM : 0;
         //为0时,不重发消息
         if (!$repeat_push_num) {
             return false;
         }
         //查询当天发送失败的消息,失败次数不超过规定重复发送次数
         $sql = "SELECT * FROM " . DB_PREFIX . "push_failed \r\n\t\t\t\t\tWHERE fail_num < " . $repeat_push_num . " \r\n\t\t\t\t\tAND create_time > " . $start_time . " \r\n\t\t\t\t\tAND create_time < " . $end_time . " \r\n\t\t\t\t\tORDER BY update_time ASC LIMIT 1";
         $res = $this->db->query_first($sql);
         if (!$res || !$res['message_id'] || !$res['error_cause'] || !$res['id']) {
             return FALSE;
         }
         //发送失败的设备信息
         $device_info = unserialize($res['error_cause']);
         if (!$device_info) {
             return FALSE;
         }
         //查询发送失败消息的内容,模块id
         $sql = "SELECT a.appid,a.message,a.content_id,a.link_module \r\n\t\t\t\t\tFROM " . DB_PREFIX . "advices a \r\n\t\t\t\t\tWHERE a.id=" . $res['message_id'] . " AND a.state=1 LIMIT 1";
         $message = $this->db->query_first($sql);
         if (!$message) {
             return FALSE;
         }
         //失败消息记录id
         $fail_id = $res['id'];
     }
     //推送全局消息,查询消息发送记录队列
     if (!$fail_id) {
         $offset_queue = $this->db->query_first('SELECT * FROM ' . DB_PREFIX . 'publish WHERE message_id = ' . intval($message['id']));
         $offset = $offset_queue['offset'] ? $offset_queue['offset'] : 0;
         $limit = $offset . ',' . NUMBER;
         //获取符合消息要求的设备
         $device_info = $this->get_device($message, $limit);
         if (!empty($device_info)) {
             //查询到设备的时候,更新设备记录offset
             $this->db->query('REPLACE INTO ' . DB_PREFIX . 'publish VALUES(' . $message['id'] . ',' . intval($offset + NUMBER) . ')');
         }
     }
     //推送消息
     if (!empty($device_info)) {
         $info = array('badge' => 1, 'sound' => 'default', 'text' => $message['message'], 'module_id' => $message['link_module'] ? $message['link_module'] : '', 'content_id' => $message['content_id']);
         $sql = 'SELECT * FROM ' . DB_PREFIX . 'certificate WHERE  appid=' . $message['appid'];
         $appcert = $this->db->query_first($sql);
         $pushNotify = new pushNotify();
         //记录发送失败的设备
         $failed_push = array();
         foreach ($device_info as $key => $val) {
             $pushNotify->setAPNsHost($key);
             if ($key) {
                 $cert = $appcert['develop'];
             } else {
                 $cert = $appcert['apply'];
             }
             $pushNotify->setCert(ZS_DIR . $cert);
             if (!$pushNotify->connectToAPNS()) {
                 foreach ($val as $row) {
                     $failed_push[$key][]['device_token'] = $row['device_token'];
                 }
                 //$this->db->query('INSERT INTO '.DB_PREFIX.'push_failed value('.$message['id'].','.$offset.','.NUMBER.', \'LINK_APPLE_SERVER_FAILED\')');
                 //$this->errorOutput(LINK_SERVER_FAILED);
             } else {
                 //推送至客户端
                 foreach ($val as $k => $row) {
                     $is_success = $pushNotify->send($row['device_token'], $info);
                     if (!$is_success) {
                         $failed_push[$key][]['device_token'] = $row['device_token'];
                     }
                 }
             }
             $pushNotify->closeConnections();
         }
         //存在发送失败设备,记录设备信息
         if (!empty($failed_push)) {
             $failed_push = serialize($failed_push);
             //推送全局消息失败
             if (!$fail_id) {
                 $this->db->query('INSERT INTO ' . DB_PREFIX . 'push_failed (id,message_id,device_offset,device_num,error_cause,create_time,update_time,fail_num) VALUE(0,' . $message['id'] . ',' . $offset . ',' . NUMBER . ', \'' . $failed_push . '\', ' . TIMENOW . ', ' . TIMENOW . ',0)');
             } else {
                 $sql = "UPDATE " . DB_PREFIX . "push_failed SET fail_num = fail_num + 1, error_cause = '" . $failed_push . "', update_time = '" . TIMENOW . "' WHERE id = " . $fail_id;
                 $this->db->query($sql);
             }
         } else {
             if ($fail_id) {
                 $sql = "DELETE FROM " . DB_PREFIX . "push_failed WHERE id = " . $fail_id;
                 $this->db->query($sql);
             }
         }
     } else {
         //满足条件的设备全部发送完,并且不是推送失败消息,更新消息发送状态is_send=1
         $this->db->query('UPDATE ' . DB_PREFIX . 'advices set is_send=1 WHERE id = ' . intval($message['id']));
         $sql = 'DELETE FROM ' . DB_PREFIX . 'publish WHERE message_id = ' . intval($message['id']);
         $this->db->query($sql);
         if ($fail_id) {
             $sql = "DELETE FROM " . DB_PREFIX . "push_failed WHERE id = " . $fail_id;
             $this->db->query($sql);
         }
         $this->errorOutput(QUEUE_FINISHED);
     }
 }
    /**
     *手动发送信息 
     * @name doinstantMessaging
     * @param $appid string 应用id
     * @param $device_token string 设备标识
     * @param
     */
    function doinstantMessaging()
    {
        //检测是否具有配置权限
        if ($this->user['group_type'] > MAX_ADMIN_TYPE) {
            if (!$this->user['prms']['app_prms'][APP_UNIQUEID]['setting']) {
                $this->errorOutput(NO_PRIVILEGE);
            }
        }
        $device_token = $this->input['device_token'];
        if (!$device_token) {
            $this->errorOutput(NO_DEVICE_SELCTED);
        }
        //$device_token = implode('","', explode(',', $device_token));
        //$appid = intval($this->input['app_id']);
        $content = trim($this->input['push_message']);
        if (!$content) {
            $this->errorOutput('请输入消息内容!');
        }
        //消息基本信息
        $user_id = intval($this->user['user_id']);
        $user_name = $this->user['user_name'];
        $org_id = $this->user['org_id'];
        $ip = hg_getip();
        //关联模块标识
        $link_module = trim($this->input['link_module']);
        //内容id
        $content_id = intval($this->input['content_id']);
        //为推送准备
        if (!class_exists('pushNotify')) {
            include_once '../lib/push_notify.class.php';
        }
        $info = array('badge' => 1, 'sound' => 'default', 'text' => $content, 'module_id' => $link_module ? $link_module : '', 'content_id' => $content_id);
        $pushNotify = new pushNotify();
        $device_tokens = explode(',', $this->input['device_token']);
        //记录发送失败的设备
        $failed_push = array();
        foreach ($device_tokens as $k => $v) {
            $arr = array();
            $arr = explode('_', $v);
            $device_token = $arr[0];
            $appid = $arr[1];
            if (!$device_token || !$appid) {
                continue;
            }
            //查询设备信息
            $sql = 'SELECT d.appid,d.device_token,d.debug,c.send_way,c.develop,c.apply FROM ' . DB_PREFIX . 'device d 
					LEFT JOIN ' . DB_PREFIX . 'certificate c
						ON d.appid = c.appid
					WHERE d.device_token = "' . $device_token . '" AND d.appid = ' . $appid;
            $device_info = $this->db->query_first($sql);
            if (!$device_info) {
                continue;
            }
            $val = array();
            $val = $device_info;
            //send_way=1推送方式,等推送完再更新已发送,拉取直接标识已发送,状态state都为已审核
            $is_send = '';
            $is_send = $val['send_way'] ? 0 : 1;
            $data = array('org_id' => $org_id, 'user_id' => $user_id, 'message' => $content, 'username' => $user_name, 'send_time' => TIMENOW, 'create_time' => TIMENOW, 'device_token' => $val['device_token'], 'is_global' => 0, 'send_way' => $val['send_way'], 'is_send' => $is_send, 'appid' => $val['appid'], 'debug' => $val['debug'], 'state' => 1, 'ip' => $ip);
            $sql = '';
            $sql = 'INSERT INTO ' . DB_PREFIX . 'advices SET ';
            foreach ($data as $k => $v) {
                $sql .= '`' . $k . '`="' . $v . '",';
            }
            $sql = rtrim($sql, ',');
            $this->db->query($sql);
            $message_id = '';
            $message_id = $this->db->insert_id();
            //判断消息发送方式
            if ($val['send_way'] == 1 && $message_id) {
                $pushNotify->setAPNsHost($val['debug']);
                $cert = '';
                if ($val['debug']) {
                    $cert = $val['develop'];
                } else {
                    $cert = $val['apply'];
                }
                $pushNotify->setCert(ZS_DIR . $cert);
                if (!$pushNotify->connectToAPNS()) {
                    //$this->db->query('INSERT INTO '.DB_PREFIX.'push_failed value('.$message_id.',0,1)');
                    $failed_push[$val['debug']][]['device_token'] = $val['device_token'];
                    continue;
                } else {
                    $is_success = $pushNotify->send($val['device_token'], $info);
                    if (!$is_success) {
                        $failed_push[$val['debug']][]['device_token'] = $val['device_token'];
                        //$this->db->query('INSERT INTO '.DB_PREFIX.'push_failed value('.$message_id.',0,1)');
                    }
                }
                $pushNotify->closeConnections();
                $this->db->query('UPDATE ' . DB_PREFIX . 'advices SET is_send=1 WHERE id = ' . $message_id);
            }
        }
        //记录发送失败的设备记录
        if (!empty($failed_push)) {
            $failed_push = serialize($failed_push);
            $sql = 'INSERT INTO ' . DB_PREFIX . 'push_failed value("",' . $message_id . ',0,1, \'' . $failed_push . '\', ' . TIMENOW . ', ' . TIMENOW . ',0)';
            $this->db->query($sql);
        }
        $this->addItem('success');
        $this->output();
    }