0

I'm developping a music player application for android and I'm stuck with a problem in the notification layout. It seems like I can't add any complicated element in it or it throws android.app.RemoteServiceException: Bad notification.

Problem is I wrote a player controller that is a Fragment. I can't find how to add the fragment by hand in code so I tried directly in XML layout but without success. I also had the problem with a custom ImageView that I wrote.

Am I really obliged to duplicate code ?

Augier
  • 352
  • 4
  • 14
  • Notifications only allow you to build an use something that can be used with a `RemoteView`, it does not support custom views either. – kandroidj May 08 '15 at 17:57

2 Answers2

0

It does not look like you can add a fragment. You could see if MediaStyle fits your needs. In your notification builder you would add .setStyle(NotificationCompat.MediaStyle). Otherwise it looks like you would have to subclass Notification.Style or NotificationCompat.Style to create a custom layout. It also looks like for some options you can intercept the notification as it's being created. Check this out for more details.

Edit:

Given your time frame, and if you're willing to flex some on your layout, then I would just add buttons to the notification. Create a pending intent for each action you want to be able to do from your notification (play, pause, skip). By way of a code sample I've included an abbreviated version of how I put a dismiss button in my notifications.

Intent resultIntent = new Intent(context, AlarmScreen.class);
    resultIntent.putExtra("Id",reminder.getId());

PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    context,
                    reminder.getId()*2,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

Notification.Builder mBuilder =
            new Notification.Builder(context)
           .setStyle(new Notification.BigTextStyle()
                            .bigText(reminder.getDescription()))
           .addAction(R.drawable.ic_stat_content_clear, "Dismiss", dismissPendingIntent)
           .build();

In your case you should be able to replace the R.drawable.ic_stat_content_clear with an appropriate icon and maybe skip the text. You can just repeat .addAction() for each button you want. Notice also where I have reminder.getId()*2 in my pending intent declaration? I found that if I had the same number there for both of my buttons I got strange results, so one of my buttons has id*2 and the other has id*2+1.

As for how you handle the intents sent by the buttons, you'll have to create a BroadcastReceiver to receive them, and figure out where to go from there based on how you're implementing the rest of your logic.

Community
  • 1
  • 1
TBridges42
  • 1,849
  • 1
  • 19
  • 29
  • `NotificationCompat.MediaStyle` is only available since API 21 meaning Android 5.0 and I'm still on Android 4 (CM11) so that won't work. I can't quite find how `NotificationCompat.Style` works. Documentation is a bit [empty](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Style.html), do you have clues ? – Augier May 08 '15 at 22:49
  • I missed that. You may be interested in this answer: http://stackoverflow.com/a/26480585/1541763 – TBridges42 May 08 '15 at 22:51
  • I'm sorry, I don't understand all of that. It's a school project and I'm running out of time. I've written a playing control which extends `Fragment` (and not `MediaController`) and I just want to display it in the notification the easiest possible way so if there's any mean I can deal with that, by overriding `NotificationCompat.Style`, maybe can you give me a piece of code or something ? Thanks a lot. – Augier May 08 '15 at 23:13
  • I've updated my answer with a (relatively) quick solution for you. Notification.BigTextStyle, which I used, is API 16 so it should be fine for you. – TBridges42 May 08 '15 at 23:31
0

That is not possible. Notification only able to use with RemoteView. RemoteView support some views only, and it do not support custom views or fragment.

Pongpat
  • 13,248
  • 9
  • 38
  • 51