How to set default device Alarm tone for scheduled notification? - android

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});

Related

Android notification sound override

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();

NotificationCompat setSound(sound, STREAM_ALARM) not working

Im building a ping feature for finding a lost phone through bluetooth. I need the phone to sound even though it is set to mute/silent like how the alarm usually works. I thought I could put the streamtype of my notification to AudioManager.STREAM_ALARM but its not working. It only sounds when the phones sound is on. This is how I set it:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
.setContentTitle("Ping")
.setContentText("Device is trying to find your phone.")
.setAutoCancel(false)
.setSound(sound, STREAM_ALARM)
.setVibrate(vibratePattern)
.addAction(cancelAction);
If I try :
Notification notification = builder.build();
notification.audioStreamType = AudioManager.STREAM_ALARM;
Im getting a warning from Android Studio that audioStreamType is deprecated. Is this the case? Any other way to make the notificaiton sound even though silent mode is on? (preferable also vibrate)
I got it working by creating a dedicated mediaplayer for the purpose but I don't think this should be needed. Heres how I did it anyway:
MediaPlayer mediaPlayer = new MediaPlayer();
final String packageName = getApplicationContext().getPackageName();
Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);
try {
mediaPlayer.setDataSource(this, sound);
} catch (IOException e) {
e.printStackTrace();
}
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setLooping(false);
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
Using builder.setSound(alarmSound, AudioManager.STREAM_AUDIO) was exactly what I needed to keep my alarm going! Perhaps your issue is with the R.raw.ping_sound sound sample that you are using. After trying a bunch of terrible implementations I found online for alarm notifications (which is where I found Settings.System.DEFAULT_RINGTONE_URI) I followed the official notification documentation and then used the NotificationCompat.Builder documentation for customization.
Here is my working alarm notification:
private void showNotification(){
// Setup Intent for when Notification clicked
Intent intent = new Intent(mContext, MedsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
// Setup Ringtone & Vibrate
Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
long[] vibratePattern = { 0, 100, 200, 300 };
// Setup Notification
String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
.setContentText(notificationMessage)
.setContentTitle(notificationTitle)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setSound(alarmSound, AudioManager.STREAM_ALARM)
.setOnlyAlertOnce(true)
.setVibrate(vibratePattern)
.setAutoCancel(true);
// Send Notification
NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, mBuilder.build());
}

Why won't my notification play a ringtone when it will play an mp3 from res/raw?

In the following code I create a notification and display it with vibration and sound. As it is now it works perfectly fine.
However, if I change to using sounds from the RingtoneManager, as I have seen multiple other examples do, no sound gets played (no error messages, just no sound). Does anyone know how I can get those sounds to play?
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setStyle(new NotificationCompat.InboxStyle());
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
Uri alarmsound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification); // Works fine
// Uri alarmsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Doesn't work
builder.setSound(alarmsound);
long[] pattern = {1000, 500, 500, 1000};
builder.setVibrate(pattern);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(1, notification);

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 });

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