musethoplicious 3rd musethoplicious 3rd - 1 year ago 55
JSON Question

Get JSON value from remoteMessage Received onMessageReceived method [FCM] Push Notification

How do I get the value

image_id
on json object
payload
in json object
data
that I got from
remoteMessage
contained in
onMessageReceived
Method?

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "FirebaseMessageService";
Bitmap bitmapSmall;
Bitmap bitmapLarge;
Bitmap bitmapPush;
private JSONObject jsonObject;
private String strJSONValue = "{\"Data\":{\"judul_push\":\"pesan_push\" ,\"image_push\":\"icon_kecil\",\"icon_besar\":" +
"\"icon_kecil\",\"is_background\":\"tipe_push\",\"timestamp"
+"\"payload\":{\"IMAGE_ID\":\"IMAGE_TYPE\" },{\"IMAGE_NAME\":\"IMAGE_PATH\"},{\"IMAGE_SEQUENCE\":\"AKTIF\"},{\"posted_by\":\"posted_date\"}}";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "======== Message data payload======: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "======= Message Notification Body======: " + remoteMessage.getNotification().getBody());
}
String title = remoteMessage.getData().get("judul_push");
String message = remoteMessage.getData().get("pesan_push");
String imageUriPush = remoteMessage.getData().get("image_push");
String imageUriSmall = remoteMessage.getData().get("icon_kecil");
String imageUriLarge = remoteMessage.getData().get("icon_besar");
String background = remoteMessage.getData().get("is_background");
String type = remoteMessage.getData().get("tipe_push");
String time = remoteMessage.getData().get("timestamp");
String datas = remoteMessage.getData().get("payload");
String imid = remoteMessage.getData().get("IMAGE_ID");
try {
final int numberOfItemsInResp = mJsonArrayPayload.length();
for (int i = 0; i < numberOfItemsInResp; i++) {
JSONObject mJsonObjectPayload = mJsonArrayPayload.getJSONObject(i);
String image_id = mJsonObjectPayload.getString("IMAGE_ID");
Config.abc1 = image_id;
String image_type = mJsonObjectPayload.getString("IMAGE_TYPE");
Config.abc2 = image_type;
String image_name = mJsonObjectPayload.getString("IMAGE_NAME");
Config.abc2 = image_name;
String image_path = mJsonObjectPayload.getString("IMAGE_PATH");
Config.abc3 = image_path;
String image_sequence = mJsonObjectPayload.getString("IMAGE_SEQUENCE");
Config.abc4 = image_sequence;
String aktif = mJsonObjectPayload.getString("AKTIF");
Config.abc5 = aktif;
String posted_by = mJsonObjectPayload.getString("posted_by");
Config.abc6 = posted_by;
String posted_date = mJsonObjectPayload.getString("posted_date");
Config.abc7 = posted_date;
}
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception HERE!!!!!!!!!!!!", e);
}Log.i("PVL", "==============++LOG TYPE PUSH++======= " +Config.abc1);
Log.i("PVL", "==============++LOG IMAGE NAME++======= " +Config.abc2);
Log.i("PVL", "==============++LOG IMAGE PATH++======= " +Config.abc3);
Log.i("PVL", "==============++LOG IMAGE SEQUENCE++======= " +Config.abc4);
Log.i("PVL", "==============++LOG AKTIF++======= " +Config.abc5);
Log.i("PVL", "==============++LOG POST BY++======= " +Config.abc6);
Log.i("PVL", "==============++LOG POST DATE++======= " +Config.abc7);
Log.i("PVL", "==============++LOG IMID++======= " +imid);
bitmapSmall = getBitmapfromUrlSmall(imageUriSmall);
Log.i("PVL", "==============++LOG BITMAP BITMALSMALL++======= " +bitmapSmall);
bitmapLarge = getBitmapfromUrlLarge(imageUriLarge);
Log.i("PVL", "==============++LOG BITMAP GETBITMAP++======= " +bitmapLarge);
bitmapPush = getBitmapfromUrlPush(imageUriPush);
Log.i("PVL", "==============++LOG BITMAP GETBITMAP PUSH++======= " +bitmapPush);
Log.i("PVL", "==============++LOG IS BACKGROUND++======= " +background);
Log.i("PVL", "==============++LOG TYPE PUSH++======= " +type);
Log.i("PVL", "==============++LOG TIMESTAMP++======= " +time);
sendNotification(message, title, bitmapSmall, bitmapLarge, bitmapPush, imageUriPush);
}public void sendNotification(String messageBody, String title, Bitmap imageSmall, Bitmap imageLarge,Bitmap imagePush, String imageUriPush) {
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
Context context = getApplicationContext();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
try {
s.bigLargeIcon(imageLarge);
s.bigPicture(Picasso.with(context).load(imageUriPush).get());
} catch (IOException e) {
e.printStackTrace();
}
notificationBuilder.setStyle(s);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.firebase_icon)
.setLargeIcon(imageLarge);
} else {
notificationBuilder.setSmallIcon(R.drawable.firebase_icon);
}
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifID =(int)System.currentTimeMillis();
notificationManager.notify(notifID /* ID of notification */, notificationBuilder.build());
}public Bitmap getBitmapfromUrlSmall(String imageUrlSmall) {
try {
Log.d(TAG, "======Log Image Bitmap: " +imageUrlSmall );
URL url = new URL(imageUrlSmall);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmapSmall = BitmapFactory.decodeStream(input);
return bitmapSmall;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d(TAG, "+++++LOG IMAGE ++++" +e.getMessage() );
e.printStackTrace();
return null;
}
}
public Bitmap getBitmapfromUrlLarge(String imageUrlLarge) {
try {
Log.d(TAG, "======Log Image Bitmap: " +imageUrlLarge );
URL url = new URL(imageUrlLarge);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmapLarge = BitmapFactory.decodeStream(input);
return bitmapLarge;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d(TAG, "+++++LOG IMAGE ++++" +e.getMessage() );
e.printStackTrace();
return null;
}
}
public Bitmap getBitmapfromUrlPush(String imageUrlPush) {
try {
Log.d(TAG, "======Log Image Push Bitmap: " +imageUrlPush );
URL url = new URL(imageUrlPush);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmapPush = BitmapFactory.decodeStream(input);
return bitmapPush;
} catch (Exception e) {
Log.d(TAG, "+++++LOG IMAGE PUSH++++" +e.getMessage() );
e.printStackTrace();
return null;
}
}
}


here is the json that I'm trying to parse.

{ "data": {
"judul_push":"jdl",
"pesan_push":"Isi",
"image_push": "xxx.png",
"icon_kecil": "xxx.png",
"icon_besar": "xxx.png",
"is_background":true,
"tipe_push":1,
"timestamp":"27-10-2016 22:51:48",
"payload":{
"IMAGE_ID":"1",
"IMAGE_TYPE":"5",
"IMAGE_NAME":"SPLASH SCREEN",
"IMAGE_PATH":"xxx.png",
"IMAGE_SEQUENCE":"1",
"AKTIF":"Y",
"posted_by":"Agus Awaludin",
"posted_date":"27-10-2016 22:51:48"
}
},
"to" : "fK2iL7Ptp9I:APA91bEFxlLyEIpr-vO2Fw3VoTOY6t-38EV_Thnn_SddL17nu7E1VcjZVEgpOAE7vEKn83Cxz3q9rXAapUq-00opHbuOrcw7AwPtFtyYAFb674olwttVLuQgCIByN-i_gJJy8z6rjUi7"
}


Here is my logcat error when Im trying to parse JSON object
payload
.

org.json.JSONException: Expected ':' after timestamp at character 128 of {"Data":{"judul_push":"pesan_push" ,"image_push":"icon_kecil","icon_besar":"icon_kecil","is_background":"tipe_push","timestamp"payload":{"IMAGE_ID":"IMAGE_TYPE" },{"IMAGE_NAME":"IMAGE_PATH"},{"IMAGE_SEQUENCE":"AKTIF"},{"posted_by":"posted_date"}}
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.readObject(JSONTokener.java:379)
at org.json.JSONTokener.nextValue(JSONTokener.java:100)
at org.json.JSONTokener.readObject(JSONTokener.java:385)
at org.json.JSONTokener.nextValue(JSONTokener.java:100)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.mitracomm.testingpush.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:100)
at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
at com.google.firebase.iid.zzb$2.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

Answer Source

I found the answer of my question. The answer is first if the firebase send us json data and any json object included, we need to parse json data to string and then we need to convert the string value of json object (payload) to data type json object. and finally we can get the string value (image_id) in json object (payload). thanks in advance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download