abandroid / gcm

Google Cloud Messaging
http://endroid.nl/
MIT License
47 stars 14 forks source link

Issue while receiving google response after sending push mensage. #8

Open DMS007 opened 8 years ago

DMS007 commented 8 years ago

Hi,

Im very new in GitHub so Im not sure if this section is the right one for asking this.

I used the lib endroid/Gcm following a android tutorial and everything worked fine, but when getting the answer from google i got this error:

[10-Nov-2015 12:20:32 Etc/GMT] PHP Fatal error: Call to undefined method Buzz\Client\FileGetContents::flush() in /home/cicloyjb/public_html/android/gcm/vendor/endroid/gcm/src/Client.php on line 89

I installed the lib into my project using composer.

Do you have any idea of what can cause this?

Thanks in advance.

endroid commented 8 years ago

Hi @DMS007 did you find a solution yet? The flush command should be executed by the MultiCurl client instead of the FileGetContents class. When I look at the Browser class (https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Browser.php) I see that when the constructor does not receive a client it assigns FileGetContents class. So it looks like your code has troubles instantiating the MultiCurl class (which is explicitly passed to the constructor by the GCM client), resulting in a null value. I see the constructor executes a curl command, maybe something goes wrong there. Do you have libcurl installed? To be sure I just added the extension to the requirements in composer in the latest version.

DMS007 commented 8 years ago

Hi endroid, I couldn't find any solution actually. I'm a beginner in Android and PHP programming, so I'm not able to check the error log, read other's programs and come up with a solution yet.

I followed what you said and updated with composer the endroid package. The previous problem didn`t show up but this one appear instead:

[24-Jan-2016 14:40:04 Etc/GMT] PHP Fatal error: Uncaught exception 'Buzz\Exception\RequestException' with message 'file_get_contents(https://android.googleapis.com/gcm/send): failed to open stream: operation failed' in /home/cicloyjb/public_html/android/gcm/vendor/kriswallsmith/buzz/lib/Buzz/Client/FileGetContents.php:28 Stack trace:

0 /home/cicloyjb/public_html/android/gcm/vendor/kriswallsmith/buzz/lib/Buzz/Browser.php(130): Buzz\Client\FileGetContents->send(Object(Buzz\Message\Request), Object(Buzz\Message\Response))

1 /home/cicloyjb/public_html/android/gcm/vendor/kriswallsmith/buzz/lib/Buzz/Browser.php(82): Buzz\Browser->send(Object(Buzz\Message\Request))

2 /home/cicloyjb/public_html/android/gcm/vendor/kriswallsmith/buzz/lib/Buzz/Browser.php(36): Buzz\Browser->call('https://android...', 'POST', Array, '{"collapse_key"...')

3 /home/cicloyjb/public_html/android/gcm/vendor/endroid/gcm/src/Client.php(97): Buzz\Browser->post('https://android...', Array, '{"collapse_key"...')

4 /home/cicloyjb/public_html/android/gcm/package/apl/AplUser.php(48): Endroid\G in /home/cicloyjb/public_html/android/gcm/vendor/kriswallsmith/buzz/lib/Buzz/Client/FileGetContents.php on line 28

I'm taking my weekends reading all the codes trying figure what is happening out. But I doubt I'll be able to solve.

Let me know if you have any hint about it. I would really like to get GCM working properlly.

endroid commented 8 years ago

@DMS007 Apparently the MultiCurl client is still not used... Can you attach your composer.json and composer.lock files here?

DMS007 commented 8 years ago

Hi @endroid! In my project file (/gcm) I have the following

Then i have a composer.json and in /gcm/vendor/endroid/gcm { "name": "endroid/gcm", "description": "Endroid Google Cloud Messaging", "keywords": ["endroid", "gcm", "push", "notification", "notifications", "google", "cloud", "messaging"], "homepage": "https://github.com/endroid/Gcm", "type": "library", "license": "MIT", "authors": [ { "name": "Jeroen van den Enden", "email": "info@endroid.nl", "homepage": "http://endroid.nl/" } ], "require": { "php": ">=5.3.0", "ext-curl": "*", "kriswallsmith/buzz": "~0.3" }, "autoload": { "psr-4": { "Endroid\Gcm\": [ "src/", "tests/" ] } }, "config": { "bin-dir": "bin" }, "extra": { "branch-alias": { "dev-master": "1.x-dev" } } }

And another composer.json in gcm/vendor/kriswallsmith/buzz

{ "name": "kriswallsmith/buzz", "description": "Lightweight HTTP client", "keywords": ["http client", "curl"], "homepage": "https://github.com/kriswallsmith/Buzz", "type": "library", "license": "MIT", "authors": [ { "name": "Kris Wallsmith", "email": "kris.wallsmith@gmail.com", "homepage": "http://kriswallsmith.net/" } ], "require": { "php": ">=5.3.0" }, "require-dev": { "phpunit/phpunit": "3.7." }, "suggest": { "ext-curl": "" }, "config": { "bin-dir": "bin" }, "autoload": { "psr-0": { "Buzz": "lib/" } } }

Is there anything wrong with them?

Really thank you for your attention.

DMS007 commented 8 years ago

@endroid I searched for a simple code to send the cURL POST request to check if my gcm code was working and everything worked pretty fine.

The code is that one: <?php $url = 'https://android.googleapis.com/gcm/send';

$message = array(
                'title' => "Titulo",
                'message' => "Msg Area",
        );

$fields = array(
        'registration_ids' => array("fucmnuw-eyM:APA91bGqG97ZkfK2jGb7MUv8mYhJfLuvIMmFPLZcrBVtxtEe9gSkFXKeBKDRya4g8kt2_8SannHeOvQ_PnK2FmgSGu7vaHt5_isem7SbiXblXSy4CRA5jQNxFY5uoVfC2AdFfDywcwqa"),
        'data' => $message,
    );

define("GOOGLE_API_KEY", <SECRET_KEY>);         
    $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_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);   

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

echo $result;

?>

So I believe cURL is working good.

It might be something related to the content of $request variable and $context

When $content = file_get_contents($url, 0, $context); it gives the error as $context = 'Resource id #32'

But when I changed $context for $request, considering that $request = POST /gcm/send HTTP/1.1 Host: https://android.googleapis.com Authorization: key= Content-Type: application/json {"collapse_key":"myTestPushMessage","delay_while_idle":false,"time_to_live":2419200,"restricted_package_name":"com.agendacoruja.diego.coruja","dry_run":false,"data":{"title":"Title","message":"Message area."},"registration_ids":["fucmnuw-eyM:APA91bGqG97ZkfK2jGb7MUv8mYhJfLuvIMmFPLZcrBVtxtEe9gSkFXKeBKDRya4g8kt2_8SannHeOvQ_PnK2FmgSGu7vaHt5_isem7SbiXblXSy4CRA5jQNxFY5uoVfC2AdFfDywcwqa"

It doesn't gives any error but yet the response is null for some reason I couldn't check.

Response = array(1) { [0]=> object(Buzz\Message\Response)#12 (5) { ["protocolVersion":"Buzz\Message\Response":private]=> NULL ["statusCode":"Buzz\Message\Response":private]=> NULL ["reasonPhrase":"Buzz\Message\Response":private]=> NULL ["headers":"Buzz\Message\AbstractMessage":private]=> array(0) { } ["content":"Buzz\Message\AbstractMessage":private]=> NULL } }

And the push notification is not sent.

endroid commented 8 years ago

I honestly have no clue. Maybe it has something to do with multi curl only, as your example uses regular curl. Maybe you could try to see if the works when you use curl_multi_init or even try to replace the Buzz client in my code with a Guzzle client (this shouldn't be too much work) to see if that library is giving any more clues on why you don't get a proper response?