"5716", "notification_type" => "stack"), "YOUR_GCM_API_KEY"); function sendPushNotification($message, $google_api_key){ if(empty($google_api_key)){ echo "Key Empty"; return; } echo $message; print_r($message); global $wpdb; $gcm = new GCM(); $table_name = $wpdb->prefix . 'pw_gcmusers'; $offset = 0; $batch_size = 50; //$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"); while(true){ $registration_ids = array(); $primary_ids = array(); $sql = "select id, gcm_regid from $table_name where status = 1 limit $offset, $batch_size;"; $results = $wpdb->get_results($sql); if(empty($results)){ break; } foreach($results as $entry){ $registration_ids[] = $entry->gcm_regid; $primary_ids[] = $entry->id; } $gcmResult = $gcm->send_notification($registration_ids, $message, $google_api_key); echo ($gcmResult); //echo "
"; //Deleting entries only if there are substantial entries in the table if(count($registration_ids) > 47){ removeInvalidRegistrationIds(json_decode($gcmResult), $primary_ids); } $offset += $batch_size; } } function removeInvalidRegistrationIds($jsonArray, $registration_ids){ global $wpdb; $table_name = $wpdb->prefix . 'pw_gcmusers'; if(!empty($jsonArray->results)){ $remove_ids = array(); for($i=0; $iresults);$i++){ if(isset($jsonArray->results[$i]->error)){ echo "error".$jsonArray->results[$i]->error."
"; if($jsonArray->results[$i]->error == "NotRegistered" || $jsonArray->results[$i]->error == "InvalidRegistration"){ $remove_ids[] = $registration_ids[$i]; } } } if(!empty($remove_ids)){ 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 $updateQuery; $wpdb->query( $updateQuery ); } } } ?>