I'm trying to make a notification with 2 buttons on it:
Has anyone got an idea on how to catch the button click event (remember that the Activity is paused)?
I am glad to post it! After working all night I found something. So, here we go!
1. Create an xml layout file for your notification.
2. Create the notification using the Notification.Builder. After adding everything you want (icons, sounds, etc) do this:
//R.layout.notification_layout is from step 1
RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout);
setListeners(contentView);//look at step 3
notification.contentView = contentView;
3. Create a method setListeners. Inside this method you have to write this:
//HelperActivity will be shown at step 4
Intent radio=new Intent(ctx, packagename.youractivity.class);
radio.putExtra("AN_ACTION", "do");//if necessary
PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
//R.id.radio is a button from the layout which is created at step 2 view.setOnClickPendingIntent(R.id.radio, pRadio);
//Follows exactly my code!
Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
volume.putExtra("DO", "volume");</p>
//HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
view.setOnClickPendingIntent(R.id.volume, pVolume);
4. For my requirements I used a HelperActivity which responds to the intents. But for you I don't think it is necessary.
If you want the full source code you can browse it or download it from my git repo. The code is for personal use, so don't expect to read a gorgeous code with a lot of comments. https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts
ALL THE ABOVE, ANSWERS THE QUESTION OF CATCHING EVENT FROM DIFFERENT BUTTONS.
About canceling the notification I redirect you here
How to clear a notification in Android
Just remember to use the id you parsed at the notify method when you called the notification for fist time
There is a full example for you here
//Add this code to onCreate or some onclick Buttton
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
long when = System.currentTimeMillis();
builder.setSmallIcon(R.drawable.ic_notification);
Intent notificationIntent = new Intent(getApplicationContext(), notificationActivity.class).putExtra("notification", "1");
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
Notification notification = builder.getNotification();
notification.when = when;
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_view);
remoteViews.setTextViewText(R.id.tvName, "New Name");
listener(remoteViews,getApplicationContext());
notification.contentView = remoteViews;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(1, notification);
and then you can define listener method :
public void listener(RemoteViews remoteViews, Context context) {
// you have to make intetns for each action (your Buttons)
Intent intent = new Intent("Accept");
Intent intent2 = new Intent("Reject");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1,intent,0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,1,intent2,0);
// add actions here !
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Accept");
intentFilter.addAction("Reject");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("Accept")){
Toast.makeText(context, "Accepted !!", Toast.LENGTH_SHORT).show();
} else if(intent.getAction().equals("Reject")) {
Toast.makeText(context, "Rejected !!", Toast.LENGTH_SHORT).show();
}
}
};
context.registerReceiver(receiver,intentFilter);
remoteViews.setOnClickPendingIntent(R.id.ivRequest,pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.ivReject,pendingIntent2);
}
and here is the notification_view layout to costumize your notification .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Request from "
/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:layout_toRightOf="@id/textView"
android:text="Amin"
/>
<ImageView
android:id="@+id/ivRequest"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/notification"
/>
<ImageView
android:id="@+id/ivReject"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_toLeftOf="@id/ivRequest"
android:layout_centerVertical="true"
android:src="@drawable/trash"
/>
</RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With