java - How to implement Custom Notification view with direct reply in Android? -


i implement custom push notification direct reply option in android. created custom notification in xml , shown remote view. working fine show xml layout. add direct reply layout. how can achieve it?

i need this. have custom ui. along that, need reply , quick reply option.

my working sample this:

mainactivity.java:

public class mainactivity extends appcompatactivity {  /**  * gets intent reply message  * @param context  * @param reply  * @return  */  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main); } } 

myfirebaseinstanceidservice.java:

public class myfirebaseinstanceidservice extends firebaseinstanceidservice {  public static final string tag = "my firebase";  @override public void ontokenrefresh() {     super.ontokenrefresh();     string refreshedtoken = firebaseinstanceid.getinstance().gettoken();     log.d(tag, "refreshed token :: "+ refreshedtoken); } 

myfirebasemessagingservice.java:

public class myfirebasemessagingservice extends firebasemessagingservice {    public static final string tag = "message service"; private static string key_reply = "key_reply_message"; public static string reply_action = "fcmsample.fcmsample.reply_action"; private int mmessageid; int notificationid = 0;  public myfirebasemessagingservice() { }  @override public void onmessagereceived(remotemessage remotemessage) {     super.onmessagereceived(remotemessage);     log.d(tag, "from: "+ remotemessage.getfrom());      // check if message contains data payload     if (remotemessage.getdata().size() > 0) {         log.d(tag, "message data payload: "+ remotemessage.getdata());     }      // check if message contains notification payload     if (remotemessage.getnotification() != null) {         log.d(tag, "message notification body: "+ remotemessage.getnotification());         string body = remotemessage.getnotification().getbody();         string title = remotemessage.getnotification().gettitle();         sendnotification(title, body, " ", 0," "); // todo: need work on     } } private void sendnotification(string title, string messagebody, string id, int type, string cid) {     intent intent = new intent(this, mainactivity.class);     intent.addflags(intent.flag_activity_clear_top);     intent.putextra("id", id);     intent.putextra("type", type);     intent.putextra("cid", cid);      id  = "1";     if (type == 1){         notificationid = integer.parseint(id);     } else {         notificationid = integer.parseint(id);         int randomno = new random().nextint();         notificationid = notificationid + randomno;     }     remoteviews expandedview = new remoteviews(getpackagename(), r.layout.news_notification_expanded);      expandedview.settextviewtext(r.id.tvnewstitle, "title");                 string publishdate = utils.formatdatestring(model.getpublishdate(), "mmm dd");                 expandedview.settextviewtext(r.id.tvpublisheddate, "published on: "+ "date");                 string snippet = model.getnewssnippet();                 expandedview.settextviewtext(r.id.tvsnippet, "desc");      pendingintent pendingintent = pendingintent.getactivity(this, notificationid /* request code */, intent,             pendingintent.flag_one_shot);      uri defaultsounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification);     notificationcompat.builder notificationbuilder = new notificationcompat.builder(this)             .setsmallicon(r.mipmap.ic_launcher)             .setcontenttitle(title)             .setcontenttext(messagebody)             .setautocancel(true)             .setsound(defaultsounduri)             .setcontentintent(pendingintent);       /* custom notification view begins here      *      */       // 1. build label     string replylabel = getstring(r.string.notif_action_reply1);     remoteinput remoteinput = new remoteinput.builder(key_reply)             .setlabel(replylabel)             .build();      // 2. build action     notificationcompat.action replyaction = new notificationcompat.action.builder(             r.drawable.ic_notif_action_reply, replylabel, getreplypendingintent())             .addremoteinput(remoteinput)             .setallowgeneratedreplies(true)             .build();      // 3. build notification     uri sounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification);     notificationcompat.builder mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_notif_smile)             .setcontenttitle(title)             .setcontenttext(messagebody)             .setautocancel(true)             .setshowwhen(true)             .setsound(sounduri)              .setcustombigcontentview(expandedview)             .addaction(replyaction);      notificationmanagercompat mnotificationmanager = notificationmanagercompat.from(this);     mnotificationmanager.notify(notificationid, mbuilder.build()); }  private pendingintent getreplypendingintent() {     intent intent;     mmessageid = 123; // dummy message id, ideally come push notification      if (build.version.sdk_int >= build.version_codes.n) {          intent = notificationbroadcastreceiver.getreplymessageintent(this, notificationid, mmessageid);         return pendingintent.getbroadcast(getapplicationcontext(), 100, intent,                 pendingintent.flag_update_current);     } else {         // start activity         intent = replyactivity.getreplymessageintent(this, notificationid, mmessageid);         intent.addflags(intent.flag_activity_new_task);         return pendingintent.getactivity(this, 100, intent, pendingintent.flag_update_current);     } }  public static charsequence getreplymessage(intent intent) {     bundle remoteinput = remoteinput.getresultsfromintent(intent);     if (remoteinput != null) {         return remoteinput.getcharsequence(key_reply);     }     return null; }} 

notificationbroadcastreceiver.java:

public class notificationbroadcastreceiver extends broadcastreceiver { private static string key_notification_id = "key_noticiation_id"; private static string key_message_id = "key_message_id";  public static intent getreplymessageintent(context context, int notificationid, int messageid) {     intent intent = new intent(context, notificationbroadcastreceiver.class);     intent.setaction(reply_action);     intent.putextra(key_notification_id, notificationid);     intent.putextra(key_message_id, messageid);     return intent; }  public notificationbroadcastreceiver() { }  @override public void onreceive(context context, intent intent) {     if (reply_action.equals(intent.getaction())) {         // whatever want message. send server or add db.         // tutorial, we'll show in toast;         charsequence message = myfirebasemessagingservice.getreplymessage(intent);         int messageid = intent.getintextra(key_message_id, 0);          toast.maketext(context, "message id: " + messageid + "\nmessage: " + message,                 toast.length_short).show();          // update notification         int notifyid = intent.getintextra(key_notification_id, 1);         updatenotification(context, notifyid);     } }  private void updatenotification(context context, int notifyid) {     notificationmanagercompat notificationmanager = notificationmanagercompat.from(context);      notificationcompat.builder builder = new notificationcompat.builder(context)             .setsmallicon(r.drawable.ic_notif_smile)             .setcontenttext(context.getstring(r.string.notif_content_sent));      notificationmanager.notify(notifyid, builder.build()); }} 

replyactivity.java:

public class replyactivity extends appcompatactivity {  private static final string key_message_id = "key_message_id"; private static final string key_notify_id = "key_notify_id";  private int mmessageid; private int mnotifyid;  private imagebutton msendbutton; private edittext meditreply;  public static intent getreplymessageintent(context context, int notifyid, int messageid) {     intent intent = new intent(context, replyactivity.class);     intent.setaction(reply_action);     intent.putextra(key_message_id, messageid);     intent.putextra(key_notify_id, notifyid);     return intent; } @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_reply);      intent intent = getintent();      if (reply_action.equals(intent.getaction())) {         mmessageid = intent.getintextra(key_message_id, 0);         mnotifyid = intent.getintextra(key_notify_id, 0);     }      meditreply = (edittext) findviewbyid(r.id.edit_reply);     msendbutton = (imagebutton) findviewbyid(r.id.button_send);      msendbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             sendmessage(mnotifyid, mmessageid);         }     }); }  private void sendmessage(int notifyid,  int messageid) {     // update notification     updatenotification(notifyid);      string message = meditreply.gettext().tostring().trim();     // handle send message     toast.maketext(this, "message id: " + messageid + "\nmessage: " + message,             toast.length_short).show();      finish(); }  private void updatenotification(int notifyid) {     notificationmanagercompat notificationmanager = notificationmanagercompat.from(this);      notificationcompat.builder builder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_notif_smile)             .setcontenttext(getstring(r.string.notif_content_sent));      notificationmanager.notify(notifyid, builder.build()); }} 

activity_reply.xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_reply" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingbottom="45dp" android:paddingtop="45dp" android:background="@color/windowbackground" android:gravity="center" tools:context="fcmsample.fcmsample.replyactivity">  <edittext     android:id="@+id/edit_reply"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_centervertical="true"     android:layout_toleftof="@+id/button_send"     android:hint="@string/hint_reply"/>  <imagebutton     android:id="@+id/button_send"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentright="true"     android:layout_centervertical="true"     android:clickable="true"     android:background="@null"     android:padding="8dp"     android:src="@drawable/ic_send"/>    </relativelayout> 

string.xml:

<resources> <string name="app_name">fcmsample</string> <string name="text_show_notif">show notification</string>  <string name="notif_content_sent">sent</string> <string name="notif_title">message received</string> <string name="notif_content">please type reply below</string> <string name="notif_action_reply1">reply</string> <string name="hint_reply">write reply</string> </resources> 

news_notification_expanded.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">  <linearlayout     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:id="@+id/titlelayout">     <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="left"         android:src="@mipmap/ic_launcher" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/app_name"         android:id="@+id/textview2"         android:layout_weight="1"         android:textcolor="@color/se_black"         android:layout_gravity="center" />     <!--<button-->         <!--android:layout_width="wrap_content"-->         <!--android:layout_height="wrap_content"-->         <!--android:id="@+id/btnclose"-->         <!--android:background="@color/se_white"-->         <!--android:textcolor="@color/se_black"-->         <!--android:text="x"-->         <!--android:layout_gravity="right" />-->   </linearlayout>   <textview     android:layout_width="match_parent"     android:layout_height="@dimen/one_dp"     android:background="@color/se_medium_grey"     android:id="@+id/view" />  <linearlayout     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:id="@+id/bodylayout">     <linearlayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="@dimen/five_dp"         android:id="@+id/linearlayout">         <textview             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_marginleft="2dp"             android:id="@+id/tvnewstitle"             android:textstyle="bold"             android:layout_weight="1"             android:text="title news"             android:textcolor="@color/se_black"/>         <textview             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_marginright="5dp"             android:layout_weight="1"             android:gravity="left"             android:id="@+id/tvpublisheddate"             android:textcolor="@color/se_black"             android:text="21st november" />      </linearlayout>     <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginleft="2dp"         android:layout_marginright="2dp"         android:layout_marginbottom="@dimen/five_dp"         android:id="@+id/tvsnippet"         android:textcolor="@color/se_black"         android:text="title news snippet"/> </linearlayout>  <textview     android:layout_width="match_parent"     android:layout_height="@dimen/one_dp"     android:background="@color/se_medium_grey"     android:id="@+id/view1" />  <textview     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margintop="@dimen/five_dp"     android:layout_marginleft="2dp"     android:layout_marginright="2dp"     android:id="@+id/tvdescription"     android:textcolor="@color/se_black"     android:text="title news description"/>  </linearlayout> 

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fcmsample.fcmsample">  <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundicon="@mipmap/ic_launcher_round"     android:supportsrtl="true"     android:theme="@style/apptheme">     <activity android:name=".mainactivity">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>      <service android:name=".myfirebaseinstanceidservice">         <intent-filter>             <action android:name="com.google.firebase.instance_id_event" />         </intent-filter>     </service>     <service         android:name=".myfirebasemessagingservice">         <intent-filter>             <action android:name="com.google.firebase.messaging_event"/>         </intent-filter>     </service>      <receiver         android:name=".notificationbroadcastreceiver"         android:enabled="true"         android:exported="false" />     <activity android:name=".replyactivity"/> </application>  </manifest> 

please me out if 1 come across issues this. in advance. :)


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -