2013-05-30 164 views
0

我是Java Android世界中的新人。我正在参加一个课程项目,我需要一些帮助。在我的项目中,我开发了一个Android应用程序和一个Java Socket服务器。 Android应用程序请求来自服务器和服务器的密钥生成密钥,将它们存储在其数据库中并将密钥发回给Android客户端。这工作正常。现在,在Android客户端中,我必须添加一个名为“注册设备”的按钮,单击此按钮后,应该使用GCM注册设备,并将REGID存储在设备中,我想通过密钥请求添加并发送到服务器。服务器将使用密钥将数据库中的REGID存储在数据库中。然后,我想使用存储的regID向设备发送推送消息。GCM + Android + Java帮助需要

问题:

  • 我如何存储和访问,然后GCM REGID发送给服务器?
  • 需要在我的套接字服务器上使用存储的REGID向设备发送推送消息需要做什么?

回答

0

正如我看到你已经获得deviceID和registrationID,你必须将它存储在你的服务器上。 现在可以创建一个按钮,它将执行以下onclick。 $ 公共无效sendRegistrationIdToServer(字符串的DeviceID, 字符串registrationId){

HttpClient client = new DefaultHttpClient(); 
    Log.d("GCM","sending to server"); 
    HttpPost post = new HttpPost("YOur URL.php"); 
    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     // Get the deviceID 
     nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId)); 
     nameValuePairs.add(new BasicNameValuePair("registrationid", registrationId)); 

     post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = client.execute(post); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     Log.d("GCM","sent to server"); 
       } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

而在服务器端,您可以接收的ID,并存储在数据库中。 希望你得到它。