首页 > 解决方案 > 除非我在应用信息设置中手动授予它的权限,否则通知声音不起作用

问题描述

首先,我尝试使用默认通知声音,但这也不起作用,所以我跳到自定义通知音调,但同样发生在那里。但是当我手动给出“允许通知声音权限应用内信息设置它工作正常”。你们能看出什么问题吗? 这是我的代码:

   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"/>

标签: javaandroidnotifications

解决方案


推荐阅读