sid
sid

Reputation: 1116

Getting issue with Firebase push notification Android

I just integrated Push notification inside my application with Firebase. I have read the whole instruction in the FCM docs.

It just auto-generated the server key and provided me the google-services.json file which I placed inside the root folder of application. Below is the screenshot of the keys.

Then I just added my package name and SHA1-Fingerprints with selection on Android app under Key Restrictions. I have also enabled GCM in the console.

The thing that's been driving me crazy is when I send a notification from the console, I receive it, but when I tried to send it from my server, it shows me error 401 Unauthorized.

So do I need to change my code in Back-end? I am using server key in back- end to send push notification, do I need to send the sender id to back-end too? Did Google change it's mechanism?

My back end code for FCM:

$fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "YOUR_KEY_HERE",
            '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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );

For GCM:

static function send_notifications($registatoin_ids, $messages) { 

        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $messages,
        );
        $apiKey='AIzaSyDzto66Dq0neU3AKjujr0eCEn2glWuk0-E';

        $headers = array(
            //'Authorization: key = AIzaSyBpZ3t68mtXRa4oRft9fmbaHaXFb-H_k-8',
             'Authorization: key=' . $apiKey,
            'Content-Type: application/json'
        );

        $ch = curl_init();


        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        //echo "<pre>";
//print_r($result);die;
        if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
    }

Thanks.

enter image description here

Upvotes: 1

Views: 641

Answers (2)

AL.
AL.

Reputation: 37778

You mentioned

Then I just added my package name and SHA-Fingerprints with selection on Android app under Key Restrictions. I have also enabled the GCM in the console.

With the new interface in the Google Developers Console, it seems there is no longer an option to select the type of API key you want to generate, however, you can still provide some restrictions. Basing from what you said, since you selected a restriction for Android, the API key is being treated as an Android Key.

The thing about FCM (or even GCM) is that the API key that must be used is a Server Key. What I suggest is try to change the restriction to IP server addresses, adding your server IP address, then test out a push notification.

Upvotes: 1

sid
sid

Reputation: 1116

It's start working now , i just found a link as i thought i should share that with you all :- https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol

Upvotes: 0

Related Questions