Android Notification with Buttons on It

How to add Buttons to a push notification in Android

You want something like this, right?

Sample Image

For notifications, just use the .addAction(R.drawable.MYDRAWABLE, "BUTTON", INTENT) method.

Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.NotIcon)
//HERE ARE YOUR BUTTONS
.addAction(R.drawable.ic_prev, "BUTTON 1", myIntentToButtonOneScreen) // #0
.addAction(R.drawable.ic_pause, "BUTTON 2", myIntentToButtonTwoScreen) // #1
.addAction(R.drawable.ic_next, "BUTTON 3", myIntentToButtonThreeScreen) // #2
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Example for you")
.setContentText("Example for you")
.setLargeIcon(ButtonExampleIcon)
.build();

Take a look at this:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Just remember this, in order to customize your notifications, use the .build method. That's what we do in the code I gave you. Let me know if this helped :)

Android: programmatically adding buttons to notification

Read through these questions:

How to add button to notifications in android?

Adding button action in custom notification

Handling buttons inside android notifications

Also take look at the Notification Actions developers guide.

Also it looks like after a notification is created you cannot add actions, so you should create new notification with specified actions, and then replace previous one (assign id to your notifications).

Show android notification action buttons expanded by default

You can't expand notification. The only solution is, set Priority Max, then the top of the notification list where it would be expanded. And it depends on the device as well.

mBuilder.setPriority(Notification.PRIORITY_HIGH)

Add Button to Notification

To add a button in Your notification, You need to use addAction function.

It requires an Action object which needs:

  • an icon (oddly),
  • a title,
  • a PendingIntent

The PendingItent can encapuslate an Intent that can be a Service that will be delegated to cancel playing the alarm or a Receiver that will do it.

Let's stick with a receiver. We can create one - CancelAlarmReceiver:

public class CancelAlarmReceiver extends BroadcastReceiver {

public static String ACTION_CANCEL = "actionCancelAlarm";

public CancelAlarmReceiver() {}

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_CANCEL)) {
// Here You will cancel the alarm.
}
}
}

Remember to register it in the Manifest:

<receiver
android:name=".CancelAlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.your.app.CancelAlarmReceiver.ACTION_CANCEL" />
</intent-filter>
</receiver>

Later on, when creating a notification with a builder, You will have to provide an action leading to our Receiver:

Intent cancelIntent = new Intent(this, CancelAlarmReceiver.class);
cancelIntent.setAction(ACTION_CANCEL);
PendingIntent cancelPendingIntent =
PendingIntent.getBroadcast(this, NOTIFICATION_ID,
cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.addAction(
new Notification.Action(
R.drawable.ic_alarm_cancel,
getString(R.string.cancel_alarm),
cancelPendingIntent
)
);

This should make sure that a button will appear by Your notification and it will trigger the receiver and disable the alarm (or whatever You will make it to do).



Related Topics



Leave a reply



Submit