0

Is it possible to make a song from the raw folder starts when i receive a notification? I tried this code to make the song "alert" starts but nothing happen.

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.beatdanger,
        "Your friend is in danger!!!", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.sound = Uri.parse("R.raw.alert");


Intent intent1 = new Intent("wael.ilahi.pfe.SMSRECEIVERESULT");
intent1.putExtra("coordination", str);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        intent1, 0);
notification.setLatestEventInfo(context, "Help your friend !!",
        str, pendingIntent);
notificationManager.notify(0, notification);
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Wael Ilahi
  • 41
  • 3

1 Answers1

1

See the "Adding a sound"-chapter in the Android Documentation for Notifications.

Your problem seems to be, that you're giving the notification-object a wrong URI: "R.raw.alert" is not valid.

Instead, if you need to get an URI to a file stored in the res/raw/-directory, use this pattern: "android.resource://" + getPackageName() + "/" + R.raw.your_raw_file as explained here: How to play videos in android from assets folder or raw folder?

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111