How to add a button for notifications in Android?

My application plays music, and when users open the notification screen by swiping their finger on the top of the screen (or, as a rule, on the lower right corner of the screen on tablets), I want to provide them with a button to stop the music currently playing and start it again if they want.

I do not plan to put the widget on the user's home screen, but simply in the notification. How can i do this?

+74
android button widget notifications
Apr 24 '13 at 15:17
source share
7 answers

You can create an intention for an action (in this case, stop playing) and 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 .

+6
Mar 28 '18 at 16:13
source share
— -

Add action button to 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); 

For more details, visit https://developer.android.com/training/notify-user/build-notification.html

+6
Apr 11 '18 at 8:20
source share
  // 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(); 

See here for more details.

+4
Mar 30 '18 at 5:02
source share

I will try to provide the solution I used, and most music players also use the same method to display player controls in the notification bar.

I am launching a service that is used to control Media Player and all its controls. Activity The user control interacts with the 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 get intentions and perform an action in the Service class, I use the following code in the onStartCommand service method

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

Now, to accurately answer your question, to show a notification with playback controls. You can call the following methods to display a notification using controls.

  // showNotification private 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 can achieve what you want. Happy coding :)

+3
Mar 29 '18 at 8:29
source share

I think that in addition to Ankit Gupta answer, you can use MediaSession (API> 21) to add your own mediaController view:

 notificationBuilder .setStyle(new Notification.MediaStyle() .setShowActionsInCompactView(new int[]{playPauseButtonPosition}) // show only play/pause in compact view .setMediaSession(mSessionToken)) .setColor(mNotificationColor) .setSmallIcon(R.drawable.ic_notification) .setVisibility(Notification.VISIBILITY_PUBLIC) .setUsesChronometer(true) .setContentIntent(createContentIntent(description)) // Create an intent that would open the UI when user clicks the notification .setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setLargeIcon(art); 

Source: textbook

You can also create your own view and display it in the notification area, the first answer is great here .

+1
Jul 25 '19 at 21:44
source share

verified working code with Android Pie. All of them are in the same class of service.

Show notification:

 public void setNotification() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("a", "status", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("notifications"); notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } else notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Receiver.service = this; Notification.MediaStyle style = new Notification.MediaStyle(); notification = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Notification") .addAction(R.drawable.close_icon, "quit_action", makePendingIntent("quit_action")) .setStyle(style); style.setShowActionsInCompactView(0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notification.setChannelId("a"); } // notificationManager.notify(123 , notification.build()); // pre-oreo startForeground(126, notification.getNotification()); } 

Auxiliary function:

 public PendingIntent makePendingIntent(String name) { Intent intent = new Intent(this, FloatingViewService.Receiver.class); intent.setAction(name); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); return pendingIntent; } 

To process actions:

 static public class Receiver extends BroadcastReceiver { static FloatingViewService service; @Override public void onReceive(Context context, Intent intent) { String whichAction = intent.getAction(); switch (whichAction) { case "quit_action": service.stopForeground(true); service.stopSelf(); return; } } } 

You also need to update the manifest:

 <receiver android:name=".FloatingViewService$Receiver"> <intent-filter> <action android:name="quit_action" /> </intent-filter> </receiver> 
+1
Aug 24 '19 at 18:12
source share

You can add a button as shown below, and you can perform an action with this button, I also did it for me, as shown below.

 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_logo) .setAutoCancel(true) .setContentTitle(name) .setContentText(body) .setGroupSummary(true) .addAction(android.R.drawable.ic_menu_directions, "Mark as read", morePendingIntent); 

// morePendingIntent (do your job)

  PendingIntent morePendingIntent = PendingIntent.getBroadcast( this, REQUEST_CODE_MORE, new Intent(this, NotificationReceiver.class) .putExtra(KEY_INTENT_MORE, REQUEST_CODE_MORE) .putExtra("bundle", object.toString()), PendingIntent.FLAG_UPDATE_CURRENT ); 
+1
Sep 27 '19 at 12:23
source share



All Articles