Notification

Notification is a short message manage by Android Operating System for applications. It is appears outside of application interface, normally at the top bar of device known as "notification bar". It is timely reminder information to the people from application or from server. It have a short reminder information with some actions like open application with click etc.




File Name
Code
XML Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <Button
      android:id="@+id/notification"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notify"
      android:background="#236db7"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Java Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
  Button buttonNotify;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      buttonNotify=findViewById(R.id.notification);
      buttonNotify.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              NotificationCompat.Builder notification=new NotificationCompat.Builder(MainActivity.this)
                      .setContentTitle("Notification App")
                      .setContentText("A new notification")
                      .setSmallIcon(R.mipmap.ic_launcher);
              NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
              manager.notify(12,notification.build());
              manager.notify(13,notification.build());
          }
      });
  }
}

Comments