5

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 :)

Ziem
  • 6,579
  • 8
  • 53
  • 86
hani salama
  • 69
  • 1
  • 1
  • 8
  • http://stackoverflow.com/questions/15809399/android-notification-sound – Yogendra May 06 '15 at 09:15
  • Try to put your sound file on raw directories under res and set sound to notification like : notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.hopo); – Haresh Chhelana May 06 '15 at 09:15
  • http://stackoverflow.com/questions/10335057/play-notification-default-sound-only-android – Yogendra May 06 '15 at 09:17
  • possible duplicate of [Android GCM PushNotification - Add add custom sound file in app](http://stackoverflow.com/questions/24731591/android-gcm-pushnotification-add-add-custom-sound-file-in-app) – Haresh Chhelana May 06 '15 at 09:56

4 Answers4

3

To combine the answers here and using the two answers from these questions:

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);
Community
  • 1
  • 1
tahhan
  • 81
  • 6
0

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

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

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)

Codelord
  • 1,120
  • 2
  • 10
  • 21
0

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 });
Nic3500
  • 8,144
  • 10
  • 29
  • 40
nicolas
  • 1
  • 1