nipun david
nipun david

Reputation: 1

How to clear notification on android when clicked

Every time I click on notification the activity pops on screen which I had used to trigger the notification in the status window.

I want to clear the notification when a user clicks on it, but it opens the activity every time.

I tried using flags not only on notification but on PendingIntent and Intents too.

My code:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification not = new Notification(R.drawable.ic_launcher,
        "this is not important", System.currentTimeMillis());

Context context = MainActivity.this;

CharSequence title = "You have been notified";
CharSequence  details = "contiue with work";

not.flags =  Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, PendingIntent. FLAG_ONE_SHOT);

not.setLatestEventInfo(context, title, details, pending);
nm.notify(0, not);

and also:

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

and:

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, Notification.FLAG_AUTO_CANCEL);

Every time I click on notification activity comes on the screen, I just want to clear notification.

Here is Logcat

09-01 00:21:10.910: E/AndroidRuntime(330): FATAL EXCEPTION: main
09-01 00:21:10.910: E/AndroidRuntime(330): java.lang.IllegalArgumentException: contentIntent required: pkg=nipun.notify id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Parcel.readException(Parcel.java:1326)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Parcel.readException(Parcel.java:1276)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.NotificationManager.notify(NotificationManager.java:111)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.NotificationManager.notify(NotificationManager.java:91)
09-01 00:21:10.910: E/AndroidRuntime(330):  at nipun.notify.MainActivity$1.onClick(MainActivity.java:49)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.view.View.performClick(View.java:2485)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.view.View$PerformClick.run(View.java:9080)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Handler.handleCallback(Handler.java:587)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-01 00:21:10.910: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 00:21:10.910: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
09-01 00:21:10.910: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 00:21:10.910: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 00:21:10.910: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1718

Answers (1)

nandeesh
nandeesh

Reputation: 24820

You should not set the pending intent

Remove below line

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, PendingIntent. FLAG_ONE_SHOT);

and send null to

not.setLatestEventInfo(context, title, details, null);

Upvotes: 0

Related Questions