首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用触发器发送firebase推送通知

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

Stack Overflow用户
提问于 2018-04-11 00:23:00
回答 1查看 473关注 0票数 0

我试图在数据库插入新值时发送通知,但在尝试此代码时不断收到“发送请求时出错”,无法找到问题所在,我已按照建议使用了服务器api密钥。我使用了一个带有两个文本框的表单,该文本框将被定向到上面的另一个文件。

代码语言:javascript
运行
复制
 <?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>
?>

有人能在这方面帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-04-11 09:42:47

不确定是不是代码中的拼写错误,但您需要调整PHP代码-您已经将HTML包装在<?php ?>段中。

尝试在HTML启动之前退出PHP段,如下所示;

代码语言:javascript
运行
复制
?>
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49758563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档