0

It Send Notification to android devices registered in the mysql database. this is the php code i'm using. and it's working well as far as i can tell.

<?php
$con = mysql_connect("*******.*****.com","*******","*********");

mysql_select_db("*******",$con);

$result1 = mysql_query("SELECT Device_ID FROM Registered_Users") or die(mysql_error());

$api_key = "***************-***********";

  $response["products"] = array(); 
   while ($row = mysql_fetch_array($result1))
 {
     $reg_id = array($row['Device_ID']);
     $registrationIDs = $reg_id;
     $message = $_POST['msg'];

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids'  => $registrationIDs,'data' 
                       => array( "message" => $message ),);
                   $headers = array('Authorization: key=' 
                     . $api_key,'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_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch);
    curl_close($ch);
}
echo "success";
?>

my question is : how can i improve this code to get the best performance " results " possible ?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
jack
  • 155
  • 5
  • 14
  • I'd say it's quite simple and does what it has to do without too much overhead. You should detail what motivates you to try to improve it in terms of performance. Does it take so long to process? – nKn Aug 28 '15 at 20:05
  • You can make everything parallel. Instead of going through the list of devices sequentially, you can just fork and push notifications to 30-50 devices at a time. documentation with examples: http://php.net/manual/en/function.pcntl-fork.php – Dimi Aug 28 '15 at 20:09
  • Please don't use mysql_* functions!! [See this posting as to why](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – james Aug 28 '15 at 20:52
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 28 '15 at 21:05

2 Answers2

0

use this way........

<?php
$con = mysql_connect("*******.*****.com","*******","*********");

mysql_select_db("*******",$con);

$result1 = mysql_query("SELECT Device_ID FROM Registered_Users") or die(mysql_error());
while ($row = mysql_fetch_array($result1))
 {
     $reg_id[] = $row['Device_ID'];
     //from this way you dont need to run function again and again......
 }
$api_key = "***************-***********";

  $response["products"] = array(); 

     $registrationIDs = $reg_id;
     $message = $_POST['msg'];

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids'  => $registrationIDs,'data' 
                       => array( "message" => $message ),);
                   $headers = array('Authorization: key=' 
                     . $api_key,'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_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch);
    curl_close($ch);

echo "success";
?>

this will increase code performance....may this will help you

Sahil Manchal
  • 472
  • 6
  • 20
  • Why should the OP "use this way"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Aug 28 '15 at 21:05
0

You should send one curl request which includes an array of registration_ids. Doing this will use less resources as it only has to connect to Google's servers in one step rather than sending off several requests. You can just send one request to handle many notifications.

You should also be using something other than mysql_* to interact with your database, for example PDO:

<?php
$dbName     = '*****';
$dbUsername = '*****';
$dbPassword = '*****';
$api_key    = "***************-***********";
$url        = 'https://android.googleapis.com/gcm/send';
$message    = $_POST['msg'];

$pdo = new PDO('mysql:host=localhost;dbname=' . $dbName, $dbUsername, $dbPassword);

$stmt      = $pdo->query("SELECT Device_ID FROM Registered_Users");
$dbResults = $stmt->fetchAll();
$stmt->closeCursor();

$regIds = array_map(function (array $dbResult) {
    return $dbResult['Device_ID'];
}, $dbResults);

$fields  = array(
    'registration_ids' => $regIds,
    'data'             => array( "message" => $message ),
);
$headers = array(
    'Authorization: key=' . $api_key,
    '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_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);

echo "success";
james
  • 26,141
  • 19
  • 95
  • 113