2Answers
  • 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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());
}
      • 2
    • multiple sounds should be supported by multiple channels. Create different channel based on different sounds.
      • 2
    • 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)
      • 2
    • The idea of a channel is to let user control the notifications. You can have only a limited amount of notification types.

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

  • 1
Reply Report

you have wrong implemented, set sound like below

notificationBuilder.setSound(soundUri,audioAttributes);
  • 0
Reply Report
      • 1
    • 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..