更新:GCM已弃用,使用FCM
如何在PHP后端集成新的谷歌云消息传递?
更新:GCM已弃用,使用FCM
如何在PHP后端集成新的谷歌云消息传递?
当前回答
在搜索了很长一段时间后,我终于能够弄清楚我到底需要什么,连接到GCM使用PHP作为服务器端脚本语言,下面的教程将让我们清楚地了解如何设置我们需要开始使用GCM的一切
Android推送通知使用谷歌云消息(GCM), PHP和MySQL
其他回答
许多教程都过时了,甚至当前的代码都没有考虑设备注册id何时更新或设备取消注册。如果不检查这些项目,最终将导致无法接收消息的问题。http://forum.loungekatt.com/viewtopic.php?t=63#p181
这很容易做到。Elad Nava放在这里的页面上的cURL代码是有效的。Elad评论了他收到的错误。
描述在为该收件人处理消息时发生的错误的字符串。可能的值与上表中记录的值相同,再加上“Unavailable”(意味着GCM服务器繁忙,无法为特定的收件人处理消息,因此可以重试)。
我已经得到了一个服务设置,似乎已经工作(ish),到目前为止,我已经回来的都是不可用的回报从谷歌。这种情况很可能很快就会改变。
要回答这个问题,请使用PHP,确保Zend框架在您的include路径中,并使用以下代码:
<?php
ini_set('display_errors',1);
include"Zend/Loader/Autoloader.php";
Zend_Loader_Autoloader::getInstance();
$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "YOUR API KEY AS GENERATED IN API CONSOLE";
$reg = "DEVICE REGISTRATION ID";
$data = array(
'registration_ids' => array($reg),
'data' => array('yourname' => 'Joe Bloggs')
);
print(json_encode($data));
$client = new Zend_Http_Client($url);
$client->setMethod('POST');
$client->setHeaders(array("Content-Type" => "application/json", "Authorization" => "key=" . $serverApiKey));
$client->setRawData(json_encode($data));
$request = $client->request('POST');
$body = $request->getBody();
$headers = $request->getHeaders();
print("<xmp>");
var_dump($body);
var_dump($headers);
结果出来了。一个在Zend Framework PHP中使用google新GCM的工作示例(很快就会工作)。
你可以在packagist上使用这个PHP库:
https://github.com/CoreProc/gcm-php
安装后,你可以这样做:
$gcmClient = new GcmClient('your-gcm-api-key-here');
$message = new Message($gcmClient);
$message->addRegistrationId('xxxxxxxxxx');
$message->setData([
'title' => 'Sample Push Notification',
'message' => 'This is a test push notification using Google Cloud Messaging'
]);
try {
$response = $message->send();
// The send() method returns a Response object
print_r($response);
} catch (Exception $exception) {
echo 'uh-oh: ' . $exception->getMessage();
}
这段代码将通过PHP CURL向多个注册id发送一个GCM消息。
// Payload data you want to send to Android device(s)
// (it will be accessible via intent extras)
$data = array('message' => 'Hello World!');
// The recipient registration tokens for this notification
// https://developer.android.com/google/gcm/
$ids = array('abc', 'def');
// Send push notification via Google Cloud Messaging
sendPushNotification($data, $ids);
function sendPushNotification($data, $ids) {
// Insert real GCM API key from the Google APIs Console
// https://code.google.com/apis/console/
$apiKey = 'abc';
// Set POST request body
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
// Set CURL request headers
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Initialize curl handle
$ch = curl_init();
// Set URL to GCM push endpoint
curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send');
// Set request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// Set custom request headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the response back as string instead of printing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set JSON post data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
// Actually send the request
$result = curl_exec($ch);
// Handle errors
if (curl_errno($ch)) {
echo 'GCM error: ' . curl_error($ch);
}
// Close curl handle
curl_close($ch);
// Debug GCM response
echo $result;
}
<?php
// Replace with the real server API key from Google APIs
$apiKey = "your api key";
// Replace with the real client registration IDs
$registrationIDs = array( "reg id1","reg id2");
// Message to be sent
$message = "hi Shailesh";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$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);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>