Exploring

Fast low cost development: Using Pusher REST API on Android.

noras noras |

Tags: #Android #API #Java #Pusher #REST

One of the projects I was working on was an Android app that will not be published on the store and will only be used for one day by one user – a truck driver that will drive around the streets of Stockholm before Christmas eve and give some kind of Christmas gifts.
This app will broadcast the location of the driver and I need to show a map to endusers with the location of the truck on a public website.
I set my own goals for doing this project:

  • I want to keep the cost as low as possible: No paid service, no paid hosting ..
  • I want to create this site without using a backend. i.e I only want to create a static page with frontend tech.

In order to broadcast the location without using a backend I need to use a cloud service that provides cloud messaging or websockets.
In the first round, I tried using Firebase cloud messaging but then I saw that the users needs to accept the notification permission in order to be able to receive messages from javascript. Then a tool that I have used previously to receive realtime messages on Android crossed to my mind – Pusher.

Pusher provides a powerful service when it comes to handling realtime events, and it supports so many client side and server side platforms.
One problem was that pusher was not build to send messages from a platform like Android. It’s more used for broadcasting from a server to set of clients.
I looked at the documentation searching for a REST API that I could hopefully wrap and hit the endpoint from the Android and here is the result..

The Code

     JSONObject params = new JSONObject();
        try {

            JSONObject data = new JSONObject();
            data.put("message", message);
            String escaped_data=data.toString();
            params.put("data",escaped_data);
            params.put("name","my-event");
            params.put("channel","my-channel");


        } catch (JSONException e) {
            e.printStackTrace();
        }

        String app_id="XXXX";
        String key="XXXXXXXXXXXXX";
        String secret="XXXXXXXXXXXXX";
        
        
        Long tsLong = System.currentTimeMillis()/1000;

        String auth_timestamp=tsLong.toString();
        String auth_version="1.0";

        String body_md5=HashingUtils.MD5(params.toString());
        String string_to_sign =  "POST\n/apps/" + app_id +
                        "/events\nauth_key=" + key +
                        "&auth_timestamp=" + auth_timestamp +
                        "&auth_version=" + auth_version +
                        "&body_md5=" + body_md5;

        String auth_signature=HashingUtils.generateHashWithHmac256(string_to_sign,secret);

        String API_ENDPOINT="https://api-eu.pusher.com/apps/672274/events?" +
                "body_md5="+body_md5+
                "&auth_version=" + auth_version +
                "&auth_key=" + key +
                "&auth_timestamp=" + auth_timestamp +
                "&auth_signature="+auth_signature;

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, API_ENDPOINT, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        Log.d("RESPONSE ", "RESPONSE " + response.toString());

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("RESPONSE_ERROR", "RESPONSE_ERROR " + error.getMessage());
                NetworkResponse networkResponse = error.networkResponse;
                if (networkResponse != null && networkResponse.data != null) {
                    String jsonError = new String(networkResponse.data);
                    Log.d("RESPONSE_ERROR", "RESPONSE_ERROR " + jsonError);
                }

            }

        }
        );
        queue.add(jsonObjectRequest);

About the author

noras

"Senior Software Engineer. MSc in Computer systems and Networks with big interest in security. Loves to play with Android code and does security research for fun and profit. Speaks 4 languages and codes in much more."

Related articles

Tags: #Android #API #Java #Pusher #REST




Copyright © 2019 - nindoda.com