How to add button to notifications in android? How to add button to notifications in android? android android

How to add button to notifications in android?


You can create an intent for the action (in this case stop playing)and that add it as an action button to your notification.

Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);snoozeIntent.setAction(ACTION_SNOOZE);snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);PendingIntent snoozePendingIntent =        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)        .setSmallIcon(R.drawable.notification_icon)        .setContentTitle("My notification")        .setContentText("Hello World!")        .setPriority(NotificationCompat.PRIORITY_DEFAULT)        .setContentIntent(pendingIntent)        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),                snoozePendingIntent);

Please refer to the Android documentation.


I will try to provide a solution that I have used and most of the music player also use the same technique to show player controls in notification bar.

I am running a service which is used to manage Media Player and all its controls. Activity User control interacts with Service by sending Intents to the service for example

 Intent i = new Intent(MainActivity.this, MyRadioService.class);        i.setAction(Constants.Player.ACTION_PAUSE);        startService(i);

TO receive intents and perform action in Service class I am using following code in onStartCommand method of Service

 @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    if (intent.getAction().equals(Constants.Player.ACTION_PAUSE)) {        if(mediaPlayer.isPlaying()) {            pauseAudio();        }    }

Now to exact answer to your question to show notification with playing controls. You can call following methods to show notification with controls.

   //    showNotificationprivate void startAppInForeground() {//  Start Service in Foreground    // Using RemoteViews to bind custom layouts into Notification    RemoteViews views = new RemoteViews(getPackageName(),            R.layout.notification_status_bar);// Define play control intent    Intent playIntent = new Intent(this, MyRadioService.class);    playIntent.setAction(Constants.Player.ACTION_PLAY);// Use the above play intent to set into PendingIntent    PendingIntent pplayIntent = PendingIntent.getService(this, 0,            playIntent, 0);// binding play button from layout to pending play intent defined above views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);views.setImageViewResource(R.id.status_bar_play,            R.drawable.status_bg);Notification status = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {        status = new Notification.Builder(this).build();    }  status.flags = Notification.FLAG_ONGOING_EVENT;    status.icon = R.mipmap.ic_launcher;    status.contentIntent = pendingIntent;    startForeground(Constants.FOREGROUND_SERVICE, status);

}Hope this really helps you. And you will be able to achieve what you want. Have a Happy Coding :)


    // It shows buttons on lock screen (notification).Notification notification = new Notification.Builder(context)    .setVisibility(Notification.VISIBILITY_PUBLIC)    .setSmallIcon(R.drawable.NotIcon)    .addAction(R.drawable.ic_prev, "button1",ButtonOneScreen)     .addAction(R.drawable.ic_pause, "button2", ButtonTwoScreen)     .....    .setStyle(new Notification.MediaStyle()    .setShowActionsInCompactView(1)    .setMediaSession(mMediaSession.getSessionToken())    .setContentTitle("your choice")    .setContentText("Again your choice")    .setLargeIcon(buttonIcon)    .build();

Please refer this for more details Click here