更新:GCM已弃用,使用FCM
如何在PHP后端集成新的谷歌云消息传递?
更新:GCM已弃用,使用FCM
如何在PHP后端集成新的谷歌云消息传递?
当前回答
这段代码将通过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;
}
其他回答
实际上,我现在在Zend_Mobile树的一个分支中工作:https://github.com/mwillbanks/Zend_Mobile/tree/feature/gcm
这将与ZF 1.12一起发布,但是,它应该会给你一些关于如何做到这一点的很好的例子。
这里有一个关于它如何工作的快速演示....
<?php
require_once 'Zend/Mobile/Push/Gcm.php';
require_once 'Zend/Mobile/Push/Message/Gcm.php';
$message = new Zend_Mobile_Push_Message_Gcm();
$message->setId(time());
$message->addToken('ABCDEF0123456789');
$message->setData(array(
'foo' => 'bar',
'bar' => 'foo',
));
$gcm = new Zend_Mobile_Push_Gcm();
$gcm->setApiKey('MYAPIKEY');
$response = false;
try {
$response = $gcm->send($message);
} catch (Zend_Mobile_Push_Exception $e) {
// all other exceptions only require action to be sent or implementation of exponential backoff.
die($e->getMessage());
}
// handle all errors and registration_id's
foreach ($response->getResults() as $k => $v) {
if ($v['registration_id']) {
printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
}
if ($v['error']) {
printf("%s had an error of: %s\r\n", $k, $v['error']);
}
if ($v['message_id']) {
printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
}
}
这很容易做到。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的工作示例(很快就会工作)。
在搜索了很长一段时间后,我终于能够弄清楚我到底需要什么,连接到GCM使用PHP作为服务器端脚本语言,下面的教程将让我们清楚地了解如何设置我们需要开始使用GCM的一切
Android推送通知使用谷歌云消息(GCM), PHP和MySQL
我知道这是一个晚的答案,但它可能对那些想要使用当前FCM格式(GCM已弃用)开发类似应用程序的人有用。 下面的PHP代码用于发送主题播客。所有在上述频道/topis注册的应用程序都会收到这个推送通知。
<?php
try{
$fcm_token = 'your fcm token';
$service_url = 'https://fcm.googleapis.com/fcm/send';
$channel = '/topics/'.$adminChannel;
echo $channel.'</br>';
$curl_post_body = array('to' => $channel,
'content_available' => true,
'notification' => array('click_action' => 'action_open',
'body'=> $contentTitle,
'title'=>'Title '.$contentCurrentCat. ' Updates' ,
'message'=>'44'),
'data'=> array('click_action' => 'action_open',
'body'=>'test',
'title'=>'test',
'message'=>$catTitleId));
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$fcm_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_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($curl_post_body));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
echo 'failure';
}else{
echo 'success' .$result;
}
curl_close($ch);
return $result;
}
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
?>
这是我从CodeMonkeysRU分叉出来的一个库。
我分叉的原因是因为谷歌需要指数回退。我使用redis服务器将消息排队,并在设定的时间后重新发送。
我还将其更新为支持iOS。
https://github.com/stevetauber/php-gcm-queue