我已经实现了 FCM 服务器端,服务器端未交付



我正在尝试实现一个php curl fcm主题广播,它只显示{"message_id":4731721763997571462}而不提供。

我经历了很多搜索,没有尽头,我似乎找不到问题所在。

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
// prep the bundle
$msg = array
(
'message'   => 'here is a message. message',
'title'     => 'This is a title. title',
);
$fields = array
(
'to'  => "/topics/hello",
'notification'            => $msg
);
$headers = array
(
'Authorization:key='. API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
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_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

我只是希望在设备上收到通知,也许有一种方法可以在谷歌控制台上跟踪成功的通知?

您需要使用字段参数添加注册 ID

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
$registrationIds = array( "/topics/obajemusagmailcom" );
// prep the bundle
$msg = array
(
'message'   => 'here is a message. message',
'title'     => 'This is a title. title',
);
$fields = array
(
//'to'  => "/topics/hello",
'registration_ids' => $registrationIds,
'notification'            => $msg
);
$headers = array
(
'Authorization:key='. API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
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_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

和 $registrationIds = array( "/topics/obajemusagmailcom" (; 不是任何应用程序的注册ID,请更正它。

最后,经过多次恐怖和尝试,结果发现数组字段不正确,本应是

$msg = array
(
'body'   => $body,
'title'=>$title
);
$data = array
(
'to'  => "/topics/".$topic,
'notification' => $msg
);

最新更新