Handling Buttons Inside Android Notifications

Handling buttons inside android notifications

I found out that I had to register "switchButtonListener" in AndroidManifest.xml

<receiver android:name="SettingsActivity$switchButtonListener" />

Source: Android Activity with no GUI


Later I found out that I can also use code like this to achieve the same thing without modifying the manifest.

switchButtonListener = new SwitchButtonListener();
registerReceiver(switchButtonListener, new IntentFilter(SWITCH_EVENT));

.

public class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "test");
}

}

.

Intent switchIntent = new Intent(LangService.SWITCH_EVENT);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, switchIntent, 0);

notificationView.setOnClickPendingIntent(R.id.buttonswitch, pendingSwitchIntent);


Note that this way I can declare the switchButtonListener class without the static attribute (if not static, it would crash in the previous example) giving me much more flexibility.

Don't forget to call unregisterReceiver() later.

Buttons in android push notification

You can use .addAction in Notification.Builder.

Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_accept, "Accept", prevPendingIntent) // #0
.addAction(R.drawable.ic_reject, "Reject", pausePendingIntent) // #1

// Apply the media style template

EDIT 1

Refer this link.

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).

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 :)

Event OnClick for a button in a custom notification

I am writing code in my MyActivity.java class that extends android.app.Activity

It creates a custom notification, when user click on the button it sends a broadcast.
There is a broadcast receiver that receives the broadcast.

private void createDownloadNotification() {
Intent closeButton = new Intent("Download_Cancelled");
closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, closeButton, 0);

RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.widget_update_notification);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setTicker("Ticker Text").setContent(notificationView);
notificationView.setProgressBar(R.id.pb_progress, 100, 12, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);

notificationManager.notify(1, builder.build());

}

public static class DownloadCancelReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

System.out.println("Received Cancelled Event");
}
}

Register receiver in AndroidManifest.xml

<receiver android:name=".MainActivity$DownloadCancelReceiver" android:exported="false">
<intent-filter>
<action android:name="Download_Cancelled" />
</intent-filter>
</receiver>

Since it is inner class so have to use $ sign

Widget xml is here

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close Me" />

<ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>


Related Topics



Leave a reply



Submit