Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 526
hani salama Avatar asked Oct 20 '22 12:10

hani salama


1 Answers

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);
like image 192
tahhan Avatar answered Nov 15 '22 05:11

tahhan