How to implement onclick() on buttons in custom notification - android

I am trying to create a custom notification with the help of remote view with a button on it. I want to write onclick listener for this button but the problem is that the notification container is catching the touch event and button is not being clicked. My question is Is there any way to disable this notification and let the button to be clicked. My code is something like below:
Notification notification = new Notification(R.drawable.applogo,
"Apps activated", System.currentTimeMillis());
Intent notificationIntent = new Intent(getBaseContext(), AppsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),
0, notificationIntent , 0);
notification.contentIntent = pendingIntent;
//Remoteview and intent for my button
RemoteViews remoteView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.customnotification);
Intent mIntent = getPackageManager().getLaunchIntentForPackage(c.getString(1));//starting some application through package
PendingIntent pintent = PendingIntent.getActivity(this, 0,
mIntent, 0);
remoteView.setOnClickPendingIntent(R.id.button1,
pintent);
notification.contentView = remoteView;
notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
I am working on api 7.
Thanks

Unfortunately, clickable buttons within notification layout available only with Custom notifications for API >= 11.
Also, for API >= 16 you can use Notification Actions to provide buttons within notification.

Related

Android: How to build a notification with a cancel button?

I am trying to create a notification with a cancel button, but when I click on it nothing happens. This is my code:
private NotificationCompat.Builder notificationBuilder;
public void createNotification() {
notificationBuilder = new NotificationCompat.Builder(service);
notificationBuilder.setProgress(100, 0, false);
notificationBuilder.setAutoCancel(true);
Intent intent = OrderProcessingService_.intent(service).actionCancelUpload(basket.getPhotobook_id()).get();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getService(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
NotificationCompat.Action cancelAction = new NotificationCompat.Action(R.drawable.ic_action_navigation_close, service.getString(R.string.cancel_upload), pendingIntent);
notificationBuilder.setContentTitle(service.getString(R.string.notification_upload_title));
notificationBuilder.setContentText(service.getString(R.string.notification_uploading_subtext, Util.getTitle(basket.getPhotobook())));
notificationBuilder.addAction(cancelAction);
addBigPicture(notificationBuilder);
service.startForeground(1, notificationBuilder.build());
}
You will have to keep notification id and then call
NotificationManager nMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(notification );
I am glad to post it! After working all night I found something. So, here we go!
Create an xml layout file for your notification.
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;
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");
//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);
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
Source : Android notification with buttons on it

Prevent notification drawer from collapsing after button click

I have a media player application with a control notification, play, next, previous.
When I click on one of these buttons, the complete notification drawer collapses.
How do I prevent the collapsing?
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent intent = new Intent(context, NotificationPlayerControlReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setAutoCancel(false);
builder.setContentTitle(title);
builder.setContentText(interpret);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.addAction(R.drawable.av_previous, "", pIntent);
builder.addAction(R.drawable.av_play, "", pIntent);
builder.addAction(R.drawable.av_next, "", pIntent);
builder.setOngoing(true);
return builder.build();
This is the way things are supposed to work when the user clicks on a button that fires a PendingIntent created with PendingIntent.getActivity(); presumably if a button in a notification navigates to a new activity, you want the notification panel to get out of the way so you can see it.
If you want to implement transport controls in your notification, use PendingIntent.getBroadcast or .getService().

Android - Track Notification onclicked

I want to start a activity when users clicked on notification but there are no onclicked event for notification (?), so how can we know when the notification is clicked?
Thanks!
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Look into PendingIntent setup there.
The PendingIntent will get called when you click the notification.
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notificationIntent will be called when notification is clicked.

Click notification that takes you to a particular class

What I have below is some notification code. What I would like to achieve with my notification code is when the notification is clicked, it will take the user to a particular class. Here is my notification code:
final int NOTIF_ID = 1234;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, "New Offers CLOSEBY!", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
I'm assuming that when the notification is clicked, this code here is supposed to take you to the declared class:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
However, it does not, the notification does appear, but I cannot see the class I have referenced in my code (i.e when clicked it does not take me to the class I want it to). I have the notification popup currently after a button has been pressed in another class. Once the user clicks on the notification it should push that screen and replace it with the intentTHING.class. Any ideas as to what I may be missing?
Set pending Intent like this:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note .contentIntent = intent;
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
Am not sure but can try it out by doing this..
PendingIntent intent =
PendingIntent.getActivity(context, 0,
new Intent(this, intentTHING.class), PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

Android notification manager help

I m currently looking into android notification. Is there any way to show all the notification from an application as a single one with the number in android notifications window. Clicking on this will redirect the user to a view in the application with separate notifications. Also Is there any way to set the activity for each notifications so that the user will be redirected to the appropriate activity based on the notification clicked.
You could attach a custom layout to your notification, which contains a counter. And when event occurs increase the counter and update the notification. Similar like in example below:
Notification notification = new Notification(R.drawable.logo, name, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(appContext.getPackageName(), R.layout.custom_layout );
contentView.setTextViewText(R.id.notification_text, Counter);
notification.contentView = contentView;
To attach an activity to the notification:
Intent notificationIntent = new Intent(appContext, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(...);
notificationIntent.putExtras( bundle );
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent contentIntent = PendingIntent.getActivity( appContext, (int)System.currentTimeMillis(), notificationIntent, 0 );
notification.contentIntent = contentIntent;
Hope it will help.

Categories

Resources