java - 除非我在应用信息设置中手动授予它的权限,否则通知声音不起作用
问题描述
首先,我尝试使用默认通知声音,但这也不起作用,所以我跳到自定义通知音调,但同样发生在那里。但是当我手动给出“允许通知声音权限应用内信息设置它工作正常”。你们能看出什么问题吗? 这是我的代码:
private void createNotificationChannel() {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.tune);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_FOR_DELETE_OR_NOT,
utilityClass.NOTIFICATION_CHANNEL_NAME_SECOND, NotificationManager.IMPORTANCE_HIGH);
channel2.enableVibration(true);
channel2.setVibrationPattern(new long[]{300, 300, 300});
channel2.shouldShowLights();
channel2.canBypassDnd();
channel2.setShowBadge(true);
channel2.enableLights(true);
channel2.setSound(sound, attributes);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_FOR_DELETE_OR_NOT)
.setSmallIcon(R.drawable.ic_baseline_bookmark_border_24)
.setContentTitle("title")
.setWhen(System.currentTimeMillis())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(""))
.setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.tune))
.setDefaults(DEFAULT_VIBRATE)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setPriority(NotificationCompat.PRIORITY_MAX)
.addAction(R.drawable.ic_baseline_bookmark_border_24, "Mark?", pendingIntent).build();
我的清单许可:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
解决方案
推荐阅读
- javascript - TypeError:this.props.onIncrement 不是函数
- c# - 如何使用 Azure B2C 实施安全调查问卷?
- android - 如何在 MpAndroidChart 中显示两位小数的百分比?
- reactjs - 使用 react-select 但占位符未显示
- mongodb - Mongoose - 用基于外部参数的布尔值替换引用数组
- python-3.x - 读取所有 CSV,在这些 CSV 上运行一些代码,然后编写新的 CSV
- java - @JsonPoperty 无法获取嵌套值
- python - 如何将单独列表的所有项目写入单个文件
- python - 试图理解线程
- arrays - WEBGL 1.0 着色器:如果我不能使用变量来计算索引,那么使用数组有什么意义?