Palani kumar
Palani kumar

Reputation: 119

I'm not able to push the notification

I'm not able to push the notification.
my response is :

"{\"multicast_id\":8319582670472068768,\"success\":0,\"failure\":3,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"}]}"

code:

public function sendNotification($registration_ids,$message){


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

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

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

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($curl);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($curl));
    }

    error_log("Response from GCM Server:".$result);

    curl_close($curl);
    return $result;

Upvotes: 2

Views: 552

Answers (1)

KelvZhan
KelvZhan

Reputation: 140

This issue usually occurs if your API key is invalid. What status code is the response? You can read more here: https://developers.google.com/cloud-messaging/http#checkAPIkey

If you receive a 401 HTTP status code, your API key is not valid. Otherwise you should see something like this:

{"multicast_id":6782339717028231855,"success":0,"failure":1, "canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

It may also be more convenient for you to use a Laravel package which handles GCM messaging for you: https://github.com/davibennun/laravel-push-notification/

Please let me know what status code you are receiving.

Upvotes: 2

Related Questions