Nate W
Nate W

Reputation: 39

Oreo & Pie Notifications

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

Upvotes: 2

Views: 120

Answers (2)

Nate W
Nate W

Reputation: 39

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

Upvotes: 1

Mayur Dabhi
Mayur Dabhi

Reputation: 3926

you have wrong implemented, set sound like below

notificationBuilder.setSound(soundUri,audioAttributes);

Upvotes: 0

Related Questions