how to create notifiaction with action button in android studio

Notification are most common used thing in any app today we are going to show an notification which show title and text content in notification with an action button . on click on action button perform any task here we are performing a toast



step :1 create a App class and create an notification channels

public class App extends Application {

    public static final String CHANNEL_ID = "channel1";

    @Override
    public void onCreate() {
        super.onCreate();
        creatnotificationchannel();

    }

    private void creatnotificationchannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "personal notification";
            String description = "personal informaton all are here";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

}

step :2 add app class into menifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notifiaction">

    <application
        android:name=".App" // This is app class 
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".Brodcast_reciver_notification"/> //add this broadcast reciver
       </application>

</manifest>

step 3: create an Broadcast Receiver class which perform any task on action button click . here user click on action button then this toast show . you can perform any task that you want

public class Brodcast_reciver_notification extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String nmesssge = intent.getStringExtra("message");
        Toast.makeText(context, "message is here for you", Toast.LENGTH_SHORT).show();
    }
}

step 4: get text from edit text or set your own text into notification call this method on button click

 public void N3_actionbtnnotification(View view) {

        stile = txttitle.getText().toString().trim();
        smessage = txtmessage.getText().toString().trim();

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


        Intent bordcastreciver_intent = new Intent(this, BroadcastReceiver.class);
        bordcastreciver_intent.putExtra("message", stile);
        PendingIntent ActionIntent = PendingIntent.getBroadcast(this, 0, bordcastreciver_intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setContentTitle("this is title of message ");
        builder.setSmallIcon(R.drawable.ic_notifications_active_black_24dp);
        builder.setContentText("This is content text of notification");
        builder.setContentIntent(pendingIntent);
        builder.addAction(R.mipmap.ic_launcher, "Reply", ActionIntent);  // use your own button icon and text you want 
        builder.setAutoCancel(true);
        builder.setOnlyAlertOnce(true);
        builder.setColor(Color.BLUE);
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);


        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1, builder.build());

    }

Thank You Keep Learning Keep Connected #AndroidShortCode

Comments