Tags
Android
Asked 3 years ago
5 Oct 2021
Views 3765
Winifred

Winifred posted

Error 401 INVALID_KEY_TYPE FireBase with PHP


   $url = "https://fcm.googleapis.com/fcm/send";
    $token = $devicetoken;
    $serverKey = 'BVhFT_kT43i-DHZt6fQBVhFT_kT43i-DHZt6fQ';
    $title =$title;
    $body = $body;
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    print_r($response);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);


i am getting error : INVALID_KEY_TYPE so not sure what is wrong in the firebase API call for FCM notification
chirag

chirag
answered Oct 5 '21 00:00

You are using the wrong key.
you should get the key from the Project > Settings > Cloud Messaging > Server Key from firebase console.
andy

andy
answered Oct 5 '21 00:00

I tested your code. and it works well but you using the key from the Project settings > General which is not the right key to use, so API response show INVALID_KEY_TYPE from the firebase. you should use from Cloud Messaging .
Post Answer