2

如何獲取包含在onMessageReceived方法JSON對象data,我從remoteMessage了JSON對象​​的價值image_id獲取JSON值從remoteMessage收到onMessageReceived方法[FCM]推送通知

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對象​​。

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) 
+0

錯誤輸出似乎是'strJSONValue'值。這是做什麼的? – Searching

+0

我不知道我必須做什麼。當我試圖解析JSON對象的有效載荷,然後logcat顯示我這樣的I/PVL:============== ++ LOG PAYLOAD ++ ======= {「IMAGE_TYPE」:「5 「,」AKTIF「:」Y「,」IMAGE_PATH「:」xxx.png「,」IMAGE_SEQUENCE「:」1「,」IMAGE_NAME「:」SPLASH SCREEN,「IMAGE_ID」:「1」,「posted_by」 Awaludin「,」posted_date「:」27-10-2016 22:51:48「} –

+0

該日誌輸出與該問題無關,但該日誌不是問題。看起來沒問題。產生錯誤的代碼部分你可以檢查你是否發送/發佈了'strJSONValue'並提供了這個代碼嗎? – Searching

回答

0

我找到了答案。

答案是,首先,如果火力地堡向我們發送JSON數據和任何JSON對象包括,我們需要解析JSON數據字符串,然後我們需要JSON對象(有效載荷)的字符串值轉換爲數據類型JSON目的。最後,我們可以在JSON對象(有效內容)中獲取字符串值(image_id)。

+0

我收到了JSON空白......可能是什麼問題? –

0

有一個類似的問題,我刪除了JSON數據,它從那裏抱怨字符,然後手工重新輸入它。不知何故,我有一個隱藏的角色。希望這可以幫助。

+0

這個問題沒有提供答案,一旦你有足夠的[聲譽](https:// stackoverflow。 com/help/whats-reputation)你可以在任何帖子上[評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案。 ](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-commen t-what-c​​an-I-do- – Bruce