我的Android应用可以很好地从Firebase控制台接收FCM,但不能从我的应用服务器接收。
这是我的PHP脚本的片段。
$target = 'device code';
$data = array('message' => 'Hello','title'=>'FCM Push Notifications');
function sendFCMMessage($data,$target){
//FCM API end-point
$url = 'https://fcm.googleapis.com/fcm/send';
$server_key = 'server key';
$fields = array();
$fields['data'] = $data;
$fields['priority'] = 'high';
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = $target;
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
//CURL request to route notification to FCM connection server (provided by Google)
$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_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('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$pn = sendFCMMessage($data,$target);
echo $pn;
但我在运行下面的脚本后确实收到了成功回复。
{
"multicast_id":4696016312851064849,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{
"message_id":"0:1486632797062481%1749d09ff9fd7ecd"
}
]
}
答案 0 :(得分:0)
this is my working code change you php code similar to this but change parrameter
$apiKey = "fjdfhdjskajklsjdsalkdsajkldsakdjksadjksadsa-";
foreach($data as $deviceid)
{
$arydevice[]=$deviceid->gcmregid;
}
//print_r($arydevice);
$count = count($arydevice);
for ($i = 0; $i < $count; $i++) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields=array('to'=>(string)$arydevice[$i] ,'notification'=>array('title'=>'title','body'=>"1",'click_action'=>'click action','icon'=>'ic_stat_final','sound'=>'default','color'=>'#00aff0'),'data'=>array('multicast_id'=>$mid,'success'='0','schoolid'=>$SchoolId,'timestamp'=>$timestamp,'title'=>$title));
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
//print_r($fields);
//print_r($headers);
//die;
//var_dump($payload);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
curl_close($ch);
}}