android P 的 NotificationChannel 中的自定义通知声音不起作用

Custom notification sound is not working at NotificationChannel for android P

我有自定义通知声音,但是当设备收到通知时,设备不是静音的,通知声音不工作。设备正在播放默认声音。

NotificationChannel mChannel = new NotificationChannel(MISSED_CALL, "missedCall", NotificationManager.IMPORTANCE_HIGH);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mChannel.setShowBadge(true);
mChannel.enableLights(true);
mChannel.enableVibration(true);

Uri soundUri = Uri.parse("android.resource://" + Util.getPackageName(context) + "/" + R.raw.missed_notification);
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
mChannel.setSound(soundUri, audioAttributes);
mChannel.setLightColor(Color.BLUE);
notificationManager.createNotificationChannel(mChannel);

builder.setChannelId(MISSED_CALL)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setNumber(totalNotificationCount);

NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle().bigText("");
notification.setBuilder(builder);
notificationManager.notify(MISSED_CALL_NOTIFICATION_ID, 
builder.build());

R.raw. missed_notification是一个整数资源ID;你想要那个[=12=中声音资源的名称 ].所以尝试:

Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
            + "://" + getPackageName() + "/raw/missed_notification");
    notification.setSound(soundUri, audioAttributes);

我有 运行 服务和 mediaPlayer。是运行宁。

public class NotificationSoundService extends Service {

        private MediaPlayer mMediaPlayer;
        public static final String ACTION_START_PLAYBACK = "start_playback";
        public static final String ACTION_STOP_PLAYBACK = "stop_playback";
        public static final String EXTRA_SOUND_URI = "soundUri";

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        public int onStartCommand(Intent intent, int flags, int startId) {

            if (intent == null || intent.getAction() == null) {
                return START_NOT_STICKY;
            }

            String action = intent.getAction();
            switch (action) {
                case ACTION_START_PLAYBACK:
                    startSound(intent.getStringExtra(EXTRA_SOUND_URI));
                    break;
                case ACTION_STOP_PLAYBACK:
                    stopSound();
                    break;
            }

            return START_NOT_STICKY;
        }

        private void startSound(String uriString) {

            Uri soundUri;
            try {
                soundUri = Uri.parse(uriString);

                // play sound
                if (mMediaPlayer == null) {
                    mMediaPlayer = new MediaPlayer();

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

                        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                                .build();

                        mMediaPlayer.setAudioAttributes(audioAttributes);
                    } else {
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                    }

                    mMediaPlayer.setOnPreparedListener(MediaPlayer::start);
                    mMediaPlayer.setOnCompletionListener(mediaPlayer -> stopSound());
                }

                mMediaPlayer.setDataSource(this, soundUri);
                mMediaPlayer.prepareAsync();

            } catch (Exception e) {
                stopSound();
            }
        }

        private void stopSound() {
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
            cleanup();
        }

        private void cleanup() {
            stopSelf();
        }

当通知到来时,运行服务。

getNotification(){

  Intent intent = new Intent(mContext, NotificationSoundService.class);
  intent.setAction(NotificationSoundService.ACTION_START_PLAYBACK);
  intent.putExtra(EXTRA_SOUND_URI, "" + soundUri);
  mContext.startService(intent);

  builder.setChannelId(MISSED_CALL)
         .setContentIntent(pendingIntent)
         .setSound(null)
         .setCategory(NotificationCompat.CATEGORY_CALL)
         .setNumber(totalNotificationCount);
}