7

Sometimes my application get this kind of exception:

Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package com.packagename: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.packagename user=UserHandle{0} id=136094146 tag=null score=0: Notification(pri=0 contentView=com.packagename/0x109007e vibrate=default sound=default defaults=0xffffffff flags=0x11 kind=[null]))
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:149)
   at android.app.ActivityThread.main(ActivityThread.java:5257)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
   at dalvik.system.NativeStart.main(NativeStart.java)

Code creating notification:

PendingIntent intent = PendingIntent.getActivities(this, id,
        notificationIntents, PendingIntent.FLAG_UPDATE_CURRENT);


int color = ContextCompat.getColor(this, R.color.notif_background);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setContentText(description)
        .setSmallIcon(getSmallIcon())
        .setLargeIcon(getLargeIcon())
        .setColor(color)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setAutoCancel(true)
        .setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(largeIcon))
        .setContentIntent(intent);

if (title != null) {
    notificationBuilder.setContentTitle(title)
            .setTicker(title)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .setBigContentTitle(title).bigText(description));
} else {
    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
            .bigText(description));
}

if (image != null) {
    notificationBuilder
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image).setSummaryText(description));
}
android.app.Notification notification = notificationBuilder.build();
notificationManager.notify(id, notification);

I have seen almost all the solution on stack overflow and it shows that this issue is regarding custom layout. But I didn't use custom layout. I couldn't understand what is the exact issue. Can anyone help?

vidha
  • 1,257
  • 2
  • 14
  • 22
  • Following. Vidha did you get any solution? – karan Dec 17 '16 at 10:13
  • I've got exact same problem. Did you get any solution? The fact that I'm not using custom view for this and this says can't expand remote view has left me perplexed. – Apsaliya Mar 30 '18 at 12:00

6 Answers6

3

I had the same issuee when I used notification icons in .webp Be sure that you use icons in .png for devices with KitKat and below.

jczerski
  • 265
  • 2
  • 13
0

Try to change

int color = ContextCompat.getColor(this, R.color.notif_background);

Reading this answer: https://stackoverflow.com/a/26024747/3994630

Or change the order of setting the icons, reading this: https://stackoverflow.com/a/40259328/3994630

Could be the problem, I hope it helps you

Community
  • 1
  • 1
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
0

You have collision with styles - title sometimes might be null, so NotificationCompat.BigTextStyle() will be applied with bigText set and after that if image is not null NotificationCompat.BigPictureStyle() will be applied. Problem is, that bigText will be set, but style won't be relevant.

Bio-Matic
  • 793
  • 1
  • 8
  • 20
0

This also happens between app updates. quite similar to this problem.

A possible explanation from this answer:

In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer that used to reference the correct drawable now referenced either the incorrect drawable or none at all (none at all - causing this crash)

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0

This crash happen when you use Vector in .setSmallIcon(R.drawable.ic_notification). I was using vector image with gradient items. I removed the gradient part. It started working fine on 5.0(lollipop), 6.0(marshmallow) notifications

Nauman Ash
  • 957
  • 1
  • 8
  • 19
-1

For me this work:

RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.toolbar_custom_layout);

NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setContent(remoteViews)
                        .setContentTitle("example example")
                        .setSmallIcon(R.drawable.app_icon_example)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setContentInfo("content example");

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

Also set RelativeLayout as your main parent in notification layout. In my case I had ConstraintLayout as parent and when I change it to RelativeLayout everythig start to work. I hope it'll help.