I have the following code but everytime I just hear the default android sound.
// create channel
NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID,
ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
// Sets whether notifications posted to this channel should display notification lights
channel.enableLights(true);
// Sets whether notification posted to this channel should vibrate.
channel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
channel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
//channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/" + R.raw.aperturaabductores);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(uri,att);
This is my sound pablomonteserin.es/aperturaabductores.wav
I tried to see the difference between your sound file and mine. I used Audacity software.
Your sound file has sampling rate 22050Hz while the sound files i use are sampled at 44100Hz. So i converted your sound file sampling rate to 44100Hz and used that as notification sound. Now it works.
The problem is with the sound file. May be it's new change in Android O because it's working fine on older Android version.
This is how to resample-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification.Builder notificationBuilder =
new Notification.Builder(MyApplication.getInstance().getApplicationContext(), NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(pTitle)
.setContentText(messageBody)
.setAutoCancel(true)
//.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
notificationChannel.setSound(defaultSoundUri,att);
notificationChannel.setDescription(messageBody);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
if (imageThumbnail != null) {
notificationBuilder.setStyle(new Notification.BigPictureStyle()
.bigPicture(imageThumbnail).setSummaryText(messageBody));
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MyApplication.getInstance().getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(pTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (imageThumbnail != null) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(imageThumbnail).setSummaryText(messageBody));
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Related
Be informed we are trying to send push notification to a device with custom sound. The custom mp3 file (120 seconds) is stored in res/raw file. The notification is sent successfully, but the sound never plays out even after many tries. The code is as given below:
Update:Based on suggestion given in the comments, created a notificationchannel and set the sound in it, but still its not working. Worse, unable to build apk at all.
Uri soundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.adhan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
Notification noti = notificationBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("345",
"new",
NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
// Configure the notification channel.
mChannel.setDescription(message);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(soundUri, attributes); // This is IMPORTANT
if (NotificationManager != null)
NotificationManager.createNotificationChannel(mChannel);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, notificationBuilder.build());
We are at loss as where we are going wrong. Please guide us as how we solve this issue.
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());
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.
I recently started coding my first android project including notifications (SDK 21 - Android 5)
Currently, I have a tiny little button, that creates a notification on click and sends it to the app itself. Sound foolish but the purpose is to test if a custom sound and vibrate pattern is used.
This is the notification that gets constructed on click:
Notification note = new Notification.Builder(this.requireContext(), "channel_id")
.setSmallIcon(R.mipmap.icon)
.setContentTitle("Title")
.setContentText("Text")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVibrate(new long[] {500, 500, 500, 500, 500})
.setSound(SettingsHandler.getRingtoneUri(this.requireContext())
.setContentIntent(anyIntent)
.setAutoCancel(true)
.build();
SettingsHandler is a helper class I created to handle settings. Like switching vibration on and off or picking a ringtone. getRingtoneUri() does the following:
public synchronized static Uri getRingtoneUri(Context context) {
SharedPreferences prefs = context.getSharedPreferences("table_name", Context.MODE_PRIVATE);
return Uri.parse(prefs.getString("ringtone_uri_key", RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString()));
}
When debugging this the result of getRingtoneUri is something like "content://media/internal/audio/media/31". This looks valid to me. However, in a later line, the sound property of the created notification is still null. Amy idea what I'm doing wrong? Thanks in forward.
try this:
Uri alarmSound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/ding");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, context.getString(R.string.checkout_channel));
mBuilder.setContentTitle(context.getString(R.string.passenger_name)).setContentText(context.getString(R.string.pit, name)).setSmallIcon(R.drawable.notification_icon).setSound(alarmSound);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelDescription = "Checkout Channel";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.checkout_channel),
channelDescription, notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.setShowBadge(true);
notificationChannel.setSound(alarmSound, audioAttributes);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(iUniqueId, notification);
You need to set the audioAttributes and create your notification channel. This works for me for all Android from 4.2 to 9
I have tried to set the custom notification sound it works fine below Oreo(8.0) devices but in Oreo device it doesn't trigger custom sound always trigger the default sound only. Can anyone help?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification.Builder notificationBuilder =
new Notification.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
//.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
notificationChannel.setSound(defaultSoundUri, att);
notificationChannel.setDescription(content);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
If you were using an Android version that is under Oreo, you only have to add your MY_SOUND_NOTIFICATION.mp3 to your Resources in a folder named raw and start coding your class but since you are using Oreo or higher, you need to check your SDK_VERSION in order to use a method called setSound of the NotificationChannel
I wont post any code since I you didnt poste code to correct. If you post some code, I will be pleased to help you