Skip to content

Instantly share code, notes, and snippets.

@rakawestu
Last active April 20, 2018 20:50
Show Gist options
  • Save rakawestu/59af3969e63279674075 to your computer and use it in GitHub Desktop.
Save rakawestu/59af3969e63279674075 to your computer and use it in GitHub Desktop.
Get json payload from Parse push notification
public class ParsePushBroadcasReceiver extends ParsePushBroadcastReceiver{
/**
* Provide resource identifier of the small icon for each
* push notification in the notification bar.
* @param context application context
* @param intent intent
* @return the icon resource id.
*/
@Override
protected int getSmallIconId(Context context, Intent intent) {
return R.mipmap.ic_launcher;
}
/**
* Provide bitmap for each push notification icon.
* For this implementation, default application icon will be used.
* @param context application context
* @param intent intent
* @return bitmpat of the icon.
*/
@Override
protected Bitmap getLargeIcon(Context context, Intent intent) {
return BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
}
/**
* Receive push notification from Parse.
* Default implementation will show the icon, title and the
* message (alert).
* @param context application context
* @param intent push intent
*/
@Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
}
@Override
protected void onPushOpen(Context context, Intent pushIntent) {
//Get payload from push intent
String json = pushIntent.getStringExtra("com.parse.Data");
try {
//Get order id using Parse Utils
int orderId = ParseUtils.getOrderId(json);
//Open the order detail
openOrderDetail(context, orderId);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void openOrderDetail(Context context, int orderId) {
//Open activity that will show the order detail
Intent intent = new Intent(context, OrderDetailActivity.class);
//Put order id into intent
intent.putExtra("order_id", orderId);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
}
}
{
"alert": "Pesanan ditambahkan oleh Raka",
"order_payload": {
"ahs": {
"ahs_name": "AHS - ONEBIT",
"ahs_phone": "085241329722",
"distributor_id": 1,
"id": 17,
"region": null,
"road_address": "JL WIRAJAYA 312"
},
"consumer": {
"additional_address": "Balikpapan",
"additional_info": "",
"consumer_name": "Raka",
"id": 42,
"phone_no": "6285643956354",
"road_address": "Jl. Al Makmur 1 No. 16"
},
"order": {
"additional_items": "Elpiji 12 kg 1 unit, Elpiji 3 kg 1 unit, Beras 1 kg",
"additional_items_price": 0,
"ahs": {
"ahs_name": "AHS - ONEBIT",
"ahs_phone": "085241329722",
"distributor_id": 1,
"id": 17,
"region": null,
"road_address": "JL WIRAJAYA 312"
},
"ahs_id": 17,
"consumer": {
"additional_address": "Balikpapan",
"additional_info": "",
"consumer_name": "Raka",
"id": 42,
"phone_no": "6285643956354",
"road_address": "Jl. Al Makmur 1 No. 16"
},
"consumer_id": 42,
"created_at": "2015-11-19T07:58:42.153Z",
"deleted_at": null,
"delivery_man_id": null,
"id": 197,
"order_details": [
{
"ahs_active_product_id": 26004,
"created_at": "2015-11-19T07:58:42.168Z",
"deleted_at": null,
"id": 429,
"order_id": 197,
"product_id": 50,
"product_image": "http://res.cloudinary.com/farishjazz/image/upload/v1447851849/product_vit_levite_sirsak_p3sxie.png",
"product_name": "VIT 350 ml Levite Soursop",
"product_qty": 3,
"total_price": null,
"updated_at": "2015-11-19T07:58:42.168Z"
},
{
"ahs_active_product_id": 70001,
"created_at": "2015-11-19T07:58:42.168Z",
"deleted_at": null,
"id": 430,
"order_id": 197,
"product_id": 51,
"product_image": "http://res.cloudinary.com/farishjazz/image/upload/v1447851918/product_tumbler_waterdrop_pink_fmueeh.png",
"product_name": "WATERDROP Pink 600 ml",
"product_qty": 2,
"total_price": null,
"updated_at": "2015-11-19T07:58:42.168Z"
},
{
"ahs_active_product_id": 50402,
"created_at": "2015-11-19T07:58:42.168Z",
"deleted_at": null,
"id": 431,
"order_id": 197,
"product_id": 52,
"product_image": "http://res.cloudinary.com/farishjazz/image/upload/v1447851859/product_vit_levite_orange_ivh38p.png",
"product_name": "VIT 350 ml Levite Orange",
"product_qty": 3,
"total_price": null,
"updated_at": "2015-11-19T07:58:42.168Z"
}
],
"order_grand_total_price": 0,
"order_status": "pending",
"order_total_price": 0,
"request_order": "VIT 350 ml Levite Soursop 3 pcs, ...",
"transaction_date": "2015-11-19T07:58:42.148Z",
"updated_at": "2015-11-19T07:58:42.153Z"
}
},
"title": "Order #197 ditambahkan"
}
import com.parse.ParseInstallation;
import org.json.JSONException;
import org.json.JSONObject;
public class ParseUtils {
/**
* Get order id inside order payload json.
* @param json order payload json string
* @return order id
* @throws JSONException exception when retrieving value from json object.
*/
public static int getOrderId(String json) throws JSONException {
//Convert string into json object
JSONObject jsonObject = new JSONObject(json);
//Get order_payload object
JSONObject order_payload = jsonObject.getJSONObject("order_payload");
//Get order object
JSONObject order = order_payload.getJSONObject("order");
//Get order id
return order.getInt("id");
}
/**
* Send AHS ID into parse to enable parse notification.
* @param ahsID identifier of AHS.
**/
public static int sendAhsID(int ahsID) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("ahsId", ahsID);
installation.saveEventually();
}
}
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class ParseUtilsTest {
private String json;
private int orderId;
@Before
public void setUp() throws Exception {
json = "{\n" +
" \"alert\": \"Pesanan ditambahkan oleh Beny\",\n" +
" \"order_payload\": {\n" +
" \"ahs\": {\n" +
" \"ahs_name\": \"AHS Jane\",\n" +
" \"ahs_phone\": null,\n" +
" \"distributor_id\": 1,\n" +
" \"id\": 1,\n" +
" \"region\": null,\n" +
" \"road_address\": null\n" +
" },\n" +
" \"consumer\": {\n" +
" \"additional_address\": \"A\",\n" +
" \"additional_info\": \"\",\n" +
" \"consumer_name\": \"Beny\",\n" +
" \"id\": 43,\n" +
" \"phone_no\": \"6285641460602\",\n" +
" \"road_address\": \"Menuju jalan cahaya\"\n" +
" },\n" +
" \"order\": {\n" +
" \"additional_items\": \"Elpiji 12 kg 1 unit, \",\n" +
" \"additional_items_price\": 0,\n" +
" \"ahs\": {\n" +
" \"ahs_name\": \"AHS Jane\",\n" +
" \"ahs_phone\": null,\n" +
" \"distributor_id\": 1,\n" +
" \"id\": 1,\n" +
" \"region\": null,\n" +
" \"road_address\": null\n" +
" },\n" +
" \"ahs_id\": 1,\n" +
" \"consumer\": {\n" +
" \"additional_address\": \"A\",\n" +
" \"additional_info\": \"\",\n" +
" \"consumer_name\": \"Beny\",\n" +
" \"id\": 43,\n" +
" \"phone_no\": \"6285641460602\",\n" +
" \"road_address\": \"Menuju jalan cahaya\"\n" +
" },\n" +
" \"consumer_id\": 43,\n" +
" \"created_at\": \"2015-11-18T10:29:40.011Z\",\n" +
" \"deleted_at\": null,\n" +
" \"delivery_man_id\": null,\n" +
" \"id\": 180,\n" +
" \"order_details\": [\n" +
" {\n" +
" \"created_at\": \"2015-11-18T10:29:40.028Z\",\n" +
" \"deleted_at\": null,\n" +
" \"id\": 380,\n" +
" \"order_id\": 180,\n" +
" \"product_id\": 1,\n" +
" \"product_image\": \"http://res.cloudinary.com/farishjazz/image/upload/v1447298069/product_img_mizone_apple_mzujqi.png\",\n" +
" \"product_name\": \"Mizone Apple Guava 500ml\",\n" +
" \"product_qty\": 1,\n" +
" \"total_price\": null,\n" +
" \"updated_at\": \"2015-11-18T10:29:40.028Z\"\n" +
" },\n" +
" {\n" +
" \"created_at\": \"2015-11-18T10:29:40.028Z\",\n" +
" \"deleted_at\": null,\n" +
" \"id\": 381,\n" +
" \"order_id\": 180,\n" +
" \"product_id\": 2,\n" +
" \"product_image\": \"http://res.cloudinary.com/farishjazz/image/upload/v1447298071/product_img_mizone_lychee_ey4u6q.png\",\n" +
" \"product_name\": \"Mizone Lychee Lemon 500ml\",\n" +
" \"product_qty\": 1,\n" +
" \"total_price\": null,\n" +
" \"updated_at\": \"2015-11-18T10:29:40.028Z\"\n" +
" }\n" +
" ],\n" +
" \"order_grand_total_price\": 0,\n" +
" \"order_status\": \"pending\",\n" +
" \"order_total_price\": 0,\n" +
" \"request_order\": \"Mizone Apple Guava 500ml 1 pcs, ...\",\n" +
" \"transaction_date\": \"2015-11-18T10:29:40.008Z\",\n" +
" \"updated_at\": \"2015-11-18T10:29:40.011Z\"\n" +
" }\n" +
" },\n" +
" \"title\": \"Order #180 ditambahkan\"\n" +
"}";
orderId = 180;
}
@Test
public void testGetOrderId() throws Exception {
assertEquals(orderId, ParseUtils.getOrderId(json));
}
}
@rakawestu
Copy link
Author

This is a simple implementation to get data from Parse push notification.

  1. ParsePushBroadcastReceiver.java is the broadcast receiver that will handle receiving push notification from Parse.
  2. ParsePushPayload.json is a sample json payload from received push notification.
  3. ParseUtils.java is a simple utility that will be used to retrieving data from push data.
  4. ParseUtilsTest.java is the unit test class for ParseUtils.java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment