我尝试使用PHP向Android设备发送推送通知。当我将文本插入文本字段并将其发送到Android设备。它在设备上发送了成功但没有内容。为什么?我该如何解决? 这是我的PHP代码:
<?php
include('config/dbconnect_log.php');
if(isset($_POST['submit'])){
function sendPushNotification($registration_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'my google api key');
$headers = array(
'Authorization:key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
}
$pushStatus = '';
$query = "SELECT DISTINCT googleid FROM ggevent";
if($query_run = mysql_query($query)) {
$gcmRegIds = array();
$i = 0;
while($query_row = mysql_fetch_assoc($query_run)) {
$i++;
$gcmRegIds[floor($i/1000)][] = $query_row['googleid'];
}
}
$pushMessage = $_POST['message'];
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('msg' => $pushMessage);
$pushStatus = array();
foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);
}
}
?>
<html>
<head>
</head>
<body>
<article class="module width_full">
<form method = 'POST' action = ''>
<fieldset>
<label>Content</label>
<textarea rows = 2 name = "message" placeholder = 'Messages to Transmit via GCM'></textarea>
</fieldset>
<div class="clear"></div>
</div>
<footer>
<div class="submit_link">
<input type="submit" name='submit' value="Send Push Notification" class="alt_btn">
</div>
</footer>
</form>
</article>
</body>
</html>
答案 0 :(得分:0)
首先将设备注册到GCM并获取gcm密钥。使用GCM密钥手动将通知发送到该设备。卷曲请求是
curl --header "Authorization: key=" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -XPOST -d '{"data":{"New message through curl"},"registration_ids":["APA91bGe-Ihcjil6YXgLNEtf1fZCvcrgcn35rTOsHPrK7mxoz70TP6jmniChLXmIbLd642CWGo0fem9XrECa2ukvAvNYfNY7HcJ0JcxTInBxNn5Pml9YQIU6qwRVQ4v8GIE4oZv4vjoc"]}'
如果您使用此方式收到通知,则说明您的设备已成功注册
有关测试的更多信息和详细工作请通过以下链接