无法使用主题消息发送通知

时间:2019-12-18 13:58:16

标签: php

无法使用主题消息发送通知。在这里,我附上了代码。请检查一下。它对邮递员运行良好。但是无法收到通知。

$topic = $_POST['topic'];
$headers = [
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
];

 if($_POST['send_to']=='topic'){
    $fcmNotification = array(
        'to' => '/topics/'.$topic,
        'notification' => $dataToSentNotification,
    );
 }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$results = curl_exec($ch);
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

使用curl_errno function来获取特定的错误,代码如下:

$error_msg = NULL;
if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
}
curl_close($ch);
if ($error_msg !== NULL) {
    print_r($error_msg);
}

正如 aynber 所说,将整个curl句子添加到if内,或者如果$fcmNotification为空或为空,您会有什么期望?