0

I am receiving a String from GCM in JSON Format . I want to Parse that String and then generate notification.i want that it should generate notification on the basis of tag for Example if tag is "comment" then it should generate the notification for comment. I am new to Android. the String that i received is {"tag":"comment","sender":null,"content":null} my Code is

public void categorizNotifications(String msg){
    try {
        JSONObject json=new JSONObject(msg);
        String Tag=json.getString(TAG);
        if(Tag=="comment"){
            String Content=json.getString(CONTENT);

            String Sender=json.getString(SENDER);
            generateNotificationForComment(Content,Sender);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void generateNotificationForComment(String content2, String sender2) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Comments.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_action_event)
            .setContentTitle(sender2+" add a comment")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(content2))
            .setContentText(content2);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    //Log.d(TAG, "Notification sent successfully.");

}
  • 1
    Ask a real question and explain the actual problem. (And please use `camelCase` variables.) – user2864740 Oct 24 '14 at 18:55
  • Please add a stacktrace if by "getting problem" you mean it's crashing. And if not, explain what "getting problem" means. On a sidenote: `Tag=="comment"` => http://stackoverflow.com/q/513832/995891 – zapl Oct 24 '14 at 18:55
  • http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – user2864740 Oct 24 '14 at 18:56
  • use gson with custom objects – mapodev Oct 24 '14 at 19:05
  • @user2864740 .i want that it should generate notification on the basis of tag for Example if tag is "comment" then it should generate the notification for comment. – arsalan qamar Oct 24 '14 at 19:06
  • you can us gson or you can write your own parser. up to you. Id use gson. – NightSkyCode Oct 24 '14 at 19:15
  • You can't compare strings like this... `if(Tag=="comment")`. Use `equals(...)` instead. – Squonk Oct 24 '14 at 19:17
  • @zapl my app is not generating notification on the basis of tag. i am just getting problem in json parsing. i don't know how to parse a String that is in JSON format. the methods that is available on the internet is also not working. – arsalan qamar Oct 24 '14 at 19:17
  • @arsalanqamar the code you posted looks ok. It should parse json where it says `new JSONObject(msg)`. You could use alternative json parsing implementations like gson but they won't help if the problem is that you're doing string comparisons wrong. `generateNotificationForComment(Content,Sender);` is basically guaranteed to be **never** called in your code because of the `==` instead of `equal` – zapl Oct 24 '14 at 20:34
  • @zapl You are right i finally figured out the problem. i will now keep these things in mind thanxx – arsalan qamar Oct 26 '14 at 18:04

1 Answers1

1

There is a few ways to parse a json object. I personally suggest the google gson library

  1. You have to download the library jar and add it to your project.
  2. Then you have to write a model class of your object that you want to parse.

    public class YourObjectModelClass{
       public String tag;
       public String sender;
       public String content;
    }
    
  3. At the categorizNotifications() you will replace the code :

    JSONObject json=new JSONObject(msg);
    Gson gson = new Gson();
    
    YourObjectModelClass model = gson.fromJson(json.toString(), YourObjectModelClass.class); //
    String tag = model.tag;
    if(tag.equals("comment"){
      String sender = model.sender;
      String content = model.content;
    }
    

    and so on...

karvoynistas
  • 1,295
  • 1
  • 14
  • 30