hani salama
hani salama

Reputation: 69

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

Upvotes: 5

Views: 22252

Answers (4)

nicolas
nicolas

Reputation: 1

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

Upvotes: 0

tahhan
tahhan

Reputation: 81

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

Upvotes: 3

snachmsm
snachmsm

Reputation: 19273

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

Upvotes: 0

Codelord
Codelord

Reputation: 1120

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)

Upvotes: 0

Related Questions