通知频道无法播放我在setound中发送的URI(来自RAW文件夹)

时间:2019-02-09 21:02:56

标签: android notifications notification-channel

我正在尝试在Android中创建一个通知频道,以播放Raw文件夹中的声音。

正如您在下面的代码中看到的那样,我已经放置了这一行:

RingtoneManager.getRingtone(a,uri).play();

这使我能够测试Uri是正确的,而且确实如此。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        CharSequence name = a.getString(R.string.channel_name);
        NotificationManager notificationManager = a.getSystemService(NotificationManager.class);

        notificationManager.deleteNotificationChannel(a.getString(R.string.default_notification_channel_id));

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(a.getApplicationContext());

        Uri uri;
        uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + a.getPackageName() + "/raw/" + prefs.getString("pref_ring", "a1"));

        RingtoneManager.getRingtone(a, uri).play();
        //CORRECT SOUND IS PLAYING, URI IS OK

        String description = a.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(a.getString(R.string.default_notification_channel_id), name, importance);
        channel.setDescription(description);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        channel.setSound(uri, audioAttributes);
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(a, a.getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("TEST")
                .setContentText("TEST")
                .setAutoCancel(true)
                .setSound(uri)//Just in case, but has no effect
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        notificationManager.notify(0, mBuilder.build());
    }

不幸的是,被触发的通知始终会播放默认的通知铃声

知道我在做什么错吗?

0 个答案:

没有答案