0
votes

I am new in the Java Android world. I am working on a course project and I need some help. In my project I have developed an Android app and a Java Socket Server. Android App requests for Keys from Server and server generates keys, Stores them in its DB and sends keys back to android client. This is working fine. Now, in android client, I have to add a button called "Register Device" and on click this should register device with GCM and store the regID in device, which I want to add with keys request and send to server. Server will store the regID in DB with the keys. Then later I want to send a push message to device using that stored regID.

Problems:

  • How can I store and then access GCM regID to send to server?
  • What needs to be done in my Socket Server to send push messages to device using stored regID?
1

1 Answers

0
votes

As I see you have obtained the deviceID and registrationID and you have to store it on your server. Now can create a button which will do the following onclick. $ public void sendRegistrationIdToServer(String deviceId, String 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();
    }
}

And in the server side you can receive the ID's and store it in your database. Hope you get it.