Breaking News

Show Notification in Android App on Button click | Android Notification Tutorial



In this post, we show a notification when the user clicks on the button, or you can show a notification when some task is finished.

For that, we create a button in XML in our MainActivity.xml


MainActivity.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <androidx.constraintlayout.widget.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/notify"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Show Notfication"  
     app:layout_constraintBottom_toBottomOf="parent"  
     app:layout_constraintLeft_toLeftOf="parent"  
     app:layout_constraintRight_toRightOf="parent"  
     app:layout_constraintTop_toTopOf="parent" />  
 </androidx.constraintlayout.widget.ConstraintLayout>  

Then, Create button instance, find it's Id and then creates Notification method in our MainActivity.java 


MainActivity.java

 package studio.harpreet.myfirstnotification;  
 import androidx.appcompat.app.AppCompatActivity;  
 import androidx.core.app.NotificationCompat;  
 import androidx.core.app.NotificationManagerCompat;  
 import android.app.NotificationChannel;  
 import android.app.NotificationManager;  
 import android.app.PendingIntent;  
 import android.content.Intent;  
 import android.net.Uri;  
 import android.os.Build;  
 import android.os.Bundle;  
 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);  
     notificationchannel();  
     btn = findViewById(R.id.notify);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         notification("My first Notification","Notification content");  
       }  
     });  
   }  
   public void notification(String Title, String Message)  
   {  
     Intent intent = new Intent(MainActivity.this,MainActivity.class);  
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);  
     PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);  
     NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"Channel Id");  
     builder.setSmallIcon(R.drawable.ic_launcher_background)  
         .setContentTitle(Title)  
         .setContentText(Message)  
         .setPriority(NotificationCompat.PRIORITY_DEFAULT)  
         .setContentIntent(pi)  
         .setAutoCancel(true);  
     NotificationManagerCompat manager = NotificationManagerCompat.from(MainActivity.this);  
     manager.notify(1,builder.build());  
   }  
   public void notificationchannel()  
   {  
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  
     {  
       CharSequence name = "First Notification";  
       String Description = "notification description";  
       //It's for when you ling click on notification to disable that.  
       //It specifies category of which notification you want to show to your users.  
       int importance = NotificationManager.IMPORTANCE_DEFAULT;  
       NotificationChannel channel = new NotificationChannel("Channel Id",name,importance);  
       channel.setDescription(Description);  
       NotificationManager manager = getSystemService(NotificationManager.class);  
       manager.createNotificationChannel(channel);  
     }  
   }  
 }  

Here, We create the Notification method to show a notification. Also, we create a method for Notification Channel, because after the Oreo update we have to tell users that, in which category we send that notification.

Then, we call the Notification Channel method in our OnCreate.

Then, call that Notification method in our Button Click method.
Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments