user2964990
user2964990

Reputation: 1

Getting the response code 200 while sending message to gcm server but the result displays error

I have created the application server that sends the message to the gcm server via http post. After executing the code i get the response code of 200 but the result displayed is Error=MissingRegistration .

I am not able to understand the that why is the registration id missing. I have successfully obtained the registration id of the emulator by creating google account on it. I have verified my project id and the keys and have also used gcm-server.jar to create server. I have also tried using server key instead of browser key but still getting the same result.

Here is the code of my application server that sends the message through http post to gcm server.

    package com.example.gcmserver;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpException;
    import org.apache.http.HttpRequest;
    import org.apache.http.HttpRequestInterceptor;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.AuthState;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.protocol.ClientContext;
    import org.apache.http.impl.auth.BasicScheme;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HttpContext;

    public class Server {
        public static void main(String[] args) throws IOException {

            DefaultHttpClient client = new DefaultHttpClient();
            client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
            client.addRequestInterceptor(new HttpRequestInterceptor() {


        @Override
        public void process(HttpRequest arg0, HttpContext context)
                throws HttpException, IOException {
            // TODO Auto-generated method stub
              AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                 if (state.getAuthScheme() == null) {
                     BasicScheme scheme = new BasicScheme();
                     CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
                     Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
                     if (credentials == null) {
                         throw new HttpException();
                     }
                     state.setAuthScope(AuthScope.ANY);
                     state.setAuthScheme(scheme);
                     state.setCredentials(credentials);
        }}
     }, 0); 
                    HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");

              List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
              urlParameters.add(new BasicNameValuePair("data.registration id","APA91bERSCmbVV-diaS7OkA6nQlatuq1e8whUhjN54WcsfasiY3PSlby2_DtSq7Gjs_rXcOuUPTj8obnvnD55VbXJiWvWgXGj71oRFw_0yP4uz9JGDTax5dY-DKhnk6tK2gEFy50bDuKebQoRnUo9CYfSSW3AAtqV4tJDKv4GYwtUI0vIUliSJM"));

              urlParameters.add(new BasicNameValuePair("data.title", "e-pass application"));
              urlParameters.add(new BasicNameValuePair("data.message", "Your code is 13133"));


              httppost.setHeader("Authorization",
                      "key=mybrowserkey");
              httppost.setHeader("Content-Type",
                      "application/x-www-form-urlencoded;charset=UTF-8");
              httppost.setHeader("Sender_ID", "myprojectid");

              httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));

              HttpResponse response = client.execute(httppost);
              System.out.println("Response Code : " 
                          + response.getStatusLine().getStatusCode());
              System.out.println("Getting the contents of the message");

              BufferedReader rd = new BufferedReader(
                      new InputStreamReader(response.getEntity().getContent()));

              StringBuffer result = new StringBuffer();
              String line = "";
              while ((line = rd.readLine()) != null) {
                  result.append(line);
              }


              System.out.println("the result is:"+result);
    }
    }

I have referred to this link to create the server: Trying to HTTP-POST to GCM from Java

Thanks a lot in advance for the help.

Upvotes: 0

Views: 742

Answers (1)

Koh
Koh

Reputation: 1570

No need to prefix registration ID with "data.". The key-value pair should be: registration_ids, array containing the registration ID

Upvotes: 0

Related Questions