Leo Starić Leo Starić - 1 year ago 121
Java Question

Android: Custom notification sound not playing

I've tried pretty much all available answers on the site, but somehow I cannot get the notification sound to work. The current code I'm testing is this (the notification is built in an alarm reciever):

public class AlarmReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 0;

public void onReceive(Context context, Intent intent) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity_.class), 0))
.setContentText("temporary text")
+ "://" + context.getPackageName() + "/raw/alert"))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)

Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);

The sound however, can be played via MediaPlayer, so the format is not the issue here.
Could it have to do with the lenght of the sound (30 seconds)?
Thank you for your help!

Answer Source

Try changing your .setSound() to this

                            + context.getPackageName() + "/"
                            + R.raw.alert))

Hope this will work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download