Where is the key to use for Android FCM push notification?

Viewed 161

I installed firebase, and if I send manually push notification through firebase console, my app receives it.

I'm trying to send a notification to FCM using a php script.

The problem is I don't know what key I'm supposed to use ?

If I use the one in firebase console, I got this 401 error: "Invalid Key"

enter image description here

If I use the one in Api & Services, I got this 401 error "PROJECT_NOT_PERMITTED"

enter image description here

This is the php script I'm using:

$server_key = ""
$token = ""
$title = ""
$body = ""
$curl = curl_init();
    $authKey = "key=" . $server_key;
    $registration_ids = $token;
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => '{
                    "registration_ids": ' . $registration_ids . ',
                    "notification": {
                        "title": ' . $title . ',
                        "body": ' . $body . '
                    }
                }',
    CURLOPT_HTTPHEADER => array(
        "Authorization: " . $authKey,
        "Content-Type: application/json",
        "cache-control: no-cache"
    ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo "no error: " . $response;
    }

Of course, it's printing my echo in else statement:

"no error: 401 ..."

Does someone can help me?

1 Answers

Try to enable Cloud Messaging API (Legacy) in Firebase Console project settings.

Related