i have android chat app, , in 1 fragment called taskfragment there list of chats notification counter.
i have class called chatservice deals notifications, whenever notification comes through chatservice updates db increment notification number on particular task.
when taskfragment opens calls function called refreshtasks(), updates gui db.
my problem is, if user in taskfragment , notification, need call refreshtasks chatservice, how do that?
thanks.
you can use localbroadcastmanager
purpose.
idea send broadcast service when new message received , receive on fragment
class yourservice extends gcmlistenerservice{ @override public void onmessagereceived(string from, bundle bundle) { ... intent pushnotification = new intent("pushnotification"); //put data using intent.putextra() method localbroadcastmanager.getinstance(this).sendbroadcast(pushnotification); ... } }
now receive on fragment:
class taskfragment extends fragment{ private broadcastreceiver mbroadcastreceiver; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { ... mbroadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { if (intent.getaction().equals("pushnotification")) { // new push message received //update ui handlepushnotification(intent); } } }; ... } @override protected void onresume() { super.onresume(); // registering receiver new notification localbroadcastmanager.getinstance(getactivity()).registerreceiver(mbroadcastreceiver, new intentfilter("pushnotification")); } @override protected void ondestroy() { //unregister receiver here localbroadcastmanager.getinstance(getactivity()).unregisterreceiver(mbroadcastreceiver); super.ondestroy(); } }
you can refer gist or find tutorial on web.
Comments
Post a Comment