0

I want to send push notification from Application.I can get token from firebase cloud messaging.I can use code in Appserv But I can't use from server online , server online ERROR HTTP ERROR 500 This is My Code

 <?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;

    ?>
AL.
  • 36,815
  • 10
  • 142
  • 281

1 Answers1

0

Error 500 indicates an Internal Server Error. Either you're experiencing what is mentioned here or the issue is the the FCM, in which case you must (as per the docs):

Errors in the 500-599 range (such as 500 or 503) indicate that there was an internal error in the FCM connection server while trying to process the request, or that the server is temporarily unavailable (for example, because of timeouts). Sender must retry later, honoring any Retry-After header included in the response. Application servers must implement exponential back-off.

AL.
  • 36,815
  • 10
  • 142
  • 281