简体   繁体   中英

NotificationManager.notify() throwing securityException only on android 11 and 12

On my alarm clock app, I have 180 crashes (impacted 42 users) of java.lang.SecurityException caused by NotificationManager.notify().

Since I have around 50K active users I guess it happens only under specific circumstances.

This is how I init my notification manager:

NotificationManager mgr = (NotificationManager) 
   context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// If there is a notification shown that might block the alarm, cancel it.
if (mgr != null) {
    mgr.cancel(NOTIFY_ID);
}

And here's how I call the notify method:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...

    if (mgr.getNotificationChannel(CHANNEL_WHATEVER) == null) {

            NotificationChannel notificationChannel = new 
                NotificationChannel(CHANNEL_WHATEVER,
                context.getString(R.string.alarms_channel_name), 
                NotificationManager.IMPORTANCE_HIGH);
             notificationChannel.setSound(mAlarm.getAlarmTone(), new 
                  AudioAttributes.Builder()
                  .setUsage(AudioAttributes.USAGE_ALARM)
                  .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                  .build());

                    mgr.createNotificationChannel(notificationChannel);
    }

    AudioManager audioManager = (AudioManager) 
       context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 
       audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 
       AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    mgr.notify(NOTIFY_ID, buildNormal(context, i, pref).build())

   }

I thought that it might be because I can't access the file of the notification sound, so I used try & catch, and in the catch to use the default ringtone like that:

 try {
        mgr.notify(NOTIFY_ID, buildNormal(context, i, pref).build());
     } catch (Exception e) {
        mgr.getNotificationChannel(CHANNEL_WHATEVER)
                 .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
                            new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_ALARM)
                            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                            .build());
          mgr.notify(NOTIFY_ID, buildNormal(context, i, pref).build());
     }

But it didn't help.

It is also worth mentioning that I'm declaring the android.permission.READ_EXTERNAL_STORAGE and android.permission.WAKE_LOCK permissions, in the manifest.

Here's the full stacktrace:

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4483)
  at android.app.ActivityThread.access$1900 (ActivityThread.java:254)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2195)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:233)
  at android.os.Looper.loop (Looper.java:344)
  at android.app.ActivityThread.main (ActivityThread.java:8210)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:584)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1034)
Caused by: java.lang.SecurityException: 
  at android.os.Parcel.createExceptionOrNull (Parcel.java:2441)
  at android.os.Parcel.createException (Parcel.java:2425)
  at android.os.Parcel.readException (Parcel.java:2408)
  at android.os.Parcel.readException (Parcel.java:2350)
  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag (INotificationManager.java:3381)
  at android.app.NotificationManager.notifyAsUser (NotificationManager.java:677)
  at android.app.NotificationManager.notify (NotificationManager.java:627)
  at android.app.NotificationManager.notify (NotificationManager.java:603)
  at com.sux.alarmclocknew.AlarmManagerHelperWakeful.onReceive (AlarmManagerHelperWakeful.java:159)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4467)
Caused by: android.os.RemoteException: 
  at com.android.server.uri.UriGrantsManagerService.checkGrantUriPermissionUnlocked (UriGrantsManagerService.java:1311)
  at com.android.server.uri.UriGrantsManagerService.checkGrantUriPermissionUnlocked (UriGrantsManagerService.java:1325)
  at com.android.server.uri.UriGrantsManagerService.access$900 (UriGrantsManagerService.java:116)
  at com.android.server.uri.UriGrantsManagerService$LocalService.checkGrantUriPermission (UriGrantsManagerService.java:1477)
  at com.android.server.notification.NotificationRecord.visitGrantableUri (NotificationRecord.java:1501)

Any idea what can I do?

with whatever information is available till this point.

Stack trace shows an Exception here

at com.sux.alarmclocknew.AlarmManagerHelperWakeful.onReceive (AlarmManagerHelperWakeful.java:159)

if the following is the related code part Then ensure that Exception does not occur within the Exception handler itself. (That I cannot conclude with the given info). The statement you are catching is being used again in the Exception handler block.

 try {
            mgr.notify(NOTIFY_ID, buildNormal(context, i, pref).build());
         } catch (Exception e) {
            mgr.getNotificationChannel(CHANNEL_WHATEVER)
                     .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
                                new AudioAttributes.Builder()
                                .setUsage(AudioAttributes.USAGE_ALARM)
                                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                .build());


/*This may cause same exception again.*/
              mgr.notify(NOTIFY_ID, buildNormal(context, i, pref).build());
         }

There is bit too much code in the Exception block anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM