Latest News

Notification With Action Buttons - Java

Show Notification , Add action button , handle button clicks

In this tutorial we will learn the followings:
✓How to show notifications
✓How to add action buttons in notification
✓Handle action button clicks

Video:

Step 1: Create a new project OR Open your project

Step 2: Create two activities 1) LikeActivity 2) DislikeActivity

Step 3: Code

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"><Buttonandroid:id="@+id/showNotificationBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Show Notification"style="@style/Base.Widget.AppCompat.Button.Colored"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

MainActivity.java

package com.blogspot.adtechofficial.simplenotification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Build;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationManagerCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {//there can be multiple notifications so it can be used as notification identityprivate static final String CHANNEL_ID = "channel_id01";public static final int NOTIFICATION_ID = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button showNotificattionBtn = findViewById(R.id.showNotificationBtn);showNotificattionBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//click button to show notificationshowNotification();}});}private void showNotification() {createNotificationChannel();//start this(MainActivity) on by Tapping notificationIntent mainIntent = new Intent(this , MainActivity.class);mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent mainPIntent = PendingIntent.getActivity(this , 0 , mainIntent , PendingIntent.FLAG_ONE_SHOT);//Click Like button to start LikeActivityIntent likeIntent = new Intent(this , LikeActivity.class);likeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent likePIntent = PendingIntent.getActivity(this , 0 , likeIntent , PendingIntent.FLAG_ONE_SHOT);//Click Dislike button to start DislikeActivityIntent disIntent = new Intent(this , DislikeActivity.class);disIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent dislikePIntent = PendingIntent.getActivity(this , 0 , disIntent , PendingIntent.FLAG_ONE_SHOT);//creating notificationNotificationCompat.Builder builder = new NotificationCompat.Builder(this , CHANNEL_ID);//iconbuilder.setSmallIcon(R.drawable.ic_notification);//titlebuilder.setContentTitle("Title of Notification");//descriptionbuilder.setContentText("This is the description of the notification");//set prioritybuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);//dismiss on tapbuilder.setAutoCancel(true);//start intent on notification tap (MainActivity)builder.setContentIntent(mainPIntent);//add action buttons to notification//icons will not displayed on Android 7 and abovebuilder.addAction(R.drawable.ic_like , "Like" , likePIntent);builder.addAction(R.drawable.ic_dislike , "Dislike" , dislikePIntent);//notification managerNotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);notificationManagerCompat.notify(NOTIFICATION_ID , builder.build());}private void createNotificationChannel() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){CharSequence name = "My Notification";String description = "My notification description";//importance of your notificationint importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID , name , importance);notificationChannel.setDescription(description);NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);notificationManager.createNotificationChannel(notificationChannel);}}}

like_activity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"android:orientation="vertical"android:gravity="center"tools:context=".LikeActivity"><TextViewandroid:textSize="40sp"android:text="Like"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

LikeActivity.java

package com.blogspot.adtechofficial.simplenotification;import android.app.ActionBar;import android.app.NotificationManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class LikeActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_like);setTitle("Like");//dismiss notification on Like button clickNotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);notificationManager.cancel(MainActivity.NOTIFICATION_ID);}}

dislike_activity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"android:orientation="vertical"android:gravity="center"tools:context=".DislikeActivity"><TextViewandroid:textSize="40sp"android:text="Dislike"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

DislikeActivity.java

package com.blogspot.adtechofficial.simplenotification;import android.app.NotificationManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class DislikeActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dislike);setTitle("Dislike");//dismiss notification on Dislike button clickNotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);notificationManager.cancel(MainActivity.NOTIFICATION_ID);}}

Step 4: Run Project

Output
Notification with action buttons - Java
Notification with action buttons - Java

0 Response to "Notification With Action Buttons - Java"