2016-12-23 157 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