0

Is it possible to create a notification that plays a specific Uri even if it's a song from the library? When I try passing in that type of Uri I just get the default notification sound. I've been looking for solutions to this and I find a lot of comments about how to make sure the notification doesn't interfere with music, but I can't find something that plays music. I see some solutions that discuss what to do in api >= 26 but I don't want to limit this to oreo and above so I'm guessing I need one solution for < Build.VERSION_CODES.O as well as a solution for >= O (unless there's one solution that will work for both). I found this handy function for showing a notification and adjusted it to receive a Uri

public void showNotification(String title, String content, Uri uriSound) {
    if (mNotificationManager == null) return;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "default")
            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
            .setContentTitle(title) // title for notification
            .setContentText(content)// message for notification
            .setSound(uriSound) // set alarm sound for notification
            .setAutoCancel(true); // clear notification after click
    Intent intent = new Intent(mContext, MyCalendarReceiver.class);
    PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pi);
    mNotificationManager.notify(0, builder.build());
}

When I call this I'm passing in a Uri with path /external/audio/media/2092. I've confirmed that this is a song from the library by playing it with the media player...

private void testMedia(Uri uriSound) {
    if (mMediaPlayer != null) {
        mMediaPlayer.reset();
        mMediaPlayer = null;
    }
    mMediaPlayer = MediaPlayer.create(mContext, uriSound);
    mMediaPlayer.start();
}

So that plays via the MediaPlayer, but when I pass this Uri to the notification builder, all I get is a "ding!". (the default notification sound). Is it possible to have the notification play the song or do I need to show the notification with no sound and play the song separately? TIA, Mike

Mike
  • 788
  • 10
  • 27
  • check this: https://stackoverflow.com/a/29949603/5243472 but I could not overcome it for now for Oreo. Have tried a tons of ways – Vladyslav Ulianytskyi Mar 13 '19 at 20:46
  • I'm not having any issues showing the notification. But I can't find out how to play music along with it. Your link doesn't appear to have anything to do with music. – Mike Mar 14 '19 at 00:53

0 Answers0