0

In Lollipop and below, you could easily send a sound only notification by omitting the icon, content title and content text when constructing, like so:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSound(Uri.parse(ringtone));
notificationManager.notify(9998, builder.build());

In Marshmallow, I'm forced to include at least an icon, or I get a 'no valid small icon' exception. I want to use the Notification system, but don't always want to display a notification in the notification bar. Is this possible with Marshmallow, or should I change to playing my notification sound with media player, even though sometimes I, or the user, may want to display a notification?

Floern
  • 33,559
  • 24
  • 104
  • 119
Johnny
  • 5
  • 2
  • "should I change to playing my notification sound with media player" -- yes, please. "even though sometimes I, or the user, may want to display a notification?" -- use `if` to branch in your code, using a `Notification` when you are willing to show an icon, or using `MediaPlayer` in cases where you want audio-only output. – CommonsWare Jun 29 '16 at 11:13

1 Answers1

0

I read somewhere that the docs said icon was required even thought it didn't throw an exception when omitted in Lollipop and below. After looking into using MediaPlayer, I finally decided to use RintoneManager to play it. I am using the notification sounds, so may as well save myself some typing and do a quick

try {
    RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringtone)).play();
} catch (Exception e) {
    e.printStackTrace();
}

to trigger the sound, I'll save the notification for when I need an actual notification. I was already planing on using if, depending on whether or not the notification should appear in the notification bar.

Johnny
  • 5
  • 2
  • can you please confirm if this approach works on Marshmallow as well ? I am running the same code which works perfectly on everything except Android M. It simply wont play anything. – Aneeb Khawar Sep 02 '16 at 11:35