ROR Firebase通知

时间:2016-07-12 05:58:46

标签: php ruby-on-rails firebase-notifications

我有一个问题,我有一个有效的PHP firebase通知脚本,但是我仍然通过XAMPP在本地发送通知,如果我希望我的RoR服务器发送通知,我是否需要重新编写整个PHP脚本代码红宝石脚本?或者我可以直接从RoR服务器调用我的PHP脚本吗?

以下是我工作的php脚本。

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;
?>

1 个答案:

答案 0 :(得分:0)

可以运行php脚本在控制器中编写此php #{RAILS_ROOT}/public/php_script.php