28

Using NotificationManagerCompat to cancel all notification.

NotificationManagerCompat manager =  
    NotificationManagerCompat.from(ctx.getApplicationContext());
manager.cancelAll();

It got exception some time (most time works).

on Andoid 6:

java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=22994, uid=10184 requires android.permission.INTERACT_ACROSS_USERS

Fatal Exception: java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=22994, uid=10184 requires android.permission.INTERACT_ACROSS_USERS
   at android.os.Parcel.readException(Parcel.java:1602)
   at android.os.Parcel.readException(Parcel.java:1555)
   at android.app.INotificationManager$Stub$Proxy.cancelAllNotifications(INotificationManager.java:649)
   at android.app.NotificationManager.cancelAll(NotificationManager.java:323)
   at android.support.v4.app.NotificationManagerCompat.cancelAll(NotificationManagerCompat.java:197)

on Android 5.0, 4.4.2:

ava.lang.SecurityException: Permission Denial: getIntentSender() from pid=5460, uid=10135, (need uid=1000) is not allowed to send as package android at android.os.Parcel.readException(Parcel.java:1465)

Fatal Exception: java.lang.SecurityException: Permission Denial: getIntentSender() from pid=3109, uid=10153, (need uid=1000) is not allowed to send as package android
   at android.os.Parcel.readException(Parcel.java:1472)
   at android.os.Parcel.readException(Parcel.java:1426)
   at android.app.INotificationManager$Stub$Proxy.cancelAllNotifications(INotificationManager.java:271)
   at android.app.NotificationManager.cancelAll(NotificationManager.java:220)
   at android.support.v4.app.NotificationManagerCompat.cancelAll(NotificationManagerCompat.java:197)

Questions:

  1. What could be the cause?
  2. What are those id here? Is it ctx.getApplicationContext().getApplicationInfo().uid or android.os.Process.myUid()?
Onik
  • 19,396
  • 14
  • 68
  • 91
lannyf
  • 9,865
  • 12
  • 70
  • 152
  • 1
    "is it ctx.getApplicationContext().getApplicationInfo().uid or android.os.Process.myUid()?" -- for most Android apps, those should be the same. Both of your errors are rather strange, though. – CommonsWare Apr 14 '16 at 21:08
  • 1
    Thanks CommonsWare! It is strange, it just uses the app's context to removeAll notifications (posted from inside the same app), but get security exception. What could be the possible cause? – lannyf Apr 14 '16 at 21:14
  • @lannyf Irrespective of the exceptions, are your notifications getting cancelled or not ? – Shadab Ansari Apr 14 '16 at 21:29
  • @ShadabAnsari, no. – lannyf Apr 14 '16 at 22:06
  • @Iannyg Did you find out solution? – Giru Bhai Jun 20 '16 at 09:55
  • Hey @lannyf did you found a solution? I got the same problem with my app occasionally throws this exception when notifications are calcelled. – Rybzor Oct 28 '16 at 07:38
  • @ Rybzor, no. seems this question is not popular enough. – lannyf Oct 28 '16 at 13:34
  • 2
    So I guess gotta try-catch this cancer. – Rybzor Oct 28 '16 at 16:49
  • https://code.google.com/p/android/issues/detail?id=227027&thanks=227027&ts=1478202971 – AjOnFire Nov 03 '16 at 19:57
  • 1
    @66CLSjY I assume, depending on whether these `pid` and `uid` belong to your app OR not, this could be either a bug in "Binder synchronization" when, say, `getIntentSender()` was called with the `uid`'s `Binder` identity while it was to be called with the system's one (`uid`=1000), OR while handling your call other object's Binders are called with the original callers (yours) identity without clearing it first. I guess there isn't much you can do about it but catching exceptions thrown by the problem calls. – Onik Nov 07 '16 at 13:11
  • 1
    @Onik, that does shed some light on how this issue is arising. Thanks. – AjOnFire Nov 07 '16 at 20:28
  • @Onik I don't want the bounty to go to waste. Pls summarize your comments as an answer, I will give it to you. – AjOnFire Nov 08 '16 at 18:30
  • @66CLSjY I spent last weekend trying to figure out a possible cause and got nothing but the assumption which, by me, wasn't worth to post it as answer. So maybe it's fine to _"go to waste"_??? Sometimes we lose, sometimes we find :)... Yet if you insist, I'll gather all I've found and post the answer in hope it could be a good start for someone else in resolving the issue in future. – Onik Nov 08 '16 at 18:53
  • @Onik Pls do. Even though it does not contain a solution, it does offer an insight to the problem and address part one of the question asked by lannyf. – AjOnFire Nov 08 '16 at 20:20
  • Can you post the manifest file and specify in which component you're trying to cancel the notifications? – Mimmo Grottoli Nov 09 '16 at 18:28
  • Can you add your manifest? I think i met this issue before only on Asus phone – Trung Nguyen Nov 10 '16 at 10:07
  • According to our Crashlytics reports, it happens only on Android 6.0 on Huawei P8 and Blackview P2. Three users, 10 crashes. – JerabekJakub Jul 13 '17 at 06:43

2 Answers2

6

The answer does not provide a solid solution for the problem, it rather attempts to give an explanation of the cause both for the OP and @66CLSjY, who offered the bounty, with a similar issue.


Inspecting the stacktrace

According to the stacktrace SecurityException is thrown in the remote process: your app process' Binder object (e.g. INotificationManager.Stub, ActivityManagerProxy etc.) makes a Binder transaction (mRemote.transact()) * on the remote Binder object and read from the object an exception (_reply.readException()) occurred within the remote call(s). If any, the exception message is analyzed and a corresponding exception is thrown in your process.

Analyzing the exception message

Both the exception messages (one with getIntentSender() and another one with getCurrentUser()) are quite straightforward - your app didn't pass a permission check, or in other words, the code snippets of ActivityManagerService that were supposed to be called under the system_server process' identity (UID=1000) **, but, in fact, were called under your app process' identity.

Possible cause and workaround

It got exception some time (most time works).

Without making an assumption, what you get "some time" is improper Android behavior. Wrapping the problem call with try/catch seems to be a workaround until someone suggests a solid solution (if exists).


* ActivityManagerProxy.setRequestedOrientation() and IAccessibilityManager$Stub$Proxy.sendAccessibilityEvent()
** android.permission.INTERACT_ACROSS_USERS is of signature | system protection level

Community
  • 1
  • 1
Onik
  • 19,396
  • 14
  • 68
  • 91
0

To me it sounds like there are two different possibilities why this is not working:

The most likely cause is that you are using the wrong context to make the call; getApplicationContext() is not 100% reliable and sometimes produces strange errors, it is always best to avoid this call. If you are calling cancelAll() from a Service or Activity, use YourClass.this instead of getApplicationContext(), if it is from a BroadcastReceiver, use the supplied Context variable.

If this is still not working, it might be a bug in NotificationManagerCompat, try if you can reproduce the same problem with NotificationManager. A workaround for that would be to save all your Notification ids in a list and then cancel them each with manager.cancel(id). That way the system won't be trying to cancel any Notifications that do not belong to your app.

Force
  • 6,312
  • 7
  • 54
  • 85