SoFunction
Updated on 2025-04-07

Notification message notification Custom message notification content layout

Specific operations: Customize the layout of message notification content; click the "Click to Send Message" TextView control in the center of the interface to simulate sending notification messages, receive messages in the notification bar, and send them several times when clicking them, click the notification bar message, and jump to the details interface.

1.activity_main.xml:

<LinearLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  tools:context=".MainActivity">

  <TextView
    android:
    android:text="Click to send a message"
    android:textSize="24sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

2.layout_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <ImageView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@mipmap/ic_launcher" />

  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="18dp"
    android:layout_toRightOf="@+id/imageView1"
    android:text="TextView" />

  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="15dp"
    android:text="TextView" />

  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="54dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="TextView" />

  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:text="TextView" />

</RelativeLayout>

3.activity_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">


  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:
    android:textSize="18sp"
    android:layout_gravity="center_horizontal"/>

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:
    android:textSize="18sp"
    android:layout_gravity="center_horizontal"/>

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:
    android:textSize="18sp"
    android:layout_gravity="center_horizontal"/>
</LinearLayout>

kind:

/**
  * Used to store the number of messages
  * Created by admin on 2017/9/4.
  */

public class SingletonF{

  private int info_number=0;

  private static class SingletonHolder {
    /**
      * Singleton object instance
      */
    static final SingletonF INSTANCE = new SingletonF();
  }

  public static SingletonF getInstance() {
    return ;
  }

  /**
   * Private constructor is used to avoid the external world using new to instantiate objects.
   */
  private SingletonF() {}

  public int getInfo_number() {
    return info_number;
  }

  public void setInfo_number(int info_number) {
    this.info_number = info_number;
  }

//  /**
 // * If SingletonF implements Serializable, the readResolve method must be implemented
 // * readResolve method should be serialized when the singleton object is serialized
 // */
//  private Object readResolve() {
//    return getInstance();
//  }

}

kind:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;

public class MyBroadcastReceiver extends BroadcastReceiver {

  private SingletonF singletonF;
  private Context context1;

  @Override
  public void onReceive(Context context, Intent intent) {
    this.context1 = context;
// if (("info").equals("Broadcast sent")) {// ("static broadcast:", "I have accepted broadcast");//   }
    Message message = ();
     = 0;
    (message);
  }

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch () {
        case 0:
          singletonF = ();
          int first = singletonF.getInfo_number();
          first++;
          Date nowTime = new Date(());
          SimpleDateFormat sdFormatter = new SimpleDateFormat("HH:mm");
          String retStrFormatNowDate = (nowTime);
          singletonF.setInfo_number( first);

          RemoteViews contentViews = new RemoteViews((), .layout_item);
          // Set properties through the control's ID          (.imageView1, .ic_launcher_round);
          (.textView1, "Message Title");
          (.textView2, "Message Content");
          (.textView3, " (" + first + "New news)");
          (.textView4, "" + retStrFormatNowDate);
          // Click on the activity to jump to the notification bar          Intent intent = new Intent(context1, );
          PendingIntent pendingIntent = (context1, 0, intent,
              PendingIntent.FLAG_CANCEL_CURRENT);

           mBuilder = new (context1)
              .setSmallIcon(.ic_launcher_round).setTicker("new message");
          // Automatically manage notification bar messages          (true);
          (pendingIntent);
          /// Custom layout          (contentViews);
          // Use the default prompt tone          (Notification.DEFAULT_ALL);
          NotificationManager mNotificationManager = (NotificationManager) context1
              .getSystemService(context1.NOTIFICATION_SERVICE);
          // notify(int id, Notification notification) If the id is the same value, the notification column will only display one line and the content of this message will be updated continuously          // If it is a different unique identifier like().hashCode(), then several messages will display a few lines.          (1, ());

          break;
        default:
          break;
      }
    }
  };

}

kind:

import ;
import .;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity {

  private TextView tv_show;
  private int btn_number = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    tv_show = (TextView) ( .tv_show);
    tv_show.setOnClickListener(new () {
      @Override
      public void onClick(View view) {
        showNotify();
        btn_number++;
        tv_show.setText("Sent" + btn_number + "Speed ​​broadcast");
      }
    });
  }

  private void showNotify() {
    Intent intent = new Intent();
    ("MASSAGE_NOTIFICATION");
// ("info", "Broadcast sent");    sendBroadcast(intent);
  }

  @Override
  protected void onStop() {
    btn_number = 0;
    tv_show.setText("Sent" + btn_number + "Speed ​​broadcast");
    ();
  }

  @Override
  protected void onPause() {
    btn_number = 0;
    tv_show.setText("Sent" + btn_number + "Speed ​​broadcast");
    ();
  }

  @Override
  protected void onDestroy() {
    btn_number = 0;
    ();
  }

}

kind:

import ;
import ;
import ;
import ;
import .;

/**
 * Created by admin on 2017/9/4.
 */

public class ActDetail extends AppCompatActivity{

  NotificationManager notificationManager;
  SingletonF singletonF;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_detail);

    singletonF = ();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    (1);
    singletonF.setInfo_number(0);
  }
}

The original author was not found, so I don’t know where it came from. I can only wait until I know the original author later before adding the source URL! ! !

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.