0

I have custom notification sound but when device take notification therefore device is not silent, notification sound is not working. Device is playing default sound.

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());
4

2 回答 2

2

I have run service and mediaPlayer. It is running.

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

When the notification is coming, run the service.

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);
}
于 2019-09-25T07:43:36.373 回答
1

R.raw. missed_notification is an integer resource ID; you want the name of the sound resource in that Uri. So try:

Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
            + "://" + getPackageName() + "/raw/missed_notification");
    notification.setSound(soundUri, audioAttributes);
于 2019-09-24T12:54:18.740 回答