php - 将 Firebase Cloud Messaging 发送到手机设备

标签 php android curl firebase firebase-cloud-messaging

我刚刚在我的实时网络服务器中开发了一个带有应用程序服务器的 firebase 示例。

我在我的本地主机 pc 上试过(离线)并且 firebase 通知是在虚拟设备 android 上发送的。

但是当我尝试将 apk 文件安装到我的手机设备并将 PHP 文件传输到我的实时服务器(即 www.aaa.com)时。

Firebase 通知根本不起作用。它不会发送到我的手机设备。

我是否遗漏了代码中的某些内容? .代码如下。谢谢。

<?php
require "connect.php";
global $con;
    if(isset($_POST['Submit'])){
        $message     = $_POST['message'];
        $title       = $_POST['title'];
        $path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; 
        $server_key  = "SERVER KEY HERE";
        echo "Data sent ! <br/>";

        $sql = "SELECT fcm_token FROM fcm_info";
        $result = mysqli_query($con, $sql);
        $row = mysqli_fetch_row($result);
            $key = $row[0];
        $headers = array('Authorization:key=' .$server_key, 'Content-Type:application/json');
        $fields  = array('to' => $key, '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);
        mysqli_close($con);
    }   
?>
<!DOCTYPE html>
<html>
<head>
    <title>Send Notification</title>
</head>
<body>
    <form action='send_notification.php' method="POST">
        <table>
            <tr>
                <td>Title : </td>
                <td><input type="text" name="title" required="required" /></td>
            </tr>
            <tr>
                <td>Message : </td>
                <td><input type="text" name="message"  required="required" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="Submit" value="Send notification"></td>

            </tr>
        </table> 
    </form>
</body>
</html>

最佳答案

<?php

$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header=array('Content-Type: application/json',
"Authorization: key=YOUR KEY");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"notification\": {    \"body\": \"Weather Forecast\",
    \"title\": \"YOur title\",
    \"to\" : \"YOUR DEVICE REG TOKEN\"}"

curl_exec($ch);
curl_close($ch);
?>

关于php - 将 Firebase Cloud Messaging 发送到手机设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971227/

相关文章:

php - 在一个mysql查询中查询两个表

macos - 在 OSX 上对 curl 的 SFTP 支持

javascript - 如何调试 ajax REST 调用(一般)

javascript - 在 URL 中传递 Javascript 变量

php - 从 PHP 中的自定义错误处理程序中抛出异常

java - 将数据从一项 Activity 解析到另一项 Activity

安卓 8 : Cleartext HTTP traffic not permitted

java - Android - 使用事件创建自定义日历

python - 如何检测 GET 请求是否来自浏览器

javascript - 如何在单击复选框时使用数据库中的值填充输入文本字段