Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add button to notifications in android?

My app plays music and when users open notifications screen by swiping from the top of the screen ( or generally from the bottom right of the screen on tablets ), I want to present them a button to stop the currently playing music and start it again if they want.

I am not planning to put a widget on the user's home screen, but just into notifications. How can I do this?

like image 225
frankish Avatar asked Apr 24 '13 15:04

frankish


People also ask

How do I add buttons to my notifications?

You can create an intent for the action (in this case stop playing) and then add it as an action button to your notification. Intent snoozeIntent = new Intent(this, MyBroadcastReceiver. class); snoozeIntent. setAction(ACTION_SNOOZE); snoozeIntent.

Do push notifications have buttons?

What Are Push Notification Action Buttons? This is a case where the name is actually pretty self-explanatory: Push notification action buttons are literal buttons that can be added to the push notifications you send in order to allow recipients to take action on the messages they receive.

How do you add an action button on notification flutter?

You may want to consider using awesome_notifications plugin in the meantime since it has support for notification buttons. You can then add the icon for the Action Button by adding the icon property on NotificationActionButton. The icon should be a String resource of the image mapped in the assets.


1 Answers

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

like image 54
Ruben Miquelino Avatar answered Sep 22 '22 14:09

Ruben Miquelino