0

我正在尝试使用 FCM 和 PHP 中的 curl 函数向多个用户发送通知。有时它发送完美,有时它重新执行整个页面并插入两次值并发送两次通知。

这是我的通知代码

$response = sendNotification(
                    $apiKey,
                    $reg_id1,
                    array(
                        'message'           => $name,
                        'title'             => $serviceName,
                        'subtitle'          => $serviceName,
                        'NotificationType'  => 'Notify',
                        'vibrate'           => 1,
                        'sound'             => 1,
                        'largeIcon'         => 'large_icon',
                        'smallIcon'         => 'small_icon'
                    )
                );
                echo $response;

function sendNotification( $apiKey, $registrationIdsArray, $messageData ){
    $headers = array("Content-Type: application/json", "Authorization: key=" . $apiKey);
    $data = array(
        'registration_ids' => $registrationIdsArray,
        'data' => array('data' => $messageData)
    );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;

}
4

1 回答 1

0

我有同样的问题,我通过起诉以下代码解决了它:

define('API_ACCESS_KEY', 'ftfh');
        $title = $_GET['title'];
        $body = $_GET['body'];

$fields = array(
        "registration_ids" => $registrationIds_array,
        "notification" => array(
             "body" => $body,
              "title" => $title,
               "icon" => $appicon,
        ),
        "data" => array(
             "isUrl" => true,
             "url" => "https://www.amazon.in/",
             "isMsg" => false,
             "msg"=>"Hello Swapnil..."
        )
            );

  $fields= json_encode($fields);

        $headers = array
            (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
#Send Reponse To FireBase Server    
        $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, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ( $msg));
        curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds

        $result = curl_exec($ch);
        curl_close($ch);
于 2017-11-04T08:11:34.947 回答