2016-07-23 124 views
1

我想从服务器使用firebase云消息传递系统向我的android设备发送推送通知。我能够成功注册我的设备并为我的设备生成令牌。我无法使用下面的脚本Android推送通过firebase通知(服务器端)

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

?> 

将通知发送到我的设备现在,只要我打的剧本,只是响应返回

To 

,并没有别的..并没有通知交付以及。无法找出问题。请求你的指导。

感谢

回答

1

尝试使用“到”而不是“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); 

希望帮助

+0

Thanks.much。但没有运气将registration_ids中的值更改为“To”。我尝试使用你的方法和邮递员给我字段“数据”必须是一个JSON数组: – Sriram

+0

这意味着有问题在cretering json – Killer

+0

道歉,你能帮我在这里请 – Sriram

0

感谢@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); 

?> 

到 - 单收件人
registration_ids - 多播消息(许多收件人 - 应在阵列)

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