android - I received notification from firebase but how can i display these messages of notification and transfer it to my mainlayout? -


this code of firebase messaging service:

public class myfirebasemessagingservice extends firebasemessagingservice{ private static final string tag="myfirebasemsgservice";  @override public void onmessagereceived(remotemessage remotemessage) {     bundle bundle = new bundle();     bundle.putstring("msgbody", remotemessage.getnotification().getbody());      intent new_intent = new intent();     new_intent.setaction("action_string_activity");     new_intent.putextra("msg", bundle);     sendbroadcast(new_intent);     log.d(tag,"from:"+remotemessage.getfrom());     //check if message contains data     if(remotemessage.getdata().size()>0){         log.d(tag,"message data:"+remotemessage.getdata());          //check if message contains notification         if(remotemessage.getnotification()!=null){             log.d(tag,"message body:"+remotemessage.getnotification().getbody());             sendnotification(remotemessage.getnotification().getbody());          }     } } 

/* * display notification code display notification code display notification code display notification * */

private void sendnotification(string body) {     intent intent=new intent(this,mainactivity.class);     intent.setflags(intent.flag_activity_clear_top);     pendingintent pendingintent=pendingintent.getactivity(this,0/*request code*/,intent,pendingintent.flag_one_shot);     //set sound notification     uri notifictaionsound= ringtonemanager.getdefaulturi(ringtonemanager.type_notification);              notificationcompat.builder notifibuilder=new               notificationcompat.builder(this)             .setsmallicon(r.mipmap.ic_launcher)             .setcontenttitle("firebase cloud messaging")             .setcontenttext(body)             .setautocancel(true)             .setsound(notifictaionsound)             .setcontentintent(pendingintent);     notificationmanager notificationmanager=     (notificationmanager)getsystemservice(context.notification_service);     notificationmanager.notify(0/*id of      notification*/,notifibuilder.build());  } 

in service onmessagereceived() call method

if(remotemessage.getnotification()!=null){         log.d(tag,"message body:"+remotemessage.getnotification().getbody());         sendnotification(remotemessage.getnotification().getbody());         sendmessagetoactivity();      } 

and add method

private static void sendmessagetoactivity(string msg) {     intent intent = new intent("notification");     // can include data.     intent.putextra("status", msg);     localbroadcastmanager.getinstance(context).sendbroadcast(intent); } 

in activity want receive

private broadcastreceiver mmessagereceiver = new broadcastreceiver() {     @override     public void onreceive(context context, intent intent) {         // data included in intent         string message = intent.getstringextra("status");         //do want notification      } }; localbroadcastmanager.getinstance(getactivity()).registerreceiver(             mmessagereceiver, new intentfilter("notification")); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -