如何修复PHP发送推送通知ERROR HTTP ERROR 500

时间:2018-04-09 10:41:45

标签: php android firebase push-notification firebase-cloud-messaging

我想从Application发送推送通知。我可以从firebase云消息中获取令牌。我可以在Appserv中使用代码但我不能在服务器上在线使用,服务器在线ERROR HTTP ERROR 500这是我的代码

 <?php 

    function send_notification ($tokens, $message)
    {
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );

        $headers = array(
            'Authorization:key = key ',
            'Content-Type: application/json'
            );

       $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('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
    }
    $conn = mysqli_connect("localhost","user","pass","db");

    $User_id = $_POST['User_id'];
    $sql = "SELECT * FROM user_token";

    $result = mysqli_query($conn,$sql);
    $tokens = array();

    if(mysqli_num_rows($result) > 0 ){

        while ($row = mysqli_fetch_assoc($result)) {
            $tokens[] = $row["Token"];
        }
    }

    mysqli_close($conn);

    $message = array("message" => " SendNotification Txt");
    $message_status = send_notification($tokens, $message);

    echo $message_status;

    ?>

1 个答案:

答案 0 :(得分:0)

错误500表示Internal Server Error。要么您正在体验所提到的内容here,要么问题是FCM,在这种情况下您必须(根据docs):

  

500-599范围内的错误(例如500或503)表示尝试处理请求时FCM连接服务器中存在内部错误,或者服务器暂时不可用(例如,由于超时) )。发件人必须稍后重试,以兑现响应中包含的任何Retry-After标头。应用程序服务器必须实现指数退避。