简体   繁体   English

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

[英]How to send firebase push notification using trigger

I am trying to send notification whenever the database is inserted with new value but i keep getting "error in sending request" repeatedly while trying this code,not able to find what's the problem, I have used server api key as suggested. 每当尝试使用新值插入数据库时​​,我都试图发送通知,但是在尝试此代码时,我一直反复收到“发送请求时出错”的信息,无法找到问题所在,我已按照建议使用服务器api密钥。 I have used one form that takes two textboxs which will be directed to the other file above. 我使用了一种形式,该形式带有两个文本框,这些文本框将定向到上面的另一个文件。

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

Can anyone help me on this? 谁可以帮我这个事?

Not sure if it was a typo in your code, but you need to adjust the PHP code - you've wrapped the HTML within the <?php ?> segment. 不知道这是否是您的代码中的错字,但是您需要调整PHP代码-您已经将HTML包装在<?php ?>段中。

Try exiting the PHP segment before the HTML starts like below; 尝试在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将附加数据发送到 Firebase 推送通知 - How to send additional data to firebase push notification 如何在Laravel中使用Kreait / Firebase云消息向多个设备发送发送推送通知 - How to send send push notification to multiple device using kreait/firebase cloud message in laravel 无法使用 fcm 令牌 firebase 向 ios 发送推送通知 - unable to send push notification to ios using fcm token firebase 我们如何使用Firebase云消息传递(fcm)在Android中发送推送通知来发送图像和图标 - How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android 如何使用phonegap和parse发送推送通知 - how to send push notification using phonegap and parse 如何使用php向Android发送推送通知 - How to send push notification by using php to android 如何将推送通知从 Firebase 发送到 iOS 设备上的 web 应用程序? - How to send push notification from Firebase to web app on iOS device? 使用PHP的Firebase推送通知 - Firebase push notification using PHP 如何使用FCM向我数据库中的用户发送推送通知? 安卓 - How to send a push notification to a user that is in my database using FCM? android 如何使用 PHP 发送带有大图像的信号推送通知 - How to send one signal push notification with large image using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM