我尝试使用PHP脚本发送FCM通知,在数据库插入事件中,我尝试从FCM控制台发送,使用topic选项可以正常工作,知道我已使用以下代码将应用程序实例注册到某个主题“ exptopic”在MainActivity.java OnCreate类中:
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"}]}
有人可以帮忙吗?
最佳答案
基于this documentation,to
字段应为/topics/exptopic