"5716", "notification_type" => "stack"), "YOUR_GCM_API_KEY"); function sendPushNotification($message, $google_api_key){ if(empty($google_api_key)){ echo "Key Empty"; return; } $success = 0; $notRegistered = 0; $mismatchsenderid = 0; $otherError = 0; $remove_ids = array(); print_r($message); global $wpdb; $gcm = new GCM(); $table_name = $wpdb->prefix . 'pw_gcmusers'; $stats_table_name = $wpdb->prefix . 'androapp_stats'; $offset = 0; $batch_size = 5000; //$message = array("post_id" => "5716", "link" => "http://puzzlersworld.com/math-puzzles/four-digit-number-aabb/", "title"=>"Two Eggs Puzzle", "excerpt"=>"there are two eggs, 100 floor building, you have to find the max building number from which the egg will not brake in minimum number of attempts"); $eligible = 0; $title = $message['title']; if(empty($title)){ $title = "post id ".$message['post_id']; } $id = updateStats($title, $eligible, $success, $notRegistered, $mismatchsenderid, $otherError, 'START',''); $outArraySize = 0; while(true){ $registration_ids = array(); $primary_ids = array(); $sql = "select id, gcm_regid from $table_name where status = 1 limit $offset, $batch_size;"; echo "
fetch query $sql"; $results = $wpdb->get_results($sql); if(empty($results)){ break; } foreach($results as $entry){ $registration_ids[] = $entry->gcm_regid; $primary_ids[] = $entry->id; $eligible++; } unset($results); $results = null; $outArray = array_chunk($registration_ids, 90); unset($registration_ids); $registration_ids = null; $outArraySize = count($outArray); $primaryIdsArr = array_chunk($primary_ids, 90); unset($primary_ids); $primary_ids = null; $i=0; foreach($outArray as $regIds){ //echo "Sending ".count($regIds) ." notifications"; $gcmResult = $gcm->send_notification($regIds, $message, $google_api_key); echo ($gcmResult); updateCounts(json_decode($gcmResult),$success, $notRegistered, $mismatchsenderid, $otherError, $remove_ids, $primaryIdsArr[$i] ); $outArray[$i] = null; $primaryIdsArr[$i] = null; $i++; } updateStats($title, $eligible, $success, $notRegistered, $mismatchsenderid, $otherError, 'WIP',$id); //echo "
"; //Deleting entries only if there are substantial entries in the table $offset += $batch_size; } if($outArraySize > 1) { removeInvalidRegistrationIds($remove_ids); } updateStats($title, $eligible, $success, $notRegistered, $mismatchsenderid, $otherError, 'FINISH',$id); } function updateCounts($jsonArray, &$success, &$notRegistered, &$mismatchsenderid, &$otherError, & $remove_ids, $primary_ids ){ if(!empty($jsonArray->results)){ for($i=0; $iresults);$i++){ if(isset($jsonArray->results[$i]->error)){ if($jsonArray->results[$i]->error == "NotRegistered" || $jsonArray->results[$i]->error == "InvalidRegistration"){ $notRegistered++; $remove_ids[] = $primary_ids[$i]; //echo "remove "; //print_r($remove_ids); }else if($jsonArray->results[$i]->error == "MismatchSenderId"){ $mismatchsenderid++; }else if($jsonArray->results[$i]->error == "InvalidRegistration"){ }else{ $otherError++; } }else{ $success++; } } } } function updateStats($title, $eligible, $success, $notRegistered, $mismatchsenderid, $otherError, $status, $id){ global $wpdb; $table_name = $wpdb->prefix . 'androapp_stats'; if(empty($id)){ $query = "insert into $table_name (`title`,`eligible`,`success`,`notRegistered`,`mismatchsenderid`,`other`,`status`) VALUES('$title',$eligible,$success,$notRegistered,$mismatchsenderid,$otherError,'$status')"; echo $query; $res = $wpdb->query( $query ); return $wpdb->insert_id; }else{ $query = "update $table_name set title = '$title', eligible = $eligible,success = $success, notRegistered = $notRegistered,mismatchsenderid = $mismatchsenderid, other = $otherError, status = '$status' where id = $id"; echo $query; $wpdb->query( $query ); } echo $res; } function removeInvalidRegistrationIds($remove_ids){ global $wpdb; $table_name = $wpdb->prefix . 'pw_gcmusers'; if(count($remove_ids) > 0){ //print_r($remove_ids); $remove_ids = implode(',', $remove_ids); //$deleteQuery = "DELETE FROM $table_name WHERE id in ({$remove_ids});"; $updateQuery = "update $table_name set status = 0 where id in ({$remove_ids});"; echo "update query ".$updateQuery; $wpdb->query( $updateQuery ); } } ?>