获取JSON值从remoteMessage Received onmessagerreceived方法[FCM]推送通知



如何在json对象data中获得json对象payload的值image_id,我从onMessageReceived方法中包含的remoteMessage获得?

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

这是我要解析的json。

{ "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"
    }
},
  "to" : "fK2iL7Ptp9I:APA91bEFxlLyEIpr-vO2Fw3VoTOY6t-38EV_Thnn_SddL17nu7E1VcjZVEgpOAE7vEKn83Cxz3q9rXAapUq-00opHbuOrcw7AwPtFtyYAFb674olwttVLuQgCIByN-i_gJJy8z6rjUi7"
}

这是我的logcat错误,当我试图解析JSON对象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)

有一个类似的问题,我从抱怨字符的地方删除了JSON数据,然后手工重新键入。不知怎么的,我有一个隐藏的性格。

我找到了答案。

答案是,首先,如果Firebase发送给我们JSON数据和任何JSON对象,我们需要将JSON数据解析为字符串,然后我们需要将JSON对象(payload)的字符串值转换为数据类型JSON对象。最后,我们可以在JSON对象(payload)中获得字符串值(image_id)。

最新更新