2

Problem: My app needs notifications that can be customized by the user. eg. set different sounds, titles, texts for notifications

Issue: I'm aware notification channels are set only once and cannot be modified so I thought I could just use variables however even with variables once I have picked a sound it stays as that sound and cannot be changed at all. The only solution I can think is a new channel each time something is changed which just seems silly. Surely there is another way??

Also to add to all of this, on Android Pie the notification sound does not work at all I can't work out why.

Uri soundUri = Uri.parse(notificationSound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(customTextTitle)
        .setContentText(customTextBody)
        .setAutoCancel(true)
        .setSound(soundUri)
        .setContentIntent(pendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

    if(soundUri != null){
        // Changing Default mode of notification
        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        // Creating an Audio Attribute
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();

        // Creating Channel
        NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(soundUri,audioAttributes);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
}
mNotificationManager.notify(0, notificationBuilder.build());
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nate W
  • 39
  • 5
  • multiple sounds should be supported by multiple channels. Create different channel based on different sounds. – Rohit5k2 Feb 21 '19 at 10:23
  • Thanks, because the user might have so many different sounds I feel like a new channel each time could get really excessive. (To users who might use them and might look buggy) – Nate W Feb 21 '19 at 20:19
  • The idea of a channel is to let user control the notifications. You can have only a limited amount of notification types. – Rohit5k2 Feb 22 '19 at 06:07

2 Answers2

1

I have solved the problem albeit in a hacky way :( I have ended up setting the sound to null if >=Oreo and using a media player to play the notification sound also setting the audioStream to STREAM_NOTIFICATION. Obviously not ideal but as it's a notification and not an alarm the audio shouldn't go over the time set to perform task during onReceive.

The only problem I can see with this solution is if the user decides to mute the notification/make adjustments to the channel on their phone. Muting the sound there obviously will have no effect to the sound played by media player and will most likely show as no sound anyway which is unfortunate.

 try {
                    mp.setDataSource(context.getApplicationContext(), soundUri);
                    mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    //exception caught in the end zone
                }

For anyone else having this issue I found a really great post here which is the same solution as mine but more robust and in more depth. Android O - Notification Channels - Change Vibration Pattern or Sound Type

Nate W
  • 39
  • 5
0

you have wrong implemented, set sound like below

notificationBuilder.setSound(soundUri,audioAttributes);
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
  • I tried this but the builder.setSound doesn't allow me to add audio attributes it asks for an int. The sound works the other way but it just gets stuck as that particular sound with no customization from the user.. – Nate W Feb 21 '19 at 20:21