Adding button action in custom notification
55,981
Start notification as:
private void startNotification(){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager =
(NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null,
System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.mynotification);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(this, switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash,
pendingSwitchIntent);
notificationManager.notify(1, notification);
}
public static class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Here", "I am here");
FlashOnOff flashLight;
flashLight = new FlashOnOff();
flashLight.flashLightOff();
flashLight.releaseCamera();
}
}
xml used:
<?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"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="@string/appImage"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="@string/flashLightOn"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/closeOnFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="@string/close" />
In manifest under Application tag:
<receiver android:name="FlashLight$switchButtonListener" />
Author by
Syed Raza Mehdi
Updated on March 24, 2020Comments
-
Syed Raza Mehdi about 4 years
I have made
custom notification
and there is a button in that, I want to perform two differentfunctionalities on notification and button click
. I look at many links but couldn't find the way to add button listener.Can anyone help. Here is my code. Thanks a lot.
private void startNotification() { Intent intent; PendingIntent pIntent; RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mynotification); Context context = getApplicationContext(); NotificationCompat.Builder builder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher).setContent( remoteViews); if (hasFlash) { intent = new Intent(context, FlashLight.class); pIntent = PendingIntent.getActivity(context, 1, intent, 0); } else { intent = new Intent(context, BlankWhiteActivity.class); pIntent = PendingIntent.getActivity(context, 1, intent, 0); } builder.setContentIntent(pIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notif = builder.setContentTitle("Flashlight") .setContentText("Lighten your world!!!").build(); mNotificationManager.notify(1, notif); remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent); }
I have passed the button id (
closeOnFlash
) insetOnClickPendingIntent
don't know why its not working.And here is my
xml
:<?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" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" />
-
Syed Raza Mehdi about 9 years@PoojaShah happy to help :)
-
Bullionist almost 9 yearsHow does the declaration in Manifest work ? Is FlashLight your class containing the swiftButtonListener ? Is it the application name ?
-
Mohammed AlBanna over 8 yearsThis is really a great idea! Thanks for sharing it!
-
Syed Raza Mehdi about 8 years@ShaikMDAshiq yes it is the class containing the receiver you may also use fully classified name of the class
-
IgorGanapolsky about 8 yearsIs this considered a global Broadcast that it has to be declared in the Manifest? If not, why can't it be used as a LocalBroadcast?
-
rams over 5 yearsHow to change notification button in receiver
-
rams over 5 years@SyedRazaMehdi How to change notification button programatically after click on that notification button?
-
Syed Raza Mehdi over 5 years@rams use notification id to get notification and replace notification by generating it again i think this could be one way to do that
-
rams over 5 yearshere my requirement is play/pause button need to change without generating new notification
-
Syed Raza Mehdi over 5 yearskeep the instance of the button globally and change it, see if it works
-
rams over 5 yearshere how to add another button click
-
rams over 5 years@SyedRazaMehdi How to add another button click in notification
-
rams over 5 yearsIs it possible or not?
-
Syed Raza Mehdi over 5 years@rams it is possible add an other button in xml and write it's on click listener
-
Syed Raza Mehdi over 5 years@rams thumbs up