Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of InternalServerError in sending Remote Push Notification to iOS using GCM in PHP

I tried to implement Google Cloud Messaging (GCM) for iOS in my App. I tried the codes provided on the pod GcmExample Xcode Project and wrote a PHP script the will send the request via PHP cURL.

My PHP code is as follows:

gcmPush(
    "kU6GAbdQEg0:APA91bGSF42WITzubJ9lwQ2qUqL4ETtZfQEOthazc4tP1CRhxx8tKSRChwgtFK37-kt7RjG2P_Ncj2bDOONpPVsYkjxCNi3NDhMNWSkJ7pnlR83jdbMJfOKlD1CPXCgAvZjbwpzdeiTa", 
    "This is only a test from GCM for iOS.", 
    "PHP test"
    );

function gcmPush($GCM_regID, $message, $title) {

    $GOOGLE_API_KEY = "AIzaSyAd8pw7_J8i-FPswnhUMkFCDHtufSCFFTc";
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'to' => $GCM_regID,
        'notification' => array(
            'sound' => 'default',
            'badge' => '1',
            'body' => $message,
            'title' => $title
        )
    );

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

    $result = curl_exec($ch);
    echo "\r\n";
    if ($result === FALSE) {
        die('Failed: ' . curl_error($ch) . "\r\n");
    } else {

        echo "Success\r\n";   
    }

    curl_close($ch);
}

The request is successful, however the result from the Google GCM server contains the following:

{"multicast_id":6164898138439370127,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InternalServerError"}]}

Is there anything wrong with the Google API Key? I tried this script to send Remote Notification to the GcmExample.xcworkspace and it worked. I would like to know how to resolve this.

like image 478
Adro Avatar asked Mar 03 '16 07:03

Adro


People also ask

What is GCM push notification?

Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...

What is APNs and GCM?

From this thread, GCM is a service that helps developers send data from servers to their Android applications on Android devices. Same with APN which is a service for app developers to propagate information to iOS (and, indirectly, watchOS), tvOS, and macOS devices.

Why do we need GCM?

GCM can take full advantage of parallel processing and implementing GCM can make efficient use of an instruction pipeline or a hardware pipeline. By contrast, the cipher block chaining (CBC) mode of operation incurs pipeline stalls that hamper its efficiency and performance.


1 Answers

If your encounter this error message in GCM for iOS {"error":"InternalServerError"}, Make sure you uploaded the correct P12 certificate in the Google Developer Console. And Make sure the Provisioning Profile is setup correctly for the Signing Certificate and set the following as follows:

  • For Development:

    @{kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};

  • For Production / Distribution:

    @{kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:@NO};

like image 123
lance_andrew Avatar answered Sep 18 '22 10:09

lance_andrew