2016-07-23 121 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 - 多播消息(許多收件人 - 應在陣列)

這可能不是我的理想解決方案,但它絕對有效:)