I can't put a custom sound in my notification - android

I'm making the notifications of my app, and I can't put a custom sound, I tried in many ways, and I notice that in Android 9 doesn't work, but in Android 7 it works fine
I tried with
.setSound(Uri.parse("android.resource://" + context.packageName + "/" + R.raw.seatbelt ))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mChannel = NotificationChannel(channelId, channelName, importance)
notificationManager.createNotificationChannel(mChannel)
}
val mBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_app_icon)
.setContentTitle(body.getString("title"))
.setContentText(body.getString("alert"))
.setAutoCancel(true)
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(body.getString("alert")))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
val notification = Uri.parse("android.resource://"+context.packageName+"/raw/mysong.mp3")
val r = RingtoneManager.getRingtone(getApplicationContext(), notification)
r.play()
I have a Samsung Galaxy s8 with android 9 and the result is that always play the default sound notification, but I tried it on a Motorola with android 7 and it play my sound

you need to add sound to your channel using AudioAttributes
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
and add it to your channel like this :
mChannel.setSound(soundUri,audioAttributes);
You may need uninstall the app to alter sound settings, Check out these link for more details.

Related

Notification Ringtone not working with Android 12

As the title suggests, this code doesn't seem to work at all. I've tried almost every combination. SetSound doesn't seem to be working. It displays the notification but it does not play any ringtone/sound.
And yes, the device is not in DND mode and also the media volume, call volume, ringtone volume are set to max.
**Notification Channel and Builder code: **
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val callInviteChannel = NotificationChannel(
"call_invite",
"Channel name",
IMPORTANCE_MAX)
callInviteChannel.lockscreenVisibility = VISIBILITY_PUBLIC
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
callInviteChannel.setSound(Settings.System.DEFAULT_RINGTONE_URI, attributes)
callInviteChannel.enableVibration(true)
notificationManager.createNotificationChannel(callInviteChannel)
}
val notification = NotificationCompat.Builder(context, "call_invite")
.setSmallIcon(android.R.drawable.ic_menu_call)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentIntent(showCallPendingIntent)
.setFullScreenIntent(showCallPendingIntent, true)
.setSound(Settings.System.DEFAULT_RINGTONE_URI, AudioManager.STREAM_RING)
.setCategory(Notification.CATEGORY_CALL)
.setPriority(PRIORITY_MAX)
.setVisibility(VISIBILITY_PUBLIC)
.build()
notificationManager.notify(notificationId, notification)

Can't enable Audio in Channel Settings

I'm working with notification channels and as standard I want to enable sound for notification. I set sound in NotificationChannel options, but it's disabled. I also tried to set Audio in the NotificationCompat.Builder but this either didn't worked.
Ps: I don't know if my phone is the problem, I have an Redmi Note 7 with Android 9 and MIUI.
I've tried in the Channel options, NotificationCompat.Builder, both and each once solo, but it didn't worked. But if I turn the option in the Channel settings on it works but I don't want that the user needs to do that.
String CHANNEL_ID = "coin_master_item_generator";
CharSequence CHANNEL_NAME = "Item Generator Coin Master";
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Notifies when Items are generated");
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{2000,1000});
channel.enableLights(true);
channel.setLightColor(Color.WHITE);
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),audioAttributes);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Item Generator")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText("New Items generated");
notificationManager.notify(1524,builder.build());

NotificationChannel setSound and vibration not working

I have a problem to play sound, when notification will come, notification is showing, but there is no sound or vibration, I saw many question and answers to how to set the sound and vibration to notificaiton channels, but it doesn't works, this is how I'm trying that
val pattern = longArrayOf(0, 200, 60, 200)
val chatSound = Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://" + context.packageName + "/" + R.raw.chat_alert)
val mBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = mBuilder.build()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val mChanel = NotificationChannel(CHANNEL_ID, "test", NotificationManager.IMPORTANCE_HIGH)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
mChanel.setSound(chatSound, audioAttributes)
mChanel.enableVibration(true)
mChanel.enableLights(true)
mChanel.vibrationPattern = pattern
mNotificationManager.createNotificationChannel(mChanel)
}
mNotificationManager.notify(i, notification)
I'm testing it for Android Pie
targetSdkVersion 28
Seems when notification channel is created at first time then it doesn't automatically update if something where changed, like in my case, at first, I ran notification channel without sound and vibration.
Solution: Clear app data or just uninstall the app, after that it worked properly

Android multiple sound for notification

Am trying to produce two type of custom notification sound by adding .wav file in raw folder, when notification like job comes it produce job related notification sound in other case if notification like message comes it produce message related notification sound.
I set channel if for get the notification in Higher end device,but after creating the channel id am getting same notification sound when a notification comes based on which type of notification comes first.
onMessageReceived
String sound = data.get("sound");
Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + sound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIconId())
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && notificationManager != null) {
int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(
getString(R.string.default_notification_channel_id), Constants.NOTIFICATION_CHANNEL_NAME_MESSAGE, importance);
mChannel.setSound(soundUri,audioAttributes);
notificationManager.createNotificationChannel(mChannel);
}
if (notificationManager != null) {
notificationManager.notify(messageId, notificationBuilder.build());
}
Manifest
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id"/>`enter code here`
You need to create two notification channels, each channel for each sound and assign the ID of the channels depending on the sound you want to reproduce.
It is explained in the documentation that once a channel is created it cannot be modified.

Enable Sound button in Notification Channel

In MI Note 5 Pro which has latest MI UI 10.0 with Oreo, so when I try to send push notification by default sound is disable, so i am not able to enable sound programmatically when I am creating a channel for that.
In other Oreo devices notification sound is coming but in MI custom Oreo OS Sound is by default disable
Let me show my code for notification :
var intent = Intent(mContext, HomeActivity::class.java)
intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
.setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(uri)
.setSmallIcon(R.drawable.ic_app_icon)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
.setVibrate(longArrayOf(0, 100, 1000, 300))
.setAutoCancel(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
channel.description = "NOTIFICATION_DESCRIPTION"
channel.lightColor = Color.LTGRAY
channel.enableVibration(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
channel.setSound(uri, attributes)
var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
var notificationManager = NotificationManagerCompat.from(mContext)
notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())
I am also set channel.setSound(uri, attributes) in cannel but sound not coming
Here is the screenshot of Notification channel see there sound icon is disabled, how to enable?
Plz help
I had similar problem in MIUI Global 10.1 with oreo but it was only while using custom sound and not like yours, default sound. Any way let me explain how I solve it and hope it may solve yours.
First thing to consider is where the registration of notification channel is executed. It must be executed in Application.onCreate so that the channel is created even before the notification arrives. I was doing it in onMessageReceived.
Second as I said it was working for default notification sound and not for custom one, I inserted below code while creating notification channel and it worked.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O) {
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND); // This line did the magic for me.
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound_notification_plucky);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
CharSequence name = "MyChild";
String description = "All MyChild messages";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
notificationChannel.enableVibration(true);
notificationChannel.setSound(sound, audioAttributes);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
I'm facing same issue and still didn't get satisfying answer, but till that time we can workaround it like so:
final Uri NOTIFICATION_SOUND = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, NOTIFICATION_SOUND).play();
You need to call this after:
notificationManager.notify(notificationId, notification);
This way you will always get sound playing even if "allow sound" was turned off for your App, and the sound played will be from the system not media (expected behavior from the notifications).
And to avoid having two sounds playing at once (for the devices that don't have this issue), you can turn the sounds off like so:
1- For the Builder:
notificationBuilder.setContentTitle(title)
.set.....
.set.....
.setSound(null);
2- For the channel:
channel.setSound(null, null);
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)
This line of code is worked for me it enables all notification settings.

Categories

Resources