[ad_1]
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.
Any idea what can I do?
[ad_2]