我需要用于将Firebase推送通知发送到Android的php脚本

时间:2018-07-13 18:14:31

标签: php firebase

我正在尝试使用Firebase和PHP将推送通知发送到Android。 我已经获得了设备ID,但无法从php脚本发送消息,我不知道我的错误在哪里。但是我可以使用该设备ID从Firebase控制台发送通知。任何人都可以帮我修复此php脚本???

 $title = $_POST["title"];
 $message = $_POST["messege"];
 $uid = $_POST["uid"];
 $fID = mysqli_fetch_array(mysqli_query($c_m_db, "SELECT firebaseId FROM      tablename WHERE uid='".$uid."'"));
 $fIDD = $fID[0];
 $url = 'http://fcm.googleapis.com/fcm/send';
 $key = "AIz***********4I";

 $headers = array(
'Authorization: Bearer='.$key,
'Content-type: application/json'
);
 $fields = array('notification'=>array('title'=>$title,'body'=>$message),'registration_ids'=>$fIDD);

 $payload = json_encode(array(messege=>$fields));

 $curl_session = curl_init();
 curl_setopt($curl_session, CURLOPT_URL, $url);
 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($c_m_db);

1 个答案:

答案 0 :(得分:0)

您可能会遇到授权失败的情况。您正在使用旧版API。授权标头使用key=代替Bearermigration guide中有一个旧标题和新标题的示例。

更改:

'Authorization: Bearer='.$key

收件人:

'Authorization: key='.$key