Notification

MainActivity.java
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
 public class MainActivity extends AppCompatActivity {
  Button btn;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   btn = (Button) findViewById(R.id.idBtn);
   btn.setOnClickListener(new View.OnClickListener() {
    @Override 
    20 public void onClick(View v) {
     ClickMe();
   }
  });
 }
 private void ClickMe() {
   //build notification 
   NotificationCompat.Builder mBuilder = 29(NotificationCompat.Builder) new NotificationCompat.Builder(this) 30.setSmallIcon(R.mipmap.ic_launcher) 31.setContentTitle("Simple notification") 32.setContentText("This is test of simple notification.");
   // Gets an instance of the NotificationManager service 
   NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   //to post your notification to the notification bar 
   notificationManager.notify(0, mBuilder.build());
 }
}

activity_main.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:layout_width="match_parent" 
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="anu.com.notification.MainActivity"> 
    
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:id="@+id/idBtn"
     android:background="#FFCC65" 
     android:text="Click" />
</RelativeLayout> 

Share this