无效的注册ID与我的gcm php服务器 [英] Invalid Registration Id with my gcm php server
问题描述
因此,我将POST张贴到我的gcm.php并将用户手机的注册ID发送到我的服务器上的文本文件中(之后将实施数据库)。出于某种原因,当我发布到gcm服务器时,我收到了无效的注册错误。
我检查了我的txt文件,它没有被填充(这是问题)所以我很好奇我在我的android客户端代码中做了什么错误这是不是与我的PHP服务器共享用户的ID。
gcm.php(服务器)
<?php
//通用php函数发送GCM推送通知
函数sendPushNotificationToGCM($ registatoin_ids,$ message){
// Google云端消息GCM-API url
$ url ='https://android.googleapis.com/gcm/send';
$ fields = array(
'registration_ids'=> $ registatoin_ids,
'data'=> $ message,
);
// Google Cloud Messaging GCM API键
define(GOOGLE_API_KEY,AIzaSyDA5dlLInMWVsJEUTIHV0u7maB82MCsZbU);
$ 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);
返回$ result;
}
?>
<?php
//这个区块是发送消息给GCM,点击
$ pushStatus =;
if(!empty($ _ GET [push])){
$ gcmRegID = file_get_contents(GCMRegId.txt);
$ pushMessage = $ _POST [message];
if(isset($ gcmRegID)&& isset($ pushMessage)){
$ gcmRegIds = array($ gcmRegID);
$ message = array(m=> $ pushMessage);
$ pushStatus = sendPushNotificationToGCM($ gcmRegIds,$ message);
//这块是从外部(移动应用程序)
接收GCM regId if(!empty($ _ GET [shareRegId] )){
$ gcmRegID = $ _POST [regId];
file_put_contents(GCMRegId.txt,$ gcmRegID);
回声OK!;
出口;
}
?>
< html>
< head>
< title> PHP中的Google Cloud Messaging(GCM)服务器< / title>
< / head>
< body>
< h1> PHP中的Google Cloud Messaging(GCM)服务器< / h1>
< form method =postaction =gcm.php /?push = 1>
< div>
< / div>
< div>< input type =submitvalue =通过GCM发送推送通知/>< / div>
< / form>
< p>< h3><?php echo $ pushStatus; ?>< / H3>< / p为H.
< / body>
< / html>
这里是Android客户端(我尝试与服务器
共享regId MainActivity.java
class GcmRegistrationAsyncTask extends AsyncTask< Void,Void,String> {
private GoogleCloudMessaging gcm ;
私有上下文上下文;
// TODO:根据上述说明将您自己的发件人ID更改为Google Developers Console项目编号
private static final String SENDER_ID =617516375608 ;
public GcmRegistrationAsyncTask(Context context){
this.context = context;
}
@Override
protected String doInBackground( Void ... params){
if(regService == null){
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),new AndroidJsonFactory(),null)
.setRootUrl(https://keen-proton-827.appspot.com/_ah/api/);
// e nd可选的本地运行代码
regService = builder.build();
}
String msg =;
尝试{
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
}
字符串regId = gcm.register(SENDER_ID);
msg =设备注册,注册ID =+ regId;
//您应该通过HTTP将注册ID发送到您的服务器
//以便它可以使用GCM / HTTP或CCS将消息发送到您的应用程序。
//如果您的应用程序
//正在使用帐户,则应向您的服务器发送请求。
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(http://68.84.84.84/gcm.php?shareRegId=1);
列表< NameValuePair> nameValuePairs = new ArrayList< NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair(regId,regId));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
Log.d(STATUS,response.toString());
regService.register(regId).execute();
} catch(IOException ex){
ex.printStackTrace();
msg =错误:+ ex.getMessage();
}
return msg;
$ b @Override
protected void onPostExecute(String msg){
Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
Logger.getLogger(REGISTRATION).log(Level.INFO,msg);
}
}
注册Id:在Android客户端成功注册后获得的令牌
String token = InstanceID.getInstance(mContext).getToken(senderId,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
So I'm POSTing to my gcm.php and sending the registration id of the users phone to be put in a text file on my server (Will implement a DB afterwards). For some reason, I'm getting an invalid registration error when I post to the gcm server.
I've checked my txt file and it's not getting populated (which is the problem) So I'm curious what I'm doing wrong in my android client side code that isn't sharing the user's ID with my php server.
gcm.php (server)
<?php
//generic php function to send GCM push notification
function sendPushNotificationToGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "AIzaSyDA5dlLInMWVsJEUTIHV0u7maB82MCsZbU");
$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;
}
?>
<?php
//this block is to post message to GCM on-click
$pushStatus = "";
if(!empty($_GET["push"])) {
$gcmRegID = file_get_contents("GCMRegId.txt");
$pushMessage = $_POST["message"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
}
}
//this block is to receive the GCM regId from external (mobile apps)
if(!empty($_GET["shareRegId"])) {
$gcmRegID = $_POST["regId"];
file_put_contents("GCMRegId.txt",$gcmRegID);
echo "Ok!";
exit;
}
?>
<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
<form method="post" action="gcm.php/?push=1">
<div>
<textarea rows="2" name="message" cols="23" placeholder="Message to transmit via GCM"></textarea>
</div>
<div><input type="submit" value="Send Push Notification via GCM" /></div>
</form>
<p><h3><?php echo $pushStatus; ?></h3></p>
</body>
</html>
And here is the android client (where I try to share the regId with the server MainActivity.java
class GcmRegistrationAsyncTask extends AsyncTask<Void, Void, String> {
private GoogleCloudMessaging gcm;
private Context context;
// TODO: change to your own sender ID to Google Developers Console project number, as per instructions above
private static final String SENDER_ID = "617516375608";
public GcmRegistrationAsyncTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(Void... params) {
if (regService == null) {
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl("https://keen-proton-827.appspot.com/_ah/api/");
// end of optional local run code
regService = builder.build();
}
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regId = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regId;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://68.84.84.84/gcm.php?shareRegId=1");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regId", regId));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
Log.d("STATUS", response.toString());
regService.register(regId).execute();
} catch (IOException ex) {
ex.printStackTrace();
msg = "Error: " + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
Logger.getLogger("REGISTRATION").log(Level.INFO, msg);
}
}
Registration Id: the token got after sucessfully registered in Android client
String token =
InstanceID.getInstance(mContext).getToken(senderId,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
这篇关于无效的注册ID与我的gcm php服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!