1

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());
}
Arnaud
  • 7,259
  • 10
  • 50
  • 71
IrApp
  • 1,823
  • 5
  • 24
  • 42
  • Change this line notificationBuilder.addAction(cancelAction); to notificationBuilder.setDeleteIntent(cancelAction); and tell me what do you get – penta Sep 22 '15 at 08:34
  • If a change notificationBuilder.addAction(cancelAction); to notificationBuilder.setDeleteIntent(cancelAction); i don´t have the cancel button anymore. And I didn´t add onClick() event to the button, I thought that is not neccessary with the flag autoCancel = true. – IrApp Sep 22 '15 at 08:40
  • Sorry I have edited my questions because I didnßt include the last line.I think the problem is that the notification is cancel but in the foreground the intent is still working, how can I call stopForeground when cancel button is clicked?? – IrApp Sep 22 '15 at 08:55

2 Answers2

0

You will have to keep notification id and then call

NotificationManager nMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notification );
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
  • It didn´t work. It seems like the notification is cancel but in the background is still runnig, the service is not stopped. – IrApp Sep 22 '15 at 09:03
0

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");
    
    //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

Source : Android notification with buttons on it

melledijkstra
  • 361
  • 3
  • 17
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • It didn´t work, finally I have removed the button at the moment :(. Like I said the notification progress is stopped but the foreground is still working.. – IrApp Sep 24 '15 at 13:16
  • using cancel button you need to use pending intent and redirect to some activity where you need to cancel the notification with notification id. – KishuDroid Sep 24 '15 at 13:21