Search code examples
phpruby-on-railsfirebase-notifications

ROR Firebase Notification


I have a question, I have a working PHP firebase notification script, but I am still sending the notification locally via XAMPP, if I want my RoR server to send the notification, do I have to re code the whole PHP script to a ruby script? or can I just call my php script from the RoR server directly?

Below is my working php script.

function send_notification($tokens, $message)
{
$url = "https://fcm.googleapis.com/fcm/send";
$fields = array(
    'registration_ids' => $tokens,
    'data' => array("message"=> $message) ,
);

$headers = array(
    'Authorization:key = AuthorizationKey',
    '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_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));

$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl Failed:' . curl_error($ch));
}
curl_close($ch);

return $result;
}

$conn = mysqli_connect("localhost","root","","demoflying");    

$sql = "Select Token From Users";

$result = mysqli_query($conn,$sql);
$tokens = array();

if(mysqli_num_rows($result)>0)
{
    while ($row = mysqli_fetch_assoc($result))
    {
        $tokens[] = $row["Token"];
    }
}

mysqli_close($conn);

$fromCountry = "UK";
$toCountry= "US";

$message = array(
    "FromCountry" => $fromCountry,
    "ToCountry" => $toCountry,
); 
$message_status = send_notification($tokens,$message);
echo $message_status;
?>

Solution

  • It is possible to run php script writing this php #{RAILS_ROOT}/public/php_script.php in your controller