android.app.RemoteServiceException Bad notification posted

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?

android
android-notifications
asked on Stack Overflow Oct 26, 2016 by vidha

6 Answers

2

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.

answered on Stack Overflow Jul 4, 2017 by jczerski
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

answered on Stack Overflow Dec 19, 2016 by Pablo Cegarra • edited May 23, 2017 by Community
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.

answered on Stack Overflow Dec 23, 2016 by Bio-Matic
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)

answered on Stack Overflow Jun 8, 2018 by Rahul Tiwari
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

answered on Stack Overflow Oct 2, 2019 by Nauman Ash
-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.

answered on Stack Overflow Apr 23, 2017 by stellyrepsolka

User contributions licensed under CC BY-SA 3.0