gpt4 book ai didi

php - Android 的 FCM 通知

转载 作者:行者123 更新时间:2023-11-30 01:03:21 24 4
gpt4 key购买 nike

我正在尝试使用 PHP 脚本发送 FCM 通知,在数据库插入事件中,我尝试从 FCM 控制台发送,它使用主题选项工作正常,知道我将应用程序实例注册到某个主题“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

关于php - Android 的 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39172861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com