Как отправить пожарное push-уведомление с помощью триггера

0

Я пытаюсь отправить уведомление всякий раз, когда база данных вставлена с новым значением, но я постоянно получаю "ошибку при отправке запроса" при попытке этого кода, не в состоянии найти, в чем проблема, я использовал серверный api key, как было предложено. Я использовал одну форму, которая принимает два текстовых поля, которые будут направлены в другой файл выше.

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

Может ли кто-нибудь помочь мне в этом?

  • 0
    это php, а не android
Теги:
firebase
push-notification
triggers

1 ответ

0

Не уверен, что это была опечатка в вашем коде, но вам нужно настроить PHP-код - вы завернули HTML в сегменте <?php ?>.

Попробуйте выйти из PHP-сегмента до начала HTML-кода, как показано ниже;

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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню