Computer >> Computer tutorials >  >> Programming >> Android

How to create a notification with NotificationCompat.Builder in Android?


Before getting into NotificationCompact.Builder, we should know what is a notification in android. Notification is just like as a message showing system on the action bar. just like missed call notification as shown below

How to create a notification with NotificationCompat.Builder in Android?

This example demonstrates how to integrate Android Notification.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android =    "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Click"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout>

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button=findViewById(R.id.button);
      button.setOnClickListener(this);
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.button:
         notificationDialog();
         break;
      }
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   private void notificationDialog() {
      NotificationManager notificationManager = (NotificationManager)       getSystemService(Context.NOTIFICATION_SERVICE);
      String NOTIFICATION_CHANNEL_ID = "tutorialspoint_01";
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
         // Configure the notification channel.
         notificationChannel.setDescription("Sample Channel description");
         notificationChannel.enableLights(true);
         notificationChannel.setLightColor(Color.RED);
         notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
         notificationChannel.enableVibration(true);
         notificationManager.createNotificationChannel(notificationChannel);
      }
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
      notificationBuilder.setAutoCancel(true)
      .setDefaults(Notification.DEFAULT_ALL)
      .setWhen(System.currentTimeMillis())
      .setSmallIcon(R.mipmap.ic_launcher)
      .setTicker("Tutorialspoint")
      //.setPriority(Notification.PRIORITY_MAX)
      .setContentTitle("sample notification")
      .setContentText("This is sample notification")
      .setContentInfo("Information");
      notificationManager.notify(1, notificationBuilder.build());
   }
}

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

How to create a notification with NotificationCompat.Builder in Android?

Now click on above button you will get an output as shown below

How to create a notification with NotificationCompat.Builder in Android?