Android push notification how to play default sound

10,509

Solution 1

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

Solution 2

  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

Solution 3

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.

Solution 4

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

Solution 5

try following code

Notification notification = new Notification(R.drawable.appicon,
                "Notification", System.currentTimeMillis()); 
notification.defaults = Notification.DEFAULT_SOUND;
Share:
10,509
LS_
Author by

LS_

Updated on June 17, 2022

Comments

  • LS_
    LS_ about 2 years

    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?

  • LS_
    LS_ almost 10 years
    I get an error that says "mBuilder is not initialized" If I click on the fix button Eclipse auto initialize mBuilder to null and when I run the app it crashes
  • alpinescrambler
    alpinescrambler almost 10 years
    I mentioned "assuming" you have it filled in somewhere. Anyway, it would look like the edited code above. Note, you have to set the title, ts, and message variables!
  • nrod
    nrod about 9 years
    Right answer for me (using adapted Xamarin code). Totally forgot to check if Builder had a SetSound method! Thanks.