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.
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 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
so i've been having this issue for quite sometime now. My notification is not making any sound at all, I've tried changing it into the default sound programmatically and it's not working. I'm currently using the Android Emulator in Android Studio (Pixel 2XL Oreo). Below is the code, I've initiated the channel and set the sound in the channel, and it's not working.
// Get the uri of the sound
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/" + R.raw.kepingalertupsound);
// Get the Notification Manager using System Service for creating notification
NotificationManager notificationManager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a notification channel to initiate notification
// Use High importance to pop-up display
// Check SDK version
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Creating Audio Attribute
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(
KEPING_NOTIFICATION_CHANNEL_ID,
mContext.getString(R.string.alert_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel
mChannel.setDescription(notificationResult);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes);
if(notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
}
// Build the notification here
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, KEPING_NOTIFICATION_CHANNEL_ID)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark))
.setSmallIcon(R.drawable.ic_bitcoin_color)
.setLargeIcon(largeIcon(mContext))
.setContentTitle(mContext.getString(R.string.notification_alert_title))
.setContentText(notificationResult)
.setContentIntent(contentIntent(mContext))
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
} else {
notificationBuilder.setChannelId(KEPING_NOTIFICATION_CHANNEL_ID);
}
if(notificationManager != null) {
// Start the notification
notificationManager.notify(ALERT_NOTIFICATION_ID, notificationBuilder.build());
}
// Unregister Content Observer
mContext.getContentResolver().unregisterContentObserver(mContentObserver);
Using the following code from the Android Developer docs I am unable to get sound working in the API 27 (Android O) simulator. It works on an API 24 device. I also double checked in the notification settings that the notification channel is set to play the default sound.
Here is a project with the example code below that you can try on the simulators: Github.
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
"Test Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(newIncidentChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());
Update 5/16/18:
I'm using the solution here: https://stackoverflow.com/a/46862503/817886 to use the media play to play sounds when the notification comes in. Not ideal but using this until I can find the proper solution.
Update 5/29/18:
The latest version of Android 8.1.0 has fixed this issue.
You should be set sound in NotificationChannel not in NotificaitonBuilder
For Example
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId ,
"Test Channel",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Change your
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
Like this :
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
.setAutoCancel(true);
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());
}