我的Android應用程序可以從Firebase控制檯接收FCM,但不是來自我的應用程序服務器。Android應用程序無法從我的應用程序服務器接收到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"
}
]
}
添加PHP的標誌,因爲大多數人使用開發Android其他語言。我不知道它是如何工作的,但也許一個日誌應該可以幫助他人瞭解什麼是錯誤的並給你解決方案。 –
嗨。從Firebase控制檯發送消息時,它將被視爲「通知」有效內容。在你的PHP腳本中,你只發送一個「數據」消息負載。查看兩個[這裏]的區別(https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages)。您可能只處理客戶端應用程序中的「通知」消息。你可以發佈你的Android端代碼嗎? –
@AL。謝謝 !我忽略了那部分。我在這裏找到了我的答案。 –