2

をonMessageReceived受信からJSON値を取得するにはどうすればonMessageReceived方法に含まれている私はremoteMessageからもらったJSONオブジェクトdataでJSONオブジェクト​​上の値image_idを得るのですか?ここremoteMessageは法[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です。イムは、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エラーです。

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

私は何をしなければならないのか分かりません。 Imが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 ":" Agus Awaludin "、" posted_date ":" 27-10-2016 22:51:48 "} –

+0

ログの出力は問題とは関係ありませんが、ログは問題ではありません。あなたが 'strJSONValue'を送って/掲示していて、そのコードを提供しているかどうか確認できますか? – Searching

答えて

0

回答が見つかりました。

Firebaseが私たちにJSONデータとJSONオブジェクトを送信した場合、まずJSONデータを文字列に解析し、JSONオブジェクト(ペイロード)の文字列値をJSONデータ型に変換する必要がありますオブジェクト。最後に、JSONオブジェクト(ペイロード)で文字列値(image_id)を取得できます。

+0

私はJSONを受け取った空...何が問題だろうか? –

0

同様の問題がありましたが、私はJSONデータを文字について不平を言っていた場所から削除し、手動で再入力しました。どういうわけか私は隠された性格を持っていたお役に立てれば。

+0

これは質問に対する答えを提供しません。あなたは[コメント](https://stackoverflow.com/help/privileges/comment)を投稿することができます;代わりに[質問者からの説明を必要としない回答を提供します。 ](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-commen私は何をすることができますか) – Bruce

関連する問題