0

I am using this code for sending push notification in Android devices. http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ Its work. But they are using php in server side. and My Server side is JAVA. so I am trying to create jUnit testing for that. so my server will send push notification. But i have not idea about that. I tried by post method but did not work. getting 401 error.

String regID = "";

        pairs.add(new BasicNameValuePair("registration_ids",regID));
        pairs.add(new BasicNameValuePair("data","test"));
        pairs.add(new BasicNameValuePair("key","my key"));

        String url = "https://android.googleapis.com/gcm/send";

        String data = makePostCall(url, pairs);

Please suggest to me in jUnit.

Rahul Rawat
  • 999
  • 2
  • 17
  • 40

1 Answers1

0

I was passing incorrect params. It should be header and body. This is way which I used and working nice.

public class SendPushNotification {

public static void main(String[] arg){
    PushNotification pushNoti = new PushNotification();

    //collect the device_tokens into JSONArray.
    // device_tokens is the tokens of user where we needs to send the push Messages.

    String id = "APA9******WVWA";

    JSONArray deviceTokenArray = new JSONArray();
    // if you want to send more than one device then you have to 
   // add more ids into JSONArray by using put method.
    deviceTokenArray.put(id);
    try {
        pushNoti.sentPushIntoAndroid(deviceTokenArray, "I Love to my sister and sending a push message in Android Device");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

and that is another class which is using HTTPHeader and HTTPBody.

public class PushNotification {

public void sentPushIntoAndroid(JSONArray device_token, String message)
        throws JSONException {


    HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");

    StringEntity stringentity = new StringEntity(generateJSONBodyForHTTPBody(device_token, message).toString(), "UTF-8");

    httppost.addHeader("Content-Type", "application/json");
    httppost.addHeader("Authorization", "key=AI**********Mo"");

    httppost.setEntity(stringentity);

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    try {
        response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();

        String strresponse = null;
        if (entity != null) {
            strresponse = EntityUtils.toString(entity);

            displayLog("HTTP Response ", strresponse);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private JSONObject generateJSONBodyForHTTPBody(JSONArray device_token, String message) throws JSONException {

    JSONObject jObj = new JSONObject();
    jObj.put(CommonUtilities.REGISTRATION_ID, device_token);

    JSONObject dataJson = new JSONObject();

//NOTE:- In Client side, you have to retrieve below param by using getBundle().getString("id") like it. so it will retrieve the id and do for other params too like as i did for id.
    dataJson.put("id", "testingID");
    dataJson.put("type", "testingType");
    dataJson.put("imglink", "testingimgLink");
    dataJson.put("seolink", "testingseoLink");
    dataJson.put("msg", "Lata Bhulli");

    jObj.put("data", dataJson);

    displayLog("JSONObject", jObj.toString());

    return jObj;
}

private void displayLog(String tag, String message) {
    System.out.println(tag+" "+message);
}

Note:- If you are getting compile error than you have to use latest HTTP Libraries and used in libs folder then add all in build path.

Rahul Rawat
  • 999
  • 2
  • 17
  • 40