2016-12-05 73 views
2

我剛剛在我的實時網絡服務器中開發了一個帶有應用程序服務器的firebase示例。將Firebase Cloud Messaging發送到手機設備

我在本地PC上(離線)嘗試了它,並且在虛擬設備android上發送了firebase通知。

但是,當我嘗試將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> 
+0

必須設置服務器密鑰這裏?? –

+0

您嘗試從服務器到虛擬設備還是從本地主機到真實設備?爲了確定問題出在服務器上,不是android設備 – X3Btel

+0

已經在那裏設置了服務器密鑰。 :) ..我只是不想顯示服務器密鑰 – Jamilah

回答

0
<?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); 
?> 
+0

它不起作用。對不起 – Jamilah

+0

你得到哪個錯誤? –

+0

輸出顯示{「message_id」:7946875986945781340} – Jamilah

-1

去你的火力點安慰的項目設置>選擇第二個選項卡「雲消息」就不會有你的服務器密鑰,您的需要在你的PHP文件;)

相關問題