Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:播放自定义通知声音,即使手机处于静音和/或DND模式_Android_Notifications_Android Notifications - Fatal编程技术网

Android 安卓:播放自定义通知声音,即使手机处于静音和/或DND模式

Android 安卓:播放自定义通知声音,即使手机处于静音和/或DND模式,android,notifications,android-notifications,Android,Notifications,Android Notifications,很久以前,我创建了一个接收通知的个人(非公共)应用程序。现在我正在重写这个应用程序,并使用通知通道。我已经实现了一个定制的振动模式和所有我需要的东西,但仍然缺少一件事: 应用程序应绕过DND模式。据我所知,这现在起作用了。但是如果手机处于静音状态(STREAM.*levels 0),则不会播放声音,但应该播放。是否有可能将此通知的音量设置为最大?是的,如果在前台收到通知,这不是问题,但它也应该在后台播放声音 到目前为止,我得到的是: 创建通知通道的MyNotificationUtils.java

很久以前,我创建了一个接收通知的个人(非公共)应用程序。现在我正在重写这个应用程序,并使用通知通道。我已经实现了一个定制的振动模式和所有我需要的东西,但仍然缺少一件事:

应用程序应绕过DND模式。据我所知,这现在起作用了。但是如果手机处于静音状态(
STREAM.*
levels 0),则不会播放声音,但应该播放。是否有可能将此通知的音量设置为最大?是的,如果在前台收到通知,这不是问题,但它也应该在后台播放声音

到目前为止,我得到的是:

创建通知通道的My
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);
当用户使用_SOUND首选项启用_Constants.PREFERENCES _NOTIFICATION _时,系统会询问用户正确的权限:

ActivateDndAllowance mDndAllowance = new ActivateDndAllowance(this.getContext());
mDndAllowance.check();
在notification receiver类中,我使用了
NotificationCompat.Builder
,我可以使用
MediaPlayer
实例和
AudioManager
来设置音量。但这只适用于前景。。。还是我错过了什么

即使在后台,如何调整音量(仅针对此通知)

非常感谢