2016-12-23 90 views
0

由於我的舊項目已整合到GCM中,因此我正在測試Firebase通知,但現在我也在使用Firebase數據庫。Firebase通知PHP腳本

我試圖執行一個PHP腳本,以便發送通知給我的設備之一。我能夠獲得TokenID也是我能夠將通知發送給從火力地堡控制檯由此看來,但是當我使用PHP腳本,我有以下錯誤:

Invalid (legacy) Server-key delivered or Sender is not authorized to perform request.

這裏是我的腳本:

$message = $_POST['message']; 
$title = $_POST['title']; 
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; 
$server_key = "AAAXXXXXX"; 
$key = "c43xxxxxxxxx"; 


$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_HTTPHEADER, $headers); 
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload); 
curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, "POST"); 

$result = curl_exec($curl_session); 
curl_close($curl_session); 

echo $result; 

我從獲取API密鑰:

Firebase console > gear icon > proyect configuration > Cloud messaging 
+2

我們如何拼寫「授權?」 – miken32

+1

什麼是PHP中的變量的簽名? – miken32

+0

是「Autorization是錯誤的」,我改爲「授權」,現在我收到此錯誤/////無效(遺留)服務器密鑰交付或發件人無權執行請求。 – user3515652

回答

0
$headers = array (
'Authorization: key =' . server_key, 
'Content-Type:application/json' 

);

你應該使server_key變量像$ server_key