Beispiel #1
0
function px_sendGCM($message, $type, $regid)
{
    global $wpdb;
    $px_table_name = $wpdb->prefix . 'gcm_users';
    $options = get_option('gcm_setting');
    $apiKey = $options['api-key'];
    $url = 'https://android.googleapis.com/gcm/send';
    $result;
    $id;
    if ($regid == 010) {
        $id = px_getIds();
    } else {
        $id = $regid;
    }
    if ($id == 010 && $id >= 1000) {
        $newId = array_chunk($id, 1000);
        foreach ($newId as $inner_id) {
            $fields = array('registration_ids' => $inner_id, 'data' => array($type => $message));
            $headers = array('Authorization' => 'key=' . $apiKey, 'Content-Type' => 'application/json');
            $result = wp_remote_post($url, array('method' => 'POST', 'headers' => $headers, 'httpversion' => '1.0', 'sslverify' => false, 'body' => json_encode($fields)));
        }
    } else {
        $fields = array('registration_ids' => $id, 'data' => array($type => $message));
        $headers = array('Authorization' => 'key=' . $apiKey, 'Content-Type' => 'application/json');
        $result = wp_remote_post($url, array('method' => 'POST', 'headers' => $headers, 'httpversion' => '1.0', 'sslverify' => false, 'body' => json_encode($fields)));
    }
    $msg = $result['body'];
    $answer = json_decode($msg);
    $cano = px_canonical($answer);
    $suc = $answer->{'success'};
    $fail = $answer->{'failure'};
    $options = get_option('gcm_setting');
    if ($options['debug'] != false) {
        $inf = "<div id='message' class='updated'><p><b>" . __('Message sent.', 'px_gcm') . "</b><i>&nbsp;&nbsp;({$message})</i></p><p>{$msg}</p></div>";
    } else {
        $inf = "<div id='message' class='updated'><p><b>" . __('Message sent.', 'px_gcm') . "</b><i>&nbsp;&nbsp;({$message})</i></p><p>" . __('success:', 'px_gcm') . " {$suc}  &nbsp;&nbsp;" . __('fail:', 'px_gcm') . " {$fail} </p></div>";
    }
    // Updating stats
    $suc_num = get_option('px_gcm_suc_msg', 0);
    update_option('px_gcm_suc_msg', $suc_num + $suc);
    $fail_num = get_option('px_gcm_fail_msg', 0);
    update_option('px_gcm_fail_msg', $fail_num + $fail);
    $total_msg = get_option('px_gcm_total_msg', 0);
    update_option('px_gcm_total_msg', $total_msg + 1);
    for ($i = 0; $i < count($id); $i++) {
        $temp = $id[$i];
        $send_msg = $wpdb->get_row("SELECT send_msg FROM {$px_table_name} WHERE gcm_regid='{$temp}' ");
        $new_num = $send_msg->send_msg + 1;
        $upquery = "UPDATE {$px_table_name} SET send_msg={$new_num} WHERE gcm_regid='{$temp}' ";
        $wpdb->query($upquery);
    }
    global $gcm_result;
    $gcm_result = $inf;
    return $inf;
}
Beispiel #2
0
function px_sendGCM($message, $type)
{
    $result;
    $options = get_option('gcm_setting');
    $apiKey = $options['api-key'];
    $url = 'https://android.googleapis.com/gcm/send';
    $id = px_getIds();
    if ($id >= 1000) {
        $newId = array_chunk($id, 1000);
        foreach ($newId as $inner_id) {
            $fields = array('registration_ids' => $inner_id, 'data' => array($type => $message));
            $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            $result = curl_exec($ch);
        }
    } else {
        $fields = array('registration_ids' => $id, 'data' => array($type => $message));
        $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
    }
    $answer = json_decode($result);
    $cano = px_canonical($answer);
    $suc = $answer->{'success'};
    $fail = $answer->{'failure'};
    $options = get_option('gcm_setting');
    if ($options['debug'] != false) {
        $inf = "<div id='message' class='updated'><p><b>" . __('Message sent.', 'px_gcm') . "</b><i>&nbsp;&nbsp;({$message})</i></p><p>{$result}</p></div>";
    } else {
        $inf = "<div id='message' class='updated'><p><b>" . __('Message sent.', 'px_gcm') . "</b><i>&nbsp;&nbsp;({$message})</i></p><p>" . __('success:', 'px_gcm') . " {$suc}  &nbsp;&nbsp;" . __('fail:', 'px_gcm') . " {$fail} </p></div>";
    }
    curl_close($ch);
    print_r($inf);
    return $result;
}