我想發送FCM通知使用PHP腳本,數據庫插入事件,我試圖從FCM控制檯發送,它使用主題選項正常工作,知道我註冊了應用程序實例某些主題「exptopic」 使用下面的代碼在MainActivity.java的OnCreate類:FCM通知爲Android
FirebaseMessaging.getInstance().subscribeToTopic("exptopic");
現在PHP腳本如下:
function sendMessage($data,$target){
//FCM api URL
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase console -> project settings -> cloud messaging -> server key
$server_key = "API_KEY";//my api key
$fields = array();
$fields['data'] = array('message'=>$data);
$fields['to'] = $target;
$headers = array('content-type:application/json',
'Authorization:key='.$server_key);
$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('FCM Send error: '.curl_error($ch));
echo 'notification not sent';
}
curl_close($ch);
echo $result;
return $result;
}
顯然$target = "exptopic"
,當我運行snedMessage函數,它返回以下結果:
{"multicast_id":6779996340041741184,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
任何人都可以幫忙嗎?
MySQL的似乎不相關這一問題,標籤去除。 – tadman