Android Custom Notification sound is Not working? - android

I am getting the custom sound name from the notification and added conditions based on key but always sound only playing in my notification.
I want to play different sounds based on sound keys and how to handle anyone have ideas to help the same.
if (playOrderAssignmentTone) {
if(key.equalsIgnoreCase("sound")){
alarmSound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound);
} else {
alarmSound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound2);
}
} else {
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
} catch (Exception e) {
LoggerUtility.PrintTrace(e);
FirebaseCrashlytics.getInstance().log(e.getMessage());
FirebaseCrashlytics.getInstance().recordException(e);
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Notification Builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
//Optional fields
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setTicker(notificationTicker)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setWhen(System.currentTimeMillis())
//.setSound(alarmSound)
//.setShowWhen(true)
.setOngoing(isOngoing);
// Setting notification sound based on channel id
if (!CHANNEL_ONE_ID.equalsIgnoreCase("auto")) {
notificationBuilder.setSound(alarmSound);
}

So the issue with your code is you need to make separate notification channels for different sounds to work. Above is the code to attach a sound to the notification channel.
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
Uri audio = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator
+ File.separator + getApplicationContext().getPackageName() + File.separator + R.raw.notification);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
channel.setSound(audio, attributes);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
So when you want to play some specific sound with a notification use that notification CHANNEL_ID with the notification builder.

You have to call setSound() method on NotificationChannel instance.
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
setSound(soundUri, audioAttributes)
}

select notification tone from your mobile:
Uri ringtone=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);;
Intent intent=new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtone);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, ringtone);
((Activity) getContext()).startActivityForResult(intent , 1);
get ringtone path in onActivity result:
if (resultCode == MainActivity.RESULT_OK && requestCode == 1) {
uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
path = uri.toString();
}
saving this path in sqlite database and retrive from in service
when you trigger your notification play this sound just once:
Uri uri;
uri = Uri.parse(myDb.alarmGetRingtone(requestcode));
r = RingtoneManager.getRingtone(getApplicationContext(), uri);
r.play();
I Hope This Code Will Help You
Thank You

Related

MediaPlayer Doesn't finish and stops during Notification

Im using a Broadcast Receiver to send notification at custom time. I use a MediaPlayer to play a sound everytime it fires. The problem is it sometimes just suddenly stops in the middle of the sound playing. This is my code:
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
#SuppressLint("InvalidWakeLockTag")
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN");
wl.acquire(10*60*1000L /*10 minutes*/);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon);
Intent inteent = new Intent(context, MainActivity.class);
inteent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, xx, inteent, PendingIntent.FLAG_UPDATE_CURRENT);
xx = xx + 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentTitle("بانگ")
.setContentText(notTitle)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
// Put here YOUR code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("com.c4kurd.bang");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"com.c4kurd.bang",
"بانگ",
NotificationManager.IMPORTANCE_HIGH
);
channel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
assert notificationManager != null;
int id = context.getResources().getIdentifier(cc(prefs.getString("voices","")), "raw", context.getPackageName());
// Toast.makeText(context, prefs.getString("voices",""),Toast.LENGTH_SHORT).show();
MediaPlayer mp= MediaPlayer.create(context, id);
notificationManager.notify(xx, builder.setVibrate(new long[]{1000,1000}).build());
mp.start();
wl.release();
}
I don't know the issue. Is it because mp.start() is before the Wake Lock?
Thanks in advance.
DEFAULT: This line returns the default notification sound
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
CUSTOM: This line chooses a custom notification sound:
(put this in RAW (resources) folder)
Uri notificationSound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.my_custom_sound_file);
Choose one of the above and add it to your notification:
setSound(notificationSound)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentTitle("Your Title")
.setContentText("Your Content")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(notificationSound)
.setAutoCancel(true);
Remove all extra lines you used your own MediaPlayer.

Notification custom sound and vibrate not working?

I have an app that receives notification, all is working. However, the custom sound and vibration is not working. I'm testing it on Android 9 pie.
Uri sound = Uri.parse("android.resource://" +getApplicationContext().getPackageName()+ "/" +R.raw.siren); //ContentResolver.SCHEME_ANDROID_RESOURCE +
String NOTIFICATION_CHANNEL_ID = "com.example.bantay.bantay.test";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.marikinalogo);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setSound(sound);
notificationBuilder.setVibrate(new long[]{500, 1000, 500, 1000});
if(title.toLowerCase().contains("1")){
notificationBuilder.setContentIntent(pendingIntent);
}
else{
notificationBuilder.setContentIntent(pendingIntent3);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{500, 1000, 500, 1000});
notificationChannel.setSound(sound, audioAttributes);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0, notificationBuilder.build());
I don't know my errors. Does the NOTIFICATION_CHANNEL_ID affect the behavior of the notification?
I am using MediaPlayer for custom sound. And This is Working fine for me. This is working for all devices.
private MediaPlayer player;
For play custom sound:
try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);
player = MediaPlayer.create(this, uri);
player.setLooping(true); // This will play sound in repeatable mode.
player.start();
// mBuilder.setSound(uri);
} catch (Exception e) {
e.printStackTrace();
}
For stop sound:
if (player != null)
player.stop();
This is working for me. Hope this will also helps you.

Notification custom sound play in foreground but not playing while app in background

I am adding custom sound for my application. The custom ringtone is playing when the application is in the foreground but the custom sound is not playing when the application is in background.
private void sendMyNotification(String message) {
Intent intent = new Intent(this, BaseActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.ring);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.iconfinal)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.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(1, notificationBuilder.build());
}
First you need to pass your sound file name from server side.
like below example:
{
"to": "firebase_notification_token",
"notification": {
"title": "Push notification title",
"body": "Push body",
"sound": "filename", <-- point to src/res/raw/filename.mp3
}
}
You need to add below code to play custom sound while app is in background.
val channelId = getString(R.string.app_name)
val NOTIFICATION_SOUND_URI =
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.filename)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
val notification: Notification
notification = notificationBuilder.setSmallIcon(R.drawable.ic_logo_white).setTicker(getString(R.string.app_name)).setWhen(0)
.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name))
.setSound(NOTIFICATION_SOUND_URI)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(messageBody)
.build()
For more detail refer to this Document.
Thanks to Ilya Eremin for great article.

Oreo+ notifications sound issue

I'm trying to set a custom sound for notifications. The problems is that it always plays the 1st sound in res/raw folder no matter how many files are in there or how much I'm trying to change the uri. If I remove all files from raw folder no sound is played at all. On Android 6 it works well.
I would like to be able to set a sound from external/internal storage and also a system sound. Is it possible?
Here's my code:
notificationSoundUri = GeneralSettingsManager.getSoundForNotification(getApplicationContext(), site, pushType);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, Splash.class);
notificationIntent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
int smallIcon = R.drawable.big_green_v;
int backgroundColor = 0x8D1919;
String channelId = "default2";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(smallIcon)
.setColor(backgroundColor)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(Notification.DEFAULT_VIBRATE);
if(TextUtils.isEmpty(notificationSoundUri))
{
Timber.e("NOTIFICATION SOUND * MISSING");
mBuilder.setVibrate(new long[]{0L});
}
else
{
Timber.e("NOTIFICATION SOUND * " + notificationSoundUri);
mBuilder.setSound(Uri.parse(notificationSoundUri));
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(!TextUtils.isEmpty(notificationSoundUri))
{
// Create an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
//remove old channel
try {
mNotificationManager.deleteNotificationChannel(channelId);
}
catch (Exception e)
{
//do nothing
}
// Create new channel
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(Uri.parse(notificationSoundUri), audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
EDIT:
The code above is from FirebaseMessagingService. The user will select the desired sound for the notification from external storage or from a list of system sounds and this sound should be played when the notification is displayed.
Yes, You can !. You have to use RingtoneManager(For System Sound),
Uri ringSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = ringSound;
For external/internal
u need to find uri for particular Sound file.
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 2);
Handle response
#Override
protected void onActivityResult(final int requestCode, final int resultCode,
final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 2)
{
Uri uri = Intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
this.chosenRingtone = uri.toString();
}
else
{
this.chosenRingtone = null;
}
}
}

Android notification setSound is not working

In my hybrid Cordova Android app targeting API 23+ I want to use a custom sound for notifications. To that end I have done the following
In plugin.xml file for the single custom plugin I use in the app I declare <resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'.
Opening the APK as a zip archive I see that the mp3 file has in fact ended up in `res/raw/mysound.mp3'.
- When building the notification I do the following
Notification notification = new Notification.Builder(context)
.setDefaults(0) //turns off ALL defaults
.setVibrate(vibrate) /sets to vibrate
....
.setSound(uri).build();
where
Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
This appears to be the recipe indicated in a number of articles I find on a spot of googling and even in other threads on SO. And yet, when I issue a notification I do not hear the expected sound. What might I be doing wrong?
The answer below does not help since in the context of my hybrid Cordova app with a custom plugin attempting to build the APK throws up an error along the lines of class R not known/found...
below code will help you:
String CHANNEL_ID="1234";
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.mysound);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
//For API 26+ you need to put some additional code like below:
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID, Utils.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, audioAttributes);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel( mChannel );
}
}
//General code:
NotificationCompat.Builder status = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
status.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logo)
//.setOnlyAlertOnce(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setVibrate(new long[]{0, 500, 1000})
.setDefaults(Notification.DEFAULT_LIGHTS )
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +mContext.getPackageName()+"/"+R.raw.apple_ring))
.setContentIntent(pendingIntent)
.setContent(views);
mNotificationManager.notify(major_id, status.build());
where mysound is my ringtone which is put under res/raw folder.
Note: you have to only put name of ringtone without extension like raw/mysound
Note: In Android Oreo you must change your channel ID to changes take effect. And before Oreo version, using ".setDefaults()" seens prevent custom sound to play.
Try clearing data (or fresh install)
Trying this again
The settings are set the first time you create the channel and then not modified unless you do it manually by fresh install or clearing data.
For more info on this read the top answer here:
Android Oreo notification keep making Sound even if I do not set sound. On Older version, works perfectly
For API 26+ you need to set the sound on the notification channel:
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.siren);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(Utils.CHANNEL_SIREN_ID, Utils.CHANNEL_SIREN_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, audioAttributes);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel( mChannel );
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_SIREN_ID)
.setSmallIcon(R.drawable.ic_stat_maps_local_library)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
.setTicker(title)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setAutoCancel(true)
.setLights(0xff0000ff, 300, 1000) // blue color
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
mBuilder.setSound(soundUri);
}
int NOTIFICATION_ID = 1; // Causes to update the same notification over and over again.
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
you can call this method while handling notification
public void playNotificationSound() {
try {
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
Ringtone r = RingtoneManager.getRingtone(mContext, alarmSound);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(mTitle);
builder.setContentText(mContentText);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
builder.setDefaults(Notification.DEFAULT_ALL);
Use this to work with sound, I hope it will solve your problem, Cheers!
Use this for setting sound
Uri defaultSoundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mContext.getPackageName() + "/raw/mysound");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext)
.setContentIntent(mainPIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentTitle("" + title)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentText("" + body);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(title, NOTIFICATION_ID, mBuilder.build());
You are accessing the sound in a subfolder in the resources
change the source of your uri to
Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);
For the default sound, use:
notification.defaults |= Notification.DEFAULT_SOUND;
I am not sure but i think issue is that you are doing the wrong way "/raw/mysound.mp3 :
Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
First add the permission in manifest : uses-permission android:name="android.permission.VIBRATE" />
then you can set the default sound like :-
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
and for vibration:
mBuilder.setVibrate(new long[] { 1000, 1000});
for custom sound, put mp3 file on this path:Res\raw\sound.mp3
and then
Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.sound);

Categories

Resources