How to Extend Your Android App to Include SMS-based Shipment Tracking

Posted by Pedram Mohammadi on April 18, 2017

How to Extend Your Android App to Include SMS-based Shipment Tracking | http://blog.zang.io

TaCode Tuesday is back!  That means your favorite developer at Zang is here to deliver free snippets of code for use in your very own text/voice apps along with hilarious taco puns. This week I’m going to show you how to make e-commerce customers smile by allowing them to track deliveries via SMS on their Android phone. But first, let's taco 'bout how this can set your business apart and improve customer loyalty.  

In most cases, products are cheaper when ordered online. When compared to retail, online products are more or less 50 percent cheaper due to the elimination of certain components that hike up product prices like storage, rent, staff, utilities, and the middle-man.

E-commerce has come a long way since the 2001 dot-com boom. In fact, cross-border e-commerce is a growing phenomenon. In Nielsen’s 2016 global report, survey data showed that 57 percent of consumers have purchased something from an online store outside their country for the past six months. Italians are the most receptive at 79 percent, followed by Germans at 73 percent, and Russians at 64 percent.

The figure below shows how much shoppers around the world have purchased internationally through an ecommerce platform.

 

nielsens-global-report-commerce-adoption.png

Figure 1: Nielsen’s Global Report of Cross-border Commerce Adoption

 

Challenges of Cross Border Commerce

Cross-border commerce’s high adoption rate can be attributed to the new-found freedom of access to a world of products that were previously unavailable to a consumer’s home country. As Nielsen said:

“Choice is greatly enhanced by cross-border e-commerce. In many developing markets, the growing middle class is trading up and demanding greater assortment than found at their domestic retailer. For example, some of these consumers are looking overseas to purchase authentic foreign brands, often at lower prices than they can find in their home country. Meanwhile, developed-market consumers gain access to a range of goods directly from foreign companies at often significant discounts to what they would pay domestically"

However, logistics and reverse logistics are a challenge when conducting a successful cross-border e-commerce. Effective logistics is key not only to a successful online business, but retail business as well. Shipping costs, reliability, and predictability can be a tipping point if not handled correctly.

 

Shipping and Customer Satisfaction

An e-commerce business’ role in customer satisfaction doesn’t stop the moment the product is handed over to the courier. Customers believe that it is still the business’ responsibility to assure that the products are delivered on time and in good condition. Not only this, returns and exchanges should also be easy with the costs of returns, custom duties, and documentation kept at a minimum, if not for free.

 

SMS-based Shipment Tracking

This week, we will teach you how to create an SMS-based shipment tracking that could help improve customer satisfaction in cross-border transactions. We will be developing this for Android since this mobile OS has dominated the smartphone market.

Before starting, you must at least have an intermediate knowledge of:

  • Java
  • SOAP Web Services
  • KSOAP2 Library for handling SOAP enabled web services
  • Zang SMS API
  • A

The architecture is quite simple. Once an order is placed via an Android phone, a pop-up is displayed asking the customer if he or she wants to receive real-time shipping updates via SMS. Once agreed, every time a shipping milestone is reached, your application server will call the Zang SMS API to send out the shipping status update to your customer’s pre-defined number which is eventually pushed to his or her respective network provider regardless of country.

 

sms-based-shipment-tracking.png 

Figure 2: SMS-Based Shipment Tracking

 

Let’s start coding!

Step 1: Request access to your shipping provider’s API, which allows you to have a real-time update of the shipment as soon as it is reflected in their systems. Alternatively, a daily file extract of shipping status can be pushed to you by the courier that you can consequently send to your customers before end of day. Perhaps a quick work-around (though this should only be temporary), is to have staff who can check at the end of day in bulk the status of each shipment, until the viability of the solution is proven for a wider-scale implementation. For this tutorial, let us assume though that we will be using FedEx as a courier partner. FedEx has a developer API that can be used if you want to integrate its functionality to your website or application.

Step 2: Craft generic shipping milestone messages that you can map to the existing status messages of your respective shipping providers, like FedEx. Below is an example:

 

shipping-milestones.png

Figure 3: A sample shipping milestones

These shipping milestone messages will be sent out to customers via SMS to show where their products are. Alternatively, you can use the exact tracking message that you can pull from the FedEx API. The advantage of sending shipping milestone messages is that it will limit the amount of SMS that you send per customer hence making this added feature more cost-efficient.

Step 3: Connect to FedEx Developer WSDL/Soap web services. Below is an example of a soap envelope tracking request. In using the FedEx API, you have the option to request status one by one or in bulk. Below is a sample of requesting shipping status per shipment.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:v10="http://fedex.com/ws/track/v10">

<soapenv:Header/>

<soapenv:Body>

<v10:TrackRequest>

<v10:WebAuthenticationDetail>

<v10:ParentCredential>

<v10:Key>XXXXXXXXXX</v10:Key>

<v10:Password>XXXXXXXXXX</v10:Password>

</v10:ParentCredential>

<v10:UserCredential>

<v10:Key>XXXXXXXXXX</v10:Key>

<v10:Password>XXXXXXXXXX</v10:Password>

</v10:UserCredential>

</v10:WebAuthenticationDetail>

<v10:ClientDetail>

<v10:AccountNumber>XXXXXXXXX</v10:AccountNumber>

<v10:MeterNumber>XXXXXXX</v10:MeterNumber>

<v10:IntegratorId/>

<v10:Localization>

<v10:LanguageCode>EN</v10:LanguageCode>

<v10:LocaleCode>us</v10:LocaleCode>

</v10:Localization>

</v10:ClientDetail>

<v10:TransactionDetail>

<v10:CustomerTransactionId>Ground Track By

Number</v10:CustomerTransactionId>

<v10:Localization>

<v10:LanguageCode>EN</v10:LanguageCode>

<v10:LocaleCode>us</v10:LocaleCode>

</v10:Localization>

</v10:TransactionDetail>

<v10:Version>

<v10:ServiceId>trck</v10:ServiceId>

<v10:Major>10</v10:Major>

<v10:Intermediate>0</v10:Intermediate>

<v10:Minor>0</v10:Minor>

</v10:Version>

<v10:SelectionDetails>

<v10:CarrierCode>FDXG</v10:CarrierCode>

<v10:PackageIdentifier>

<v10:Type>{TRACKING_NUMBER_OR_DOORTAG}</v10:Type>

<v10:Value>{XXXXXXXXXXXXXXX}</v10:Value>

</v10:PackageIdentifier>

</v10:SelectionDetails>

<v10:ProcessingOptions>{INCLUDE_DETAILED_SCANS}</v10:ProcessingOptions>

</v10:TrackRequest>

</soapenv:Body>

</soapenv:Envelope>

 

Step 4: Install KSOAP2 Library and add it to your Android project then write your Android code that will connect to the SOAP web service of your third-party Courier provider.

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapPrimitive;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

 

 

public class MainActivity extends ActionBarActivity

{

 

String TAG = "Response";

Button bt;

EditText celcius;

String getCel;

SoapPrimitive resultString;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

bt = (Button) findViewById(R.id.bt);

celcius = (EditText) findViewById(R.id.cel);

 

 

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

 

getParcelData = trackParcel.getText().toString();

AsyncCallWS task = new AsyncCallWS();

task.execute();

}

});

}

 

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

 

@Override

protected void onPreExecute() {

Log.i(TAG, "onPreExecute");

}

 

@Override

protected Void doInBackground(Void... params) {

Log.i(TAG, "doInBackground");

calculate();

return null;

}

 

@Override

protected void onPostExecute(Void result) {

Log.i(TAG, "onPostExecute");

Toast.makeText(MainActivity.this, "Response" + resultString.toString(), Toast.LENGTH_LONG).show();

}

 

}

 

public void tracking()

{

String SOAP_ACTION = "{YOUR THIRD PARTY COURIER API}";

String METHOD_NAME = "{YOUR THIRD PARTY COURIER API METHOD_NAME}";

String NAMESPACE = "{http://www.domain.com/webservices/}";

String URL = "{domain.com/xxxxx.asmx}";

 

try {

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

Request.addProperty("TrackingStatus", getCel);

 

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

soapEnvelope.dotNet = true;

soapEnvelope.setOutputSoapObject(Request);

 

HttpTransportSE transport = new HttpTransportSE(URL);

 

transport.call(SOAP_ACTION, soapEnvelope);

resultString = (SoapPrimitive) soapEnvelope.getResponse();

 

Log.i(TAG, "TrackingStatusResult: " + resultString);

} catch (Exception ex) {

Log.e(TAG, "Error: " + ex.getMessage());

}

}

}

Step 5: Write the code for the pop-up message asking the customer if he wanted to have shipping updates via SMS. You will insert this code into your existing Android app to have a sort of opt-in feature for your users to receive SMS.

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);

builder1.setMessage("Do you want to receive SMS tracking notification for your package?

");

builder1.setCancelable(true);

builder1.setPositiveButton(

"Yes",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

// Add handler for Yes

// Goto Step 5

}

});

builder1.setNegativeButton(

"No",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

//Add handler for No

}

});

AlertDialog alert11 = builder1.create();

alert11.show();

Step 5: Write the code to call Zang SMS API to send the shipping status every time FedEx posts shipping update.

public void postData()

{

// Create a new HttpClient and Post Header

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new

HttpPost("https://api.zang.io/v2/Accounts/{AccountSid}/SMS/Messages.json");

try {

//milestones reached

if ( var milestone == true ) {

// Add your data

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

nameValuePairs.add(new BasicNameValuePair("To", "XXX-XXX-XXXX"));

nameValuePairs.add(new BasicNameValuePair("From", "XXX-XXX-XXXX"));

nameValuePairs.add(new BasicNameValuePair("Body", "This is an SMS sent from

Zang"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request

HttpResponse response = httpclient.execute(httppost);

}

catch (ClientProtocolException e) {

// TODO Auto-generated catch block

} catch (IOException e) {

// TODO Auto-generated catch block

}

}

}

//Successful result in JSON format after call to postData

{

"api_version": "v2",

"sid": "{SmsSid}",

"account_sid": "{AccountSid}",

"date_created": "Tue, 24 Jul 2012 14:08:23 +0000",

"date_updated": "Tue, 24 Jul 2012 14:08:24 +0000",

"date_sent": "Tue, 24 Jul 2012 14:08:24 +0000",

"to": "+17325551234",

"from": "+14245551234",

"body": "Test message sent from TelAPI!",

"status": "sent",

"direction": "outbound-api",

"price": "0.070000",

"uri": "/v2/Accounts/AC5521767e53d04deeb0a06f660cee50f9/SMS/Messages/{SmsSid}.json"

}

That’s it!

Using SMS-based shipment tracking is an effective way to increase customer satisfaction that could eventually turn into customer loyalty. Once this state is reached, typical customers can become brand ambassadors that can promote your e-commerce business to their family, friends, and peers transforming them to so-called zombie loyalists- the type of customer that characterizes the patrons of some of the most successful brands, like Apple.

Start building on Zang

Topics: TaCode Tuesday, cPaaS, customer happiness, SMS