I've recently switched servers and my Google Cloud messaging code no longer works. I do not have much server experience and I've tried to look throughout the internet for an appropriate solution but have been unsuccessful.
I had a GoDaddy server (works) and I've set up and switched to a lamp stack on a linode server. I've must have missed something in server config cause the code that sends gcm's works on the godaddy but doesn't work on the apache linode server.
There are no issues registering the ids but it fails to send the gcm message. I'm sending them using a php script and I know its not the problem.
Is there any settings that need to be set so Google Cloud Messaging can work?
Access log:
###.###.###.### - - [23/Dec/2015:12:47:40 -0500] "POST /test/test.php HTTP/1.1" 500 372 "-" "Mozilla/5.0 (Macin
Error log:
empty
I followed these tutorials:
https://www.linode.com/docs/getting-started
https://www.linode.com/docs/websites/lamp/lamp-on-ubuntu-14-04
https://www.linode.com/docs/databases/mysql/install-mysql-phpmyadmin-on-ubuntu-12-04
https://www.linode.com/docs/security/ssl/ssl-apache2-debian-ubuntu
https://www.linode.com/docs/websites/apache/apache-web-server-ubuntu-12-04
GCM.php
<?php
class GCM {
function __construct() {
}
public function send_notification($registatoin_ids, $message, $google) {
$url = 'https://gcm-http.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . $google,
'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_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;
}
}
?>
test.php
<?php
include_once './gcm.php';
$message = $_POST['msg'];
$reg = $_POST['regid'];
$registation_ids = array();
array_push($registation_ids, $reg);
$googleAPIKey = "GOOGLE_API_KEY";
$gcm = new GCM();
$messages = array("message" => $message, "analyticsId" => "someid");
$result = $gcm->send_notification($registation_ids, $messages, $googleAPIKey);
echo $result;
?>
Turned on php logging and still nothing. I still only get the 500 internal error on the linode server.
I'm going to answer my own question because I got it working.
I never installed PHP curl.
http://jesselau.com/2013/04/26/how-to-add-curl-to-linode-ubuntu/