Mike Baxter
Mike Baxter

Reputation: 7258

Sending notification to GCM using HTTP POST

I'm trying to send an HTTP POST notification to Google Cloud Messaging in order for a notification to be delivered to the app that I am developing.

For testing purposes, I am also delivering the HTTP POST from the app as well - just to see if I can get a response (will implement this logic server-side later).

I am following the tutorial found here but it's hard to follow as I am not using JSON. When I run the code below, I get the expected response: Unauthorized: Error 401. This is because I am not supplying enough data, but cannot seem to discover which data I need to send with this request.

class RequestTask extends AsyncTask<String, String, String>
    {
        final static String API_KEY = ***********; // From google developer console
        final static String CONTENT_TYPE = "application/x-www-form-urlencoded;charset=UTF-8";
        @Override
        protected String doInBackground(String... uri)
        {
            String responseString = "";
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(uri[0]);
            try
            {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                // not even sure if "Authorization" is the correct key to send
                nameValuePairs.add(new BasicNameValuePair("Authorization", "key=" + API_KEY));
                nameValuePairs.add(new BasicNameValuePair("Content-Type", CONTENT_TYPE));
                HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
                post.addHeader(entity.getContentType());
                post.setEntity(entity);

                HttpResponse response = client.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null)
                {
                    Log.i("HTTP RESPONSE", line);
                    responseString = responseString + "\n" + line;
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

            return responseString;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            Intent intent = new Intent(ctx, ResponseActivity.class);
            intent.putExtra("text", result);
            startActivity(intent);
        }
    }

What must I change in order to successfully send this GCM notification request?

Upvotes: 0

Views: 907

Answers (2)

nKn
nKn

Reputation: 13761

Additionally to what @Eran proposed, other possible issue could be setting the Content-Type of the HTTP header incorrectly. Try replacing this line:

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

With this one:

HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);

Afterwards:

post.addHeader(entity.getContentType());
post.setEntity(entity);

Upvotes: 1

Eran
Eran

Reputation: 393841

Here's your error :

nameValuePairs.add(new BasicNameValuePair("Authorization", API_KEY));

It should be :

nameValuePairs.add(new BasicNameValuePair("Authorization", "key="+API_KEY));

The HTTP header must contain the following headers:

Authorization: key=YOUR_API_KEY
Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.

Upvotes: 0

Related Questions