我需要向多个设备发送GCM通知。在这里,我制作了PHP代码以从MySql获取注册ID数组,并尝试向多个设备发送通知,但这里有一些问题。
我的PHP代码:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$tags = $_GET['tags'];
$api_key = 'My OWN API KEY';
//Getting registration token we have to make it as array
//Getting the message
$message = Testing GCM';
$title= 'Cuboid';
$vibrate= '1';
$sound= '1';
require_once('dbConnect.php');
$user_ids = array();
foreach ($_REQUEST['tags'] as $key => $val) {
$user_ids[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}
$tagss = "'" . implode("','", $user_ids) . "'";
$sql = "SELECT user_tags.user_id AS userID , gcm_token.regtoken AS regToken
FROM user_tags,gcm_token
WHERE tags IN ({$tagss}) AND user_tags.user_id=gcm_token.user_id";
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
$reg_token = array();
//looping through all the records fetched
while ($row = mysqli_fetch_array($r)) {
$result['regToken'][] = $row['regToken'];
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
$reg_token = (json_encode(array('result'=>$result)));
//$reg_token = array('result'=>$result);
$msg = array
(
'message' => $message,
'title' => $title,
'subtitle' => 'Android Push Notification using GCM Demo',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => $vibrate,
'sound' => $sound,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
//Creating a new array fileds and adding the msg array and registration token array here
$fields = array
(
'registration_ids' => $reg_token,
'data' => $msg
);
//Adding the api key in one more array header
$headers = array
(
'Authorization: key=' . $api_key,
'Content-Type: application/json'
);
//Using curl to perform http request
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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 ) );
//Getting the result
$results = curl_exec($ch );
curl_close( $ch );
//Decoding json from results
$res = json_decode($results);
mysqli_close($con);
}
更新:
根据更新的文档,用于发送推送通知的 API 终结点从 https://android.googleapis.com/gcm/send 更改为 https://fcm.googleapis.com/fcm/send
较旧的终结点仍在工作,但我建议使用最新的终结点。对于这两个终结点,此代码保持不变。
试试这个,声明一个发送通知的函数:
function sendfcmMessage($registrationIds, $msg)
{
if (!defined('API_ACCESS_KEY')) define('API_ACCESS_KEY', '<PUT_YOUR_KEY_HERE>');
$fields = array('registration_ids' => $registrationIds, 'data' => $msg, 'content-available' => 1, 'priority' => 'high'); //set priority as required
$headers = array('Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
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);
curl_close($ch);
return $result;
}
像这样使用它,
$msg = array('message' => $message,
'title' => $title,
'body' => $body,
'subtitle' => $subtitle,
'tickerText' => $ticker_text,
'vibrate' => $vibrate,
'sound' => $sound,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon');
$registrationIds = array();
while ($row = mysqli_fetch_array($r)) {
array_push($registrationIdsIOS, $row['regToken'];);
}
sendfcmMessage($registrationIds, $msg);
您还可以通过打印此函数的结果来进行调试,以了解是否发送了通知,它还使您知道未发送通知的原因。
echo sendfcmMessage($registrationIds, $msg);
您必须在 JSON 数组中获取结果。请尝试以下代码:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$tags = $_GET['tags'];
// Replace with the real server API key from Google APIs
$apiKey = "YOUR_API_CODE";
$message = "Hello Raja";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
require_once('dbConnect.php');
$user_ids = array();
foreach ($_REQUEST['tags'] as $key => $val) {
$user_ids[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}
$tagss = "'" . implode("','", $user_ids) . "'";
$sql = "SELECT user_tags.user_id AS userID , gcm_token.regtoken AS regToken
FROM user_tags,gcm_token
WHERE tags IN ({$tagss}) AND user_tags.user_id=gcm_token.user_id";
$r = mysqli_query($con,$sql);
$result = array();
//looping through all the records fetched
while ($row = mysqli_fetch_array($r)) {
$result[] = $row['regToken'];
}
//Displaying the array in json format
//echo json_encode(array('result'=>$result));
echo json_encode(($result));
$registrationIDs = ($result);
//echo json_encode(array('result'=>$result));
$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);
}
?>
问题内容: 我正在关注http://javapapers.com/android/google-cloud-messaging-gcm-for-android- and-push-notifications/ 吗?通过GCM发送推送通知。一切正常,但是,我只能将推送通知发送到一台设备。注册另一台设备将替换先前设备的注册ID。我在http://javapapers.com/android/andro
我需要使用< code>aws-sns-javascript向多个设备发送推送通知。当我创建createPlatformEndpoint时,我可以只添加一个设备令牌,但我需要向多个设备发送通知,如一个数组令牌 创建平台应用程序 创建平台终端节点 向Amazon SNS主题发送消息
我有一个包含用户名数据库的服务器。我还有一个Android应用程序,用户可以在服务器数据库中注册自己的用户名,也可以在服务器的GCM部分注册设备。 我目前正在服务器代码中使用GCM演示,它将多播一个推送通知到每个注册的设备。但是,我希望它能够将推送通知发送给某些用户,而不是每个注册的GCM设备。 我的第一个想法是将数据库中的每个用户与其GCM注册表关联起来。这样行吗?我读过一些关于regID更改或
我需要使用< code>aws-sdk-go lib通过设备令牌数组向多个设备发送SNS推送通知。 目前我正在使用以下步骤向SNS发送推送消息: 创建endpoint: 将消息发送到endpoint: 我还没有见过一种方法,只使用一个请求就可以将一个推送消息发送到多个设备。可能吗? 像这个例子来说明:
我正在尝试通过AWS SNS向GCM发送移动推送通知。根据最新的GCM 3.0留档,其中可能包括“通知”有效负载或“数据”有效负载(或两者兼而有之)。如果您发送通知有效负载,那么GCM将负责为您在最终用户设备上显示通知。 使用Amazon SNS控制台,我尝试发送仅通知有效负载,但遇到以下错误: 无效参数:消息原因:协议GCM的通知无效:json消息中需要数据密钥(服务:AmazonSNS状态码:
问题内容: 我正在开发一个PHP网站+ iPhone应用程序和iPhone应用程序API,具有一个面向学生和医生的消息传递系统,当任何一个人(从网站或iPhone)发送消息时,另一个用户应在其iPhone上获得推送通知。例如,如果学生为老师添加了一个新问题,则老师iPhone / iPad上的推送通知将发送给老师,并且当老师回复学生的答案时,学生会收到推送通知。 由于对在网站上注册的师生人数没有限