繁体   English   中英

自定义通知声音不适用于 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());

我已经运行了服务和媒体播放器。 它正在运行。

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);
}

R.raw. missed_notification R.raw. missed_notification是一个 integer资源 ID; 您想要该Uri中声音资源的名称 所以试试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM