androidnotificationsandroid-9.0-pienotification-channel

Custom notification sound is not working at NotificationChannel for android P


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

Solution

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