Example #1
0
<?php

include_once './dbInstance.php';
include_once './dbConfig.php';
if (isset($_POST[DB_REGID]) && isset($_POST[DB_GROUP])) {
    $regId = $_POST[DB_REGID];
    $group = $_POST[DB_GROUP];
    $mDbInstance = new dbInstance();
    $mDbInstance->dbConnect();
    $newReferId = $mDbInstance->dbInsert($regId, $group);
    $mDbInstance->dbDisconnect();
} else {
    echo 'No input data';
}
 $message = $argv[1];
 $gcmSuccess = false;
 $mDbInstance = new dbInstance();
 $mDbInstance->dbConnect();
 // Get all 'registration IDs' from database
 $userRegIds = $mDbInstance->dbGetAllUsers();
 $mGcmInstance = new gcmInstance();
 for ($i = 0; $i < mysqli_num_rows($userRegIds); $i++) {
     $userArray = mysqli_fetch_row($userRegIds);
     // Send message to devices and getting back a new 'registration ID' in case it has been updated by the GCM server
     $gcmSuccess = $mGcmInstance->sendNotification($userArray, $message);
     if ($gcmSuccess) {
         // If the return value is not '', the entry has to be updated ('dbInsert' with a third argument)
         if ($mGcmInstance->newId != '') {
             //echo 'From dbInstance, result: ' . $mGcmInstance->newId . PHP_EOL;
             $resultQuery = $mDbInstance->dbInsert($mGcmInstance->newId, "", $userArray[0]);
             echo 'Update query result: ' . $resultQuery . PHP_EOL;
             // If '0' is retrieved, no update has been performed, and therefore the entry is repeated
             if ($resultQuery == 0) {
                 $resultQuery = $mDbInstance->dbDelete($userArray[0]);
                 echo 'Entry deleted' . PHP_EOL;
             }
         }
     } else {
         if ($mGcmInstance->error != '') {
             echo 'From dbInstance, error: ' . $mGcmInstance->error . PHP_EOL;
             $resultQuery = $mDbInstance->dbDelete($userArray[0]);
             echo 'Entry deleted: ' . $resultQuery . PHP_EOL;
         }
     }
     // Reset 'gcmInstance' values for next iteration