Android.app.RemoteServiceException Invalid notification sent

Sometimes my application gets this 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 Generation 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 saw almost all the solutions when stack overflowing, and this shows that this problem is related to custom layout. But I did not use a custom layout. I could not understand what the problem was. Can anyone help?

+7
source share
6 answers

I had the same problem when I used notification icons in .webp Make sure you use the icons in .pngfor devices with KitKat and below.

+2
source

Try to change

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

: fooobar.com/questions/94750/...

, : fooobar.com/questions/94750/...

, ,

0

- , NotificationCompat.BigTextStyle() bigText, , null NotificationCompat.BigPictureStyle() . , bigText , .

0

. .

:

, , . , , , ( - )

0

, Vector .setSmallIcon(R.drawable.ic_notification). . . 5,0 (), 6,0 ()

0

:

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 the primary parent in the notification layout. In my case, I had ConstraintLayout as a parent, and when I change it to RelativeLayout, everyone starts working. Hope this helps.

-1
source

Source: https://habr.com/ru/post/1658894/


All Articles