朋友们,我正在开发一个简单的 android 测试应用程序,其基本任务是从 php 应用程序服务器向设备发送消息从 gcm 到设备。我可以从设备在gcm上注册,然后尝试将注册ID发送到Web应用程序。以下是与应用程序相关的主要文件,我认为可能需要纠正某些内容:
Congfig.java
public interface Config {
// used to share GCM regId with application server - using php app server
static final String APP_SERVER_URL = "http://49.249.146.129/gcm/gcm.php?shareRegId=1";
// GCM server using java
// static final String APP_SERVER_URL =
// "http://192.168.1.17:8080/GCM-App-Server/GCMNotification?shareRegId=1";
// Google Project Number
//static final String GOOGLE_PROJECT_ID = "512218038480";
static final String GOOGLE_PROJECT_ID = "1190";
static final String MESSAGE_KEY = "message";
}
这是我尝试从 php 文件发送推送消息时遇到的错误。
Warning: file_get_contents(GCMRegId.txt): failed to open stream: No such file or directory in C:'xampp'htdocs'gcm'gcm.php on line 37
Google Cloud Messaging (GCM) Server in PHP
{"multicast_id":7821558258646259390,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
请帮帮我。
使用下面的代码,它对我有用。只需传递设备 ID 并发送通知的消息。检查一下
function send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
define("GOOGLE_API_KEY", "API Key Here");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'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_VERIFYHOST, 0);
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);
return $result;
}
$pushMessage = $name." was created meeting to you.";
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('message' => $pushMessage, 'type' => 'Request', 'token' => $token);
$pushStatus = $this->send_notification($gcmRegIds, $message);
}
"无效注册"表示您发送的注册 ID 未注册。正确检查 API 密钥和注册 ID。检查传递给服务器的注册 ID 的格式。因此,基本上您在将手机收到的DeviceID发送到服务器时出错。
您可以在此处查看接收下游消息的部分:https://developers.google.com/cloud-messaging/
以下是有关注册令牌的有用链接:https://developers.google.com/cloud-messaging/android/client#sample-register