如何使用jUnit测试通过服务器端发送推送通知

时间:2015-01-27 07:36:51

标签: java android junit java-server

我正在使用此代码在Android设备中发送推送通知。 http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/它的工作。但是他们在服务器端使用php。我的服务器端是JAVA。所以我正在尝试为此创建jUnit测试。所以我的服务器将发送推送通知。但我不知道这一点。我通过post方法尝试但没有工作。得到401错误。

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);

请在jUnit中向我推荐。

1 个答案:

答案 0 :(得分:0)

我传递了不正确的参数。它应该是标题和正文。这是我使用和工作的方式。

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();
    }
}

这是另一个使用HTTPHeader和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);
}

注意: - 如果您遇到编译错误,则必须使用最新的HTTP库并在libs文件夹中使用,然后在构建路径中添加所有内容。