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??
Related
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();
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});
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 });
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.
i have an android program, in which i receive a message from the database at the external server on web then i put message in SMS inbox.
now i use system notification like this :
Notification intent :
ctx = context ;
notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
syncNotification = new Notification();
notificationIntent = new Intent(Intent.ACTION_MAIN,null);
notificationIntent.setComponent(new ComponentName("com.android.mms","com.android.mms.ui.ConversationList"));
notificationIntent.addFlags(Notification.FLAG_AUTO_CANCEL);
contentIntent = PendingIntent.getActivity(ctx, 0,
notificationIntent, 0);
Notification creation :
syncNotification.icon = android.R.drawable.stat_notify_chat;
syncNotification.tickerText = ctx.getText(R.string.new_message);
syncNotification.when = System.currentTimeMillis();
syncNotification.setLatestEventInfo(ctx, ctx.getText(R.string.new_message), ctx.getText(R.string.check_your_inbox),contentIntent);
notificationManager.notify(5, syncNotification);
and then play sms ringtone:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(ctx, notification);
r.play();
after receive message and put it into inbox, notification shown but when i tap it nothing done.
are there a way that after inserting message to inbox, device show default notification and manage it ?
in android api 14 and later it is not possible because google prevent .