0

I have tried setAutocancel as well as FLAG_AUTO_CANCEL but none of these work and i have two buttons inside notification which open two diff activities.. kindly help me

public void createNotification(View view) {

    Intent yesIntent = new Intent(this, MyDialog.class);
    yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
    yesIntent.putExtra("ACTION", 1);
    PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);

    Intent noIntent = new Intent(this, MyDialog.class);
    noIntent.putExtra("ACTION", 0);
    PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
    NotificationCompat.Builder noti = new  NotificationCompat.Builder (this)
            .setContentTitle("New Project Approval")
            .setContentText("Project Description")
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.bell)
            .addAction(R.mipmap.approve_ic, "Approve", yespIntent)
            .addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    noti.build().flags  |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(NOTIFICATION_ID, noti.build());

}
zmarties
  • 4,809
  • 22
  • 39
viddzy
  • 131
  • 1
  • 8
  • I got my issue resolved by the following link http://stackoverflow.com/questions/22230568/notification-android-does-not-close-after-click – viddzy Nov 06 '15 at 08:17

2 Answers2

0

You have to cancel your notification manually in your activity. Pass the notification ID to your PendingIntent to retrieve it in your Activity and cancel the corresponding notification via the NotificationManager like :

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);

A good and full example : How to dismiss notification after action has been clicked

Community
  • 1
  • 1
Bubu
  • 1,533
  • 14
  • 14
  • Using above link i am calling the method present in my activity and cancelling the notification.. i am creating an object of resulting activity and calling the method like dialog.CancelNotification(NOTIFICATION_ID); where inside method i have pasted above code given by you but i am getting the below error java.lang.IllegalStateException: System services not available to Activities before onCreate() – viddzy Nov 03 '15 at 14:23
  • Where do you call this method ? If you call it in onCreate() method, do you call the super.onCreate() before ? – Bubu Nov 04 '15 at 21:39
0

Previously I callied the NotificationManager outside the onCreate() method, which wasn't working. But following the StackOverflow threadr suggested by @bubu I resolved it by calling NotificationManager inside onCreate();

Community
  • 1
  • 1
viddzy
  • 131
  • 1
  • 8