如何使用触发器发送firebase推送通知

jhdbpxl9  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(380)

我试图发送通知,每当数据库插入新的值,但我不断得到“错误,在发送请求”反复而尝试这段代码,不能找到什么问题,我已经使用服务器api密钥的建议。我使用了一个表单,它包含两个文本框,这两个文本框将指向上面的另一个文件。

<?php 
 $host='localhost';
 $username='';
 $pwd="";
 $db="";

 $con=mysqli_connect($host,$username,$pwd,$db);
 if($_SERVER['REQUEST_METHOD']=='POST'){
 $full_name = $_POST['full_name'];
 $contact_number = $_POST['contact_number'];

 //require_once('dbConnect.php');
 $sql = "INSERT INTO notification (full_name,contact_number) VALUES (
     '$full_name'
     '$contact_number')";

$check = "SELECT * from notification where full_name='$full_name' AND contact_number='$contact_number'";
     $checkData = mysqli_query($con,$check);
     if (mysqli_num_rows($checkData) > 0) {
      echo "Request already posted";
     }else{

    if(mysqli_query($con,$sql)){

               $notiTitle = "notification request";
               $notiMessage ="by".$full_name;

                    sendNotification($notiTitle, $notiMessage); 
        echo "sucessfully added";

        }else{
        echo "error in sending request";
        }
    }

}else{
echo 'error';
}

function sendNotification($title, $msg) {
$titlee = $title;
$message = $msg;
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AIz#################################";
$sql = "SELECT app_id FROM user_app_id";
$result = mysqli_query($con,$sql);

// fetch all key of devices 

$finalKey=array();
while($row= mysqli_fetch_array($result)){
$finalKey[]=$row['app_id'];
}
$headers = array(
    'Authorization:key=' .$server_key, 
            'Content-Type : application/json');

$fields = array('registration_ids'=>$finalKey, 'notification'=>array('title'=>$title, 'body'=>$message));

$payload = json_encode($fields);

$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($curl_session);
curl_close($curl_session);
echo $result;
mysqli_close($con);

}

<html>
<body>

<form action="init.php" method="post">
Full Name: <input type="text" name="full_name"><br>
Contact No: <input type="text" name="contact_number"><br>
<input type="submit" value="submit">
</form>

</body>
</html>
?>

有人能帮我吗?

qybjjes1

qybjjes11#

不确定这是否是您的代码中的一个输入错误,但是您需要调整php代码—您已经 Package 了 HTML

在html开始之前尝试退出php段,如下所示;

?>
<html>
<body>

<form action="init.php" method="post">
Full Name: <input type="text" name="full_name"><br>
Contact No: <input type="text" name="contact_number"><br>
<input type="submit" value="submit">
</form>

</body>
</html>

相关问题