Android notification sound override - android

I have made an android taxi hailing app like Uber. When a request is going to the driver, it is showing as a notification like any other notification of whatsapp and message with the default popup notification sound. I need the notification sound to ring for 15 to 20 seconds instead of the pop up sound . How do I do this ?

try to set the ring tone file which have a length of 15 to 20 sec
//App.appInstance --> Application class instance
Uri uri = Uri.parse("android.resource://" + App.appInstance.getPackageName() + "/" +
R.raw.notification_sound);
NotificationCompat.Builder builder = new NotificationCompat.Builder(App.appInstance, "")
.setSound(uri)
place the sound file in the raw folder

Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification mNotification = new Notification.Builder(this)
................setSound(soundUri).........
.build();

Related

How to set default device Alarm tone for scheduled notification?

I want to set default device alarm tone for my scheduled notification as normal notification sounds are hard to notice.
I try to get alarm tone by: Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
and then simply set it with: builder.setSound(alarmTone);
All I get is vibration with out any alarm tone sound. Any ideas?
Whole code:
private Notification getNotification(String content) {
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this,
Receiver.NOTIFICATION_CHANNEL_ID
);
builder.setContentTitle("Title");
builder.setSmallIcon(R.drawable.x);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
builder.setSound(alarmTone);
builder.setContentText(content);
builder.setAutoCancel(true);
return builder.build();
}
Try Below Code:
1.builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
2.mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Put your preferred alarm in "Res\raw\{your_ringtone}.mp3"
and then do like this:
Notification mNotification = builder.build();
mNotification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.{your_ringtone});

How to change notification sound dynamically in Android O

Recently I use notification channel to support android O.
But the problem is I cannot change the sound Uri dynamically.
Our app have notification sound setting which user can change app notification sound as they want.
But as you know, Android now do not allow developer to update notification channel before user reinstall app.
There I consider several possible solutions which is not looks good.
User ringtone manager to play ringtone instead of setSound. But when user disable notification in app setting, still ringtone will not stop playing. (This will be bad user experience)
Delete notification channel and create new one when user change ringtone sound. But this also looks bad because in app setting google shows the history of deleted channel info.(Actually not necessary)
Is there any good solution?
On Android O+ devices, you should remove any notification specific settings within your app and offer a link within your settings screen to open the system's notification channel settings, where the user can adjust the sound of the notification channel directly.
#RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
Nmanager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
ArrayList arrayList = new ArrayList<String>();
Field[] fields = R.raw.class.getFields();
for (int i = 1; i < fields.length - 1; i++) {
arrayList.add(fields[i].getName());
}
DBRingtone RDB = new DBRingtone(this);
Cursor cursor = RDB.getringtone();
if (cursor.getCount() > 0) {
int ring = parseInt(cursor.getString(0));
resID = getResources().getIdentifier((String) arrayList.get(ring), "raw", getPackageName());
i=i+resID;
uri = Uri.parse("android.resource://" + getPackageName() + "/" + resID);
} else
{
i=i+10;
uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.default_sound);
}
CHANNEL_ID = CHANNEL_ID+i;
notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Your message");
notificationChannel.setLightColor(Color.RED);
notificationChannel.setSound(uri, attributes);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Nmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Nmanager.createNotificationChannel(notificationChannel);
}
Hello all, I have solved this problem using the above code and it's working. You can also assign CHANNEL_ID = CHANNEL_ID+resID directly to. For the purpose i have assigned using variable i. I have stored the user preferred Notification sounds resID is SQLite database and In the createchannels class i have retrieved that resID using cursor to create the uri's path.Hope this will help you Thank you...

How to make push notification with specific sound?

I'm working on android app with system of notifications and i need the android device to push the notification with a specific sound i stored on assets folder
this is my java code for notification :
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
Assuming that my sound stored like that : (\assets\hopo.mp3)
how to make this notification pushed with this sound without changing the push notification systems for other apps by changing the sound from the list that android device offered !!.
I hope my question is very clear to you :)
To combine the answers here and using the two answers from these questions:
How to add sound to notification?
How to get URI from an asset File?
Try this:
Uri sound = Uri.parse("file:///android_asset/hopo.mp3");
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setSound(sound);
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
You need to place your sound file to res/raw directory.
Then add this code into current code
final String packageName = context.getPackageName();
notification.sound =
Uri.parse("android.resource://" + packageName + "R.raw.hopo");
see this link for more details
http://developer.android.com/reference/android/app/Notification.Builder.html#setSound(android.net.Uri)
you can set default sound using
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmSound != null) {
mBuilder.setSound(alarmSound);
}
if you want custom sound just use different Uri. how to get Uri from assets? check this topic
To get the resource:
Uri alarmSound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.somefile);
** the raw folder must be created inside res folder and add youre sound file **
To set up:
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setContentText(message)
.setSound(alarmSound) // -> add this one
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

Unable to set User Selected Sound to Push Notification

Hi I'm working on Push Notification in my app.
In that I'm able to get Notification successfully.
I'm trying to set user selected sound to Push notification.
For that I'm storing user selected sound file path in preferences
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notif_icon));
mBuilder.setContentTitle(context.getString(R.string.app_name));
mBuilder.setTicker("Message from " +context.getString(R.string.app_name));
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setAutoCancel(true);
Uri uri = Uri.parse(CommonUtilities.sp.getString("PUSHNOTIFICATIONTONE", "Null"));
Log.d("Path: ", uri.toString());
mBuilder.setSound(uri);
I'm getting exact path in Log as I selected before
09-19 16:14:45.511: D/Path:(17832): content://com.estrongs.files/mnt/sdcard/Ringtones/bbm_tone.wav
Can Any one tell me, where I'm getting wrong??

Android push notification how to play default sound

I'm using MixPanel to send push notification and on the custom payload I add the following code:
{"sound":"default"} the problem Is that no sound gets played when I receive the notification. Does anyone have a solution for this?
Maybe this helps found here code will look like this.
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
In order to send notification + sound using mixpanel, you need to do the following:
add the following code to the onCreate:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
Send notification from mixpanel and see it received. This will send notification on create with default sound configured on the user's device.
try following code
Notification notification = new Notification(R.drawable.appicon,
"Notification", System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_SOUND;
final Notification notification =
new Notification(iconResId, tickerText, System.currentTimeMillis());
final String packageName = context.getPackageName();
notification.sound =
Uri.parse("android.resource://" + packageName + "/" + soundResId);
Assuming you have a declaration...
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(title)
.setWhen(ts)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);
... variable constructed somewhere in your code, try this:
final String ringTone = "default ringtone"; // or store in preferences, and fallback to this
mBuilder.setSound(Uri.parse(ringTone));
The default GCMReceiver in the Mixpanel library for Android that handles incoming push notifications from Mixpanel doesn't include sounds. You'll need to write your own BroadcastReceiver to process incoming messages from Mixpanel.
You can take a look at Mixpanel's documentation for using the low level API at : https://mixpanel.com/help/reference/android-push-notifications#advanced - then you an apply the advice from the other answers to do anything you'd like with your custom data payload.

Categories

Resources