Android Notification Tutorial

Android Notification

Android notification is a message which is displayed top of the application UI with the small icon,title and content.

Here we will create notification step by step

1 First creating Notification Builder

Here first we will create NotificationCompat.Builder class for setting the notification icon,title and content.

NotificationCompat.Builder builder=new NotificationCompat.Builder(this);

2 Some properties of Android Notification:

We can set the notification properties with the help of NotificationCompat.Builder objects.

  • setSmallIcon( ):It is used to set the notification icon.
  • setContentTitle( ):It is used to set the notification title.
  • setContentText( ):It is used to set the message text of notification.
  • setAutoCancel( ):It is used to cancel the notification.

Example of Android Notification

Here we will create notification and when click on the button then notification show.

Creating the main layout where clicking the button notification will come/show.

activity_main.xml

<LinearLayout 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"/>

  <Button
   android:id="@id/click_button"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
</LinearLayout>

This is the main activity where we will put the notification code and implementation of button click.

MainActivity.java

package example.placementpreparation.com.notification;  
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.widget.Button
import android.view.View;
public class MainActivity extends AppCompatActivity {  
   private  Button button;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main_activity);  
        button = findViewById(R.id.click_button);  
        button.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
               addingNotification(); 
            }  
        }); 
    }  
 private void addingNotification() {
      NotificationCompat.Builder builder =
         new NotificationCompat.Builder(this)
         .setSmallIcon(R.drawable.notification_icon)
         .setContentTitle("Placementpreparation Notification")
         .setContentText("This notification is from placementpreparation");

      Intent notificationIntent = new Intent(this, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
         PendingIntent.FLAG_UPDATE_CURRENT);
      builder.setContentIntent(contentIntent);

      // Adding as notification
      NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      manager.notify(0, builder.build());
   }
}

  

When click on the button then notification will show:

notification icon
Placementpreparation Notification
This notification is from placementpreparation

Note:

AndroidManifest.xml

Always remember,All the activity class must be declared in the manifest.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *