How can we send GCM messages in lots of 1000 to a database of, say, 10,000 registered users?

后端 未结 1 1822
予麋鹿
予麋鹿 2021-01-16 16:12

I am using the following code to send GCM messages using PHP and MySQL. Please help me out so that it can send GCM messages in lots of 1000 to a database of, say, 10,000 reg

相关标签:
1条回答
  • 2021-01-16 16:40
    //GCM Send Notification
    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(sizeof($regid) == 010) {
    $id = px_getIds(); //Can you post this function, what is this condition for?
    }else {
    $id = $regid;
    }
    
    if(sizeof($id) > 1000){ // the condition over here cannot be both equal to 10 and >= 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>";
    }
    
    0 讨论(0)
提交回复
热议问题