java - How to open the particular fragment on the click of the push notification message in the notification tab? -


this gcmintentservice class sending message. problem when click on push notification message opens main activity.but want open particular fragment.i knew changes in sendnotification()method. can tell me how can open particular fragment on click of push notification ?

public class gcmintentservice extends intentservice {      public static final int notification_id = 1;     private notificationmanager mnotificationmanager;     private final static string tag = "gcmintentservice";      public gcmintentservice() {         super("gcmintentservice");     }      @override     protected void onhandleintent(intent intent) {         bundle extras = intent.getextras();           googlecloudmessaging gcm = googlecloudmessaging.getinstance(this);         string messagetype = gcm.getmessagetype(intent);          if (!extras.isempty()) {             if (googlecloudmessaging.message_type_send_error                     .equals(messagetype)) {                 sendnotification("send error: " + extras.tostring());              } else if (googlecloudmessaging.message_type_deleted                     .equals(messagetype)) {                 sendnotification("deleted messages on server: " + extras.tostring());              } else if (googlecloudmessaging.message_type_message                     .equals(messagetype)) {                  (int = 0; < 5; i++) {                     log.d(tag, " working... " + (i + 1) + "/5 @ "                             + systemclock.elapsedrealtime());                     try {                         thread.sleep(5000);                     } catch (interruptedexception e) {                         e.printstacktrace();                     }                 }                  sendnotification(extras.getstring("message"));             }         }      wakefulbroadcastreceiver.         gcmbroadcastreceiver.completewakefulintent(intent);     }           private void sendnotification(string msg) {         mnotificationmanager = (notificationmanager)                 .getsystemservice(context.notification_service);  pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, mainactivity.class), 0);           notificationcompat.builder mbuilder = (notificationcompat.builder) new notificationcompat.builder(this)                 .setsmallicon(getnotificationicon())                 .setcontenttitle("telepoh")                 .setstyle(new notificationcompat.bigtextstyle().bigtext(msg))                 .setcontenttext(msg)                 .setdefaults(notification.default_sound | notification.default_vibrate);          mbuilder.setcontentintent(contentintent);         mbuilder.getnotification().flags |= notification.flag_auto_cancel;         mbuilder.setautocancel(true);          mnotificationmanager.notify(notification_id, mbuilder.build());     }      private int getnotificationicon() {         boolean usewhiteicon = (build.version.sdk_int >= build.version_codes.lollipop);         return usewhiteicon ? r.drawable.gcm : r.drawable.push_icon;     } } 

this fragment want open on click of push notification message:-

public class notificationactivity extends fragment {      progressdialog pd;     private sharedpreferencesutilities sharedpreferencesutilities;     private generalutilities generalutilities;     private view rootview;     private listview listview;     private textview txtkm;     private textview emptyview;     int progressvalue=0;     int progressvalue2;     public notificationactivity notilist = null;     httpresponse response;     public arraylist<listviewitem> notiarray = new arraylist<listviewitem>();      public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         if (rootview == null) {             rootview = inflater.inflate(r.layout.notification_screen, container, false);             getactivity().settitle("notifications");             generalutilities = new generalutilities(getactivity());             sharedpreferencesutilities = new sharedpreferencesutilities(getactivity());              notilist = this;             new getnotificationdata().execute();         }         return rootview;     }      public class getnotificationdata extends asynctask<string , string , string> {          @override         protected void onpreexecute() {             super.onpreexecute();             pd = new progressdialog(getactivity());             pd.setcancelable(true);             pd.setmessage("loading...");             pd.setprogressstyle(progressdialog.style_spinner);             pd.show();         }          @override         protected string doinbackground(string... params) {             notificationlistdata();             return null;         }          @override         protected void onpostexecute(string s) {             super.onpostexecute(s);             pd.dismiss();              if (generalutilities.isconnected()) {                  httpentity entity = response.getentity();                 string json = null;                 try {                     json = entityutils.tostring(entity);                 } catch (ioexception e) {                     e.printstacktrace();                 }                  try {                     final jsonobject jobject = new jsonobject(json);                       if (jobject.getstring("replycode").equals("1")) {                         jsonarray jsonuserobject = jobject.getjsonarray("data");                           (int = 0; < jsonuserobject.length(); i++) {                              notiarray.add(new listviewitem(jsonuserobject.getjsonobject(i).getstring("otherusername"),                                     jsonuserobject.getjsonobject(i).getstring("userprofilepic"), jsonuserobject.getjsonobject(i).getstring("status"),                                     jsonuserobject.getjsonobject(i).getstring("id"),jsonuserobject.getjsonobject(i).getstring("eventsid"),                                     jsonuserobject.getjsonobject(i).getstring("otheruserid")));                         }                          resources res =getresources();                         listview = (listview) rootview.findviewbyid(r.id.notification_list);                         emptyview = (textview) rootview.findviewbyid(r.id.empty_view);                          if (notiarray.isempty()) {                             listview.setvisibility(view.gone);                             emptyview.setvisibility(view.visible);                         }                         else {                             listview.setvisibility(view.visible);                             emptyview.setvisibility(view.gone);                             notificationadapter nadapter = new notificationadapter(notilist, notiarray, res);                             listview.setadapter(nadapter);                         }                     } else {                         generalutilities.showalertdialog("request cancelled", new jsonobject(json).getstring("message"), "ok");                      }                 } catch (jsonexception e) {                     e.printstacktrace();                 }             } else {                 generalutilities.showalertdialog("error", getresources().getstring(r.string.internet_error), "ok");             }         }     }      public void notificationlistdata() {          httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(getresources().getstring(r.string.api_end_point) + "shownotification/notificationdata");         httppost.setheader(http.content_type, "application/x-www-form-urlencoded;charset=utf-8");         try {              list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(1);              namevaluepairs.add(new basicnamevaluepair("id", string.valueof(sharedpreferencesutilities.getuserid())));             httppost.setentity(new urlencodedformentity(namevaluepairs, "utf-8"));              response = httpclient.execute(httppost);         } catch (clientprotocolexception e) {           e.printstacktrace();         } catch (ioexception e) {              e.printstacktrace();         }     }     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         sethasoptionsmenu(true);     }     @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         inflater.inflate(r.menu.main_distance, menu);     }     @override     public boolean onoptionsitemselected(menuitem item) {          switch (item.getitemid()) {              case r.id.action_distance:                  alertdialog.builder dialogbuilder = new alertdialog.builder(getactivity());                 layoutinflater inflater = getactivity().getlayoutinflater();                 view dialogview = inflater.inflate(r.layout.setdistance_popup, null);                 dialogbuilder.setview(dialogview);                 final alertdialog alertdialog = dialogbuilder.create();                 alertdialog.show();                 textview txttitle = (textview) dialogview.findviewbyid(r.id.textview58);                 txtkm = (textview) dialogview.findviewbyid(r.id.textview500);                 textview  txthn = (textview) dialogview.findviewbyid(r.id.textview59);                 textview txtdiscription = (textview) dialogview.findviewbyid(r.id.textview63);                 linearlayout btnset = (linearlayout) dialogview.findviewbyid(r.id.buttonset);                 linearlayout btncancel = (linearlayout) dialogview.findviewbyid(r.id.buttoncancel);                 txthn.settext(10+sharedpreferencesutilities.getradiodistance());                 seekbar popupseek = (seekbar) dialogview.findviewbyid(r.id.seekbar2);                  if(sharedpreferencesutilities.getprofiledistance()=="")                 {                     popupseek.setprogress(0);                     txtkm.settext("500 meter");                 }                 else                 {                     integer checkcount = integer.parseint(sharedpreferencesutilities.getprofiledistance());                     if(checkcount==0)                     {                         popupseek.setprogress(0);                         txtkm.settext("500 meter");                     }                     else                     {               popupseek.setprogress(integer.parseint(sharedpreferencesutilities.getprofiledistance()));                         txtkm.settext(sharedpreferencesutilities.getprofiledistance() + sharedpreferencesutilities.getradiodistance());                     }                 }                  popupseek.setmax(10);                 popupseek.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() {                      @override                     public void onstoptrackingtouch(seekbar seekbar) {                     }                      @override                     public void onstarttrackingtouch(seekbar seekbar) {                     }                      @override                     public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {                         // progress = ((int)math.round(progress/0.5));                          progressvalue2 = progressvalue+progress;                          if(progress==0)                         {                             txtkm.settext("500 meter");                          }                         else {                             txtkm.settext(integer.tostring(progressvalue2) + sharedpreferencesutilities.getradiodistance());                         }                     }                 });                  btnset.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view v) {                         sharedpreferencesutilities.setprofiledistance(string.valueof(progressvalue2));                         alertdialog.dismiss();                     }                 });                  btncancel.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view v) {                         alertdialog.dismiss();                     }                 });                  return true;              default:                 return super.onoptionsitemselected(item);         }     } } 

manage flag , pass information notification mainactivity via intent.

use switch case or if/else in mainactivity, if receiving perticular flag/data, load desired fragment.

intent passintent = new intent(this, mainactivity.class); passintent.putextra("flag","some value"); pendingintent contentintent = pendingintent.getactivity(this, 0, passintent, 0); 

in mainactivity.java

if(getintent().hasextra("flag")){     if(getintent.getstringextra("flag").equalsignorcase("some value"))     {           //write code load fragment.     } } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -