Android 通过 firebase 推送通知(服务器端)

Android push Notification through firebase (server side)

我正在尝试使用 firebase 云消息系统从服务器向我的 android 设备发送推送通知。我能够成功注册我的设备,并且还为我的设备生成了令牌。我无法使用以下脚本向我的设备发送通知

<?php

function send_notification($token1,$message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array('registration_ids'=>$token1,'data'=>$message);
    $header = array('Authorization:key = <my key here>','Content-Type: application/json');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if($result===false)
    {
        die('Curl failed'.curl_error($ch));
    }
    curl_close($ch);

    return $result;

}

require "init.php"; //my db details here

$sql = "select token from fbuser";

$result = mysqli_query($con,$sql);
$tokens = array();

if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_assoc($result))
    {
        $tokens[] = $row["token"];   // i was able to successfully echo out token as well
    }
}

mysqli_close($con);

$message = array("message"=> "This is a test message"); 
$message_status = send_notification($tokens,$message);
echo $message_status; ***//Trouble here. it just prints out "To" whenever i hit the php script***

?>

现在每当我点击脚本时,响应只是 returns

To

仅此而已.. 也没有发送通知。无法弄清楚问题所在。请求您的指导。

谢谢

尝试使用'to'代替'registration_ids'

这是保存在邮递员历史中的正文数据[我不记得是否有效]:

{ "data": {
"score": "5x1",
"time": "15:10" }, "to" : "token_id"}

检查这个脚本:如果你想做主题消息

$url = "https://fcm.googleapis.com/fcm/send";
                $topic = "topic_name";
                $fields = array(
                    "to"                => "/topics/".$topic,
                    "data"              => $message,
                    "time_to_live"      => 30,
                    "delay_while_idle"  => true
                );

                $auth_key = "auth_key";

                $headers = array(
                    "Authorization: key=".$auth_key,
                    "Content-Type: application/json"
                );

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);

希望有所帮助

非常感谢@Shubham 提供的解决方案。

大家请使用@Shubham 的代码向“主题”发送通知

我稍微修改了他的代码以解决单一收件人消息传递问题。开始了

<?php

$url = "https://fcm.googleapis.com/fcm/send";
                $val = "<your recipient id>";
                $message = array("message" => "This is a test message from server");
                $fields = array(
                    "to"                => $val,
                    "data"              => $message,
                    "time_to_live"      => 30,
                    "delay_while_idle"  => true
                );

                $auth_key = "<Your auth key>";

                $headers = array(
                    "Authorization: key=".$auth_key,"Content-Type: application/json"
                    );

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);

?>

to - Single recipient
registration_ids - multicast messages (Many recipients - should be in array)

从我的角度来看,这可能不是理想的解决方案,但它绝对有效:)