很久以前,我已经创建了一个个人(非公开!)应用程序,可以接收通知。现在,我正在重写此应用程序,并且正在使用通知通道。我已经实现了自定义振动模式以及我需要的所有东西,但是仍然缺少一件事:
应用程序应绕过DND模式。就我来看,这现在可行。但是,如果电话处于静音状态(STREAM_*
级别0),则不会播放声音-但是应该。是否有可能将此通知的音量设置为MAX?是的,如果通知是在前台接收的,这不是问题,但它也应该在后台播放声音。
到目前为止我所拥有的:
我的NotificationUtils.java
类,用于创建通知频道。这是应该绕过DND的一种:
SharedPreferences mSharedPreferences = this.getSharedPreferences(Constants.PREFERENCES_NAME, Context.MODE_PRIVATE);
Boolean prefVibration = mSharedPreferences.getBoolean(Constants.PREFERENCES_NOTIFICATION_VIBRATE, false);
Boolean prefSound = mSharedPreferences.getBoolean(Constants.PREFERENCES_NOTIFICATION_WITH_SOUND, false);
String prefSoundfile = mSharedPreferences.getString(Constants.PREFERENCES_NOTIFICATION_SOUNDFILE, "not");
String channelId = getString(R.string.default_notification_channel_id);
NotificationChannel alarmsChannel = new NotificationChannel(channelId, getString(R.string.channel_title_alarms), NotificationManager.IMPORTANCE_HIGH);
alarmsChannel.enableLights(true);
alarmsChannel.enableVibration(true);
alarmsChannel.setLightColor(Color.RED);
alarmsChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
alarmsChannel.setBypassDnd(true);
alarmsChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
alarmsChannel.shouldShowLights();
alarmsChannel.canBypassDnd();
if (prefVibration) {
alarmsChannel.setVibrationPattern(mVibratePattern);
alarmsChannel.shouldVibrate();
}
if (prefSound) {
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + getResId(prefSoundfile, R.raw.class));
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
alarmsChannel.setSound(soundUri, audioAttributes);
}
getManager().createNotificationChannel(alarmsChannel);
当用户启用Constants.PREFERENCES_NOTIFICATION_WITH_SOUND
首选项时,系统会要求他提供正确的权限:
ActivateDndAllowance mDndAllowance = new ActivateDndAllowance(this.getContext());
mDndAllowance.check();
在使用NotificationCompat.Builder
的通知接收器类中,我可以仅使用MediaPlayer
实例和AudioManager
来设置音量。但这仅适用于前台...还是我错过了什么?
即使在后台如何调整音量(仅针对此通知)?
非常感谢!