public static function send_notification()
 {
     global $wpdb;
     if (!empty($_GET['savehistory'])) {
         update_option('smpush_history', $_POST);
         echo 1;
         exit;
     }
     if (!empty($_GET['clearhistory'])) {
         update_option('smpush_history', '');
         echo 1;
         exit;
     }
     if (get_transient('smpush_resum') !== false && !isset($_GET['lastid'], $_GET['increration'])) {
         $_POST = get_transient('smpush_post');
     }
     if ($_POST) {
         if (isset($_POST['message'], $_POST['type'])) {
             $wpdb->query("TRUNCATE `" . $wpdb->prefix . "push_queue`");
             $wpdb->query("TRUNCATE `" . $wpdb->prefix . "push_feedback`");
             delete_transient('smpush_send_options');
             self::$sendoptions['message'] = $_POST['message'];
             self::updateStats();
             if ($_POST['type'] == 'ios') {
                 $where = "AND {tbname}.{type_name}='{ios_name}'";
             } elseif ($_POST['type'] == 'android') {
                 $where = "AND {tbname}.{type_name}='{android_name}'";
             } else {
                 $where = '';
             }
             if (isset($_POST['channel'])) {
                 $defconid = self::$apisetting['def_connection'];
                 $channelids = implode(',', $_POST['channel']);
                 $tablename = $wpdb->prefix . 'push_relation';
                 $smpush_query = self::parse_query("SELECT {tbname}.{id_name} AS token_id,{tbname}.{token_name} AS device_token,{tbname}.{type_name} AS device_type FROM {$tablename}\r\n          INNER JOIN {tbname} ON({tbname}.{id_name}={$tablename}.token_id AND {tbname}.{active_name}='1' {$where})\r\n          WHERE {$tablename}.channel_id IN({$channelids}) AND {$tablename}.connection_id='{$defconid}' AND {$tablename}.token_id>[lastid] GROUP BY {$tablename}.token_id ASC LIMIT 0,[limit]");
                 $alltokens = $wpdb->get_var(self::parse_query("SELECT COUNT({$tablename}.token_id) FROM {$tablename}\r\n          INNER JOIN {tbname} ON({tbname}.{id_name}={$tablename}.token_id AND {tbname}.{active_name}='1' {$where})\r\n          WHERE {$tablename}.channel_id IN({$channelids}) AND {$tablename}.connection_id='{$defconid}'"));
             } else {
                 $smpush_query = self::parse_query("SELECT {id_name} AS token_id,{token_name} AS device_token,{type_name} AS device_type FROM {tbname} WHERE {active_name}='1' {$where} AND {id_name}>[lastid] ORDER BY {id_name} ASC LIMIT 0,[limit]");
                 $alltokens = self::$pushdb->get_var(self::parse_query("SELECT COUNT({id_name}) FROM {tbname} WHERE {active_name}='1' {$where}"));
                 if ($alltokens === null) {
                     wp_die('Please reconfig the default push notification database connection <a href="' . admin_url() . 'admin.php?page=smpush_connections">here</a>');
                 }
             }
             $feedback = isset($_POST['feedback']) ? 1 : 0;
             $iostestmode = isset($_POST['iostestmode']) ? 1 : 0;
             if (isset($_POST['feedback']) && ($_POST['type'] == 'ios' or $_POST['type'] == 'all')) {
                 $wpdb->insert($wpdb->prefix . 'push_feedback', array('device_type' => 'ios'));
             }
             $_POST['feedback'] = $feedback;
             $_POST['iostestmode'] = $iostestmode;
             if ($_POST['extra_type'] == 'multi') {
                 $json = array();
                 foreach ($_POST['key'] as $loop => $key) {
                     if (!empty($key) && !empty($_POST['value'][$loop])) {
                         $json[$key] = $_POST['value'][$loop];
                     }
                 }
                 if (empty($json)) {
                     $_POST['extra'] = '';
                     $_POST['extra_type'] = '';
                 } else {
                     $_POST['extra'] = json_encode($json);
                     $_POST['extra_type'] = 'json';
                 }
             }
             if ($_POST['and_extra_type'] == 'multi') {
                 $json = array();
                 foreach ($_POST['and_key'] as $loop => $key) {
                     if (!empty($key) && !empty($_POST['and_value'][$loop])) {
                         $json[$key] = $_POST['and_value'][$loop];
                     }
                 }
                 if (empty($json)) {
                     $_POST['and_extra'] = '';
                     $_POST['and_extra_type'] = '';
                 } else {
                     $_POST['and_extra'] = json_encode($json);
                     $_POST['and_extra_type'] = 'json';
                 }
             }
             $options = array('message' => $_POST['message'], 'iostestmode' => $_POST['iostestmode'], 'feedback' => $_POST['feedback'], 'expire' => $_POST['expire'], 'ios_slide' => $_POST['ios_slide'], 'ios_badge' => $_POST['ios_badge'], 'ios_sound' => $_POST['ios_sound'], 'ios_cavailable' => $_POST['ios_cavailable'], 'ios_launchimg' => $_POST['ios_launchimg'], 'extra_type' => $_POST['extra_type'], 'extravalue' => $_POST['extra'], 'and_extra_type' => $_POST['and_extra_type'], 'and_extravalue' => $_POST['and_extra']);
             $sendtimeformat = $_POST['mm'] . '/' . $_POST['jj'] . '/' . $_POST['aa'] . ' ' . $_POST['hh'] . ':' . $_POST['mn'] . ':00';
             $options['sendtime'] = strtotime($sendtimeformat, current_time('timestamp', 1));
             $options['sendtype'] = isset($_POST['sendnow']) ? 'sendnow' : 'cronsend';
             if ($options['sendtype'] == 'cronsend') {
                 $transient_unique = rand(1000, 3000) . current_time('timestamp', 1);
                 set_transient('smpush_cronsend_' . $transient_unique, $options, $options['sendtime'] + 864000);
                 $options['uniqueoperid'] = $transient_unique;
             } else {
                 self::updateStats('totalsend', $alltokens);
             }
             set_transient('smpush_send_options', $options, 2592000);
             set_transient('smpush_post', $_POST, 43200);
             set_transient('smpush_query', $smpush_query, 43200);
             if ($alltokens == 0) {
                 $increration = 0;
             } else {
                 $increration = ceil($alltokens / 20);
             }
             self::send_process(false, $alltokens, $increration, $feedback);
         } else {
             wp_redirect(admin_url() . 'admin.php?page=smpush_send_notification');
         }
     } elseif (isset($_GET['lastid'], $_GET['increration'])) {
         $lstid_trans = get_transient('smpush_lastid');
         if ($lstid_trans !== false) {
             $_GET['lastid'] = $lstid_trans;
         }
         $smpush_query = get_transient('smpush_query');
         $options = get_transient('smpush_send_options');
         $query = str_replace(array('[lastid]', '[limit]'), array($_GET['lastid'], $_GET['increration']), $smpush_query);
         $tokens = self::$pushdb->get_results($query);
         if (!empty(self::$pushdb->last_error)) {
             self::jsonPrint(0, '<p class="error">Please reconfig the default push notification database connection</p>');
         }
         if ($tokens) {
             if ($options['sendtype'] == 'sendnow') {
                 foreach ($tokens as $token) {
                     $wpdb->insert($wpdb->prefix . 'push_queue', array('token' => $token->device_token, 'device_type' => $token->device_type));
                     $lastid = $token->token_id;
                 }
             } else {
                 foreach ($tokens as $token) {
                     $wpdb->insert($wpdb->prefix . 'push_cron_queue', array('token' => $token->device_token, 'device_type' => $token->device_type, 'sendtime' => $options['sendtime'], 'sendoptions' => $options['uniqueoperid']));
                     $lastid = $token->token_id;
                 }
             }
             set_transient('smpush_lastid', $lastid, 43200);
             set_transient('smpush_resum', 1, 43200);
             self::jsonPrint(1, $lastid);
         }
         delete_transient('smpush_lastid');
         delete_transient('smpush_post');
         delete_transient('smpush_query');
         delete_transient('smpush_resum');
         if ($options['sendtype'] == 'sendnow') {
             self::jsonPrint(-1, '');
         } else {
             self::archiveMsgLog($options['message'], $options['sendtime'], $options['uniqueoperid']);
             self::jsonPrint(-2, '');
         }
     } else {
         $queuecount = $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->prefix . "push_queue");
         if ($queuecount > 0) {
             self::send_process(true, $queuecount);
         } else {
             $params = array();
             $params['all'] = self::$defconnection['counter'];
             $params['ios'] = self::$pushdb->get_var(self::parse_query("SELECT COUNT({id_name}) FROM {tbname} WHERE {type_name}='{ios_name}'"));
             $params['channels'] = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "push_channels ORDER BY id ASC");
             $params['dbtype'] = $wpdb->get_var("SELECT dbtype FROM " . $wpdb->prefix . "push_connection WHERE id='" . self::$apisetting['def_connection'] . "'");
             wp_enqueue_script('postbox');
             self::$history = get_option('smpush_history', $_POST);
             self::loadpage('send_notification', 0, $params);
         }
     }
 }