Set custom Notification sound from internal storage - android

I am trying to play notification sound from my internal storage.
First I downloaded a file when anyone installs an application and then I want to use that notification sound that is downloaded in internal storage.
Following is the code that I am using but notification is playing system sound instead.
Notification notification = new Notification.Builder(this)
.setContentIntent(contentIntent)
.setContentText(text)
.setContentTitle(waktu)
.setSmallIcon(R.mipmap.ic_logo)
.setVibrate(new long[]{500, 1000})
.setSound(Uri.fromFile(new File(Environment.getExternalStorageDirectory()
+ "/Folder", Name + ".mp3")))
.setAutoCancel(true)
.build();
notification.flags |=
Notification.FLAG_ONGOING_EVENT |
Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 300;
notification.ledOffMS = 3000;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification.
mNotificationManager.notify(0, notification);

Related

custom push notification sound not working in android (FCM)

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.

Make android notification sound rise

I have custom notification sound in my application, with those parameters.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ex_mark_r)
// open app on click
.setContentIntent(contentIntent)
.setSound(defaultSoundUri, RingtoneManager.TYPE_ALARM)
.setContentTitle(title)
.setAutoCancel(false)
.setCategory(Notification.CATEGORY_ALARM)
.addAction(R.mipmap.ex_mark_r, getResources().getString(R.string.alert), pendingIntentAlert)
.addAction( R.mipmap.close, getResources().getString(R.string.dismiss), pendingIntentDismiss)
.setContentText(message);
// .setFullScreenIntent(contentIntent, true);
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
mNotification = mBuilder.build();
mNotification.flags |= Notification.VISIBILITY_PUBLIC;
mNotification.flags |= Notification.FLAG_INSISTENT;
mNotification.flags |= Notification.FLAG_HIGH_PRIORITY;
mNotification.flags |= Notification.PRIORITY_MAX;
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
}
How can I make my sound to rise [start low and become lowed] ?

Stop local notification sound in Android

I have implemented local notification with sound. It's working properly but when I clear local notification the custom sound what I am playing should stop.
Uri uri = null;
String[] split = content.split("-");
Intent intents = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intents, 0);
//getting audio from raw folder
uri = Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.beep);
//plays audio infinite times until user clicks or clear notification
try
{
MediaPlayer player = MediaPlayer.create(context, uri);
player.setLooping(true);
player.start();
Notification noti = new Notification.Builder(context)
.setContentTitle(split[0])
.setContentText(device_name + split[1]).setSmallIcon(R.drawable.hi_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
noti.sound = uri;
noti.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
notificationManager.notify(0, noti);
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
Set the notification sound to be INSISTENT.
1.Notification.DEFAULT_SOUND:used to play sound.
2.Notification.FLAG_INSISTENT:this flag is makes your sound to ring continuously until you have done few operations on notification ie, either you drag the bar or you click the bar.
3.Notification.FLAG_AUTO_CANCEL:this flag is used to automatically cancel the notification after you have seen it
builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title)
.setContentText(msg)
.setColor(getResources().getColor(R.color.white))
.addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent)
.setOngoing(true)
.setSound(Uri.parse(path));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WAKE_UP_ALARM_ID, notification)

Android GCM PushNotification - Add add custom sound file in app

I am getting GCM Push Notification successfully. Now i want to add custom sound file instead of default sound. I have tried with Uri from
file:///res/raw/pop.mp3
in
Notification.DEFAULT_SOUND;
but not success. Please share if you have better solution.
My GCMIntentService.java method code is below -
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
System.out.println("Called generateNotification>>>>>>>>>>>>>"+message);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);
Intent notificationIntent = new Intent(context,
SplashActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
To add custom sound add this
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
i.e. In your code change
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
to
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;

Notification sound from URI parse does not work

There are at least 7 questions on Stackoverflow related to this, I have tried every single suggestion and solution multiple times and none of them have worked. Here is my latest attempt:
private Notification createNotification() {
Notification notification = new Notification();
if(notifyImage=="food")
{
notification.icon = R.drawable.food;
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
}
else
{
notification.icon = R.drawable.bar;
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
}
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
You can see the two times I try and use a sound which never works, but the icons work perfectly. I do not know if I am missing anything in order to get this to work, but all of the code I am using is in my post.
My sound file is in res/raw/start.mp3, I can get this sound to work when pressing a button, so the sound is fine.
I think the package name is right, my application at has this at the top of each class:
package com.example.memoryGuide;
Any ideas why the sound never plays?
use
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/start");
Note one thing.
String android.content.ContextWrapper.getPackageName()
returns the package name of your android aplication. And
package com.example.memoryGuide;
shows the package name of your source package name.
If this one work for you. Please vote up..........
Just put your sound file in Res\raw\siren.mp3 folder ::
Then put this code..This will definitely work for you.
For Custom Sound ::
notification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.siren);
For Default Sound ::
notification.defaults |= Notification.DEFAULT_SOUND;
For Custom Vibrate ::
long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
For Default Vibrate ::
notification.defaults |= Notification.DEFAULT_VIBRATE;
I was having a similar problem while testing on API 23. While I do not know the cause of the problem, I did manage to find a workaround that got the job done.
In one of Google's examples, a notification is constructed thusly:
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Define the notification settings.
builder.setSmallIcon(R.drawable.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
Notice the class NotificationCompat.Builder. For some reason which is beyond me, this did not play any sound other than the default one. In other words, only this worked:
// Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Default
...
builder.setSound(soundUri); //Set the sound to play
But this didn't:
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
getPackageName() + "/" + R.raw.name_of_sound);
After simply replacing the builder class with Notification.Builder (available for API >= 11) the "this didn't" section above started working as expected.
Conclusion: use this if you're willing to sacrifice (or are not interested in) compatibility with API < 11.

Categories

Resources