Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification
I am trying to post a notification with a custom view in the notification area from an IntentService
, and getting the Couldn't expand Rem开发者_运维问答oteView
error.
Here's what I am doing in onCreate()
:
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
icon = R.drawable.icon;
tickerText = "data upload in progress";
contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notiflayout);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "Hello");
contentView.setProgressBar(R.id.progressBar, 100, 10, false);
whatNext = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), starterActivity.class), 0);
notification = new Notification(icon, tickerText, System.currentTimeMillis());
notification.contentView = contentView;
notification.contentIntent = whatNext;
I am calling notify()
from onHandleIntent()
, and canceling the notifications in onDestroy()
.
I have verified that this code works in an independent app, which does not have an IntentService
. Doing this in an IntentService
is somehow giving trouble.
Could someone please explain what is it that I am doing wrong?
For me, the problem was that I was setting a specific height for the root layout, in the custom notification view xml file.
As soon as I changed:
android:layout_height="@dimen/notification_expanded"
to
android:layout_height="match_parent"
in the root layout of notification view, the problem was solved.
Also take a look at this example to see a simple example of using custom layout for notifications.
for unknown reason you are not allowed to reference dimention in the root view of the custom remote view! so you have to hard code it like android:layout_height="64dp"
but if you used android:layout_height="@dimen/notification_height_that_64"
it will give you Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification
. i hope this will help :)
In my case the exception was caused by a regular View
in my custom notification layout. Basically, it's because you're allowed to use only certain widgets like TextView, ImageView and so on.
In my case, i was able to fix this error by reducing the icon size i was providing into .setSmallIcon();
For me the problem was having a View
item in the custom layout set for the custom notification. Removing the View
item from the layout solved the problem of the Bad Notification Posted.
Here's a list of layout items which can be used, if you want to create a custom notification using RemoteView
.
Neither cleaning project nor setting the layout_height
as match_parent
worked for me.
I had the same problem. In my case:
reason -> I used for the builder.setAction(R.drawable.icon,...) function a vectordrawable and I tried also to enable them from support lib but nothing worked. In recent Android systems I do not see action icons, in the others it gives this error.
solution -> I did not find nothing, the only workaround for me is to avoid .xml files for drawables and to use the .png files in all directories hdpi mdpi ldpi..
I got the same error, but the problem for me was the constrain layout. I changed it to Relative Layout
to fix the the problem.
i see the is alot of trouble related to this topic, in my case this problem was caused by using
android:background="?attr/selectableItemBackground"
i used that on my ImageButton
and each time my app crashed but when i removed it, everything was fine, i guess the problem is you should not use any custom View
type or any theme attributes on your views, i hope this will help another stuck with this problem
@Nikolai's answer was helpful for me, indeed it was the problem. I had the same issue. There are specific controls that can be used in the notification. I had a View in my layout for notification as below.
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.04"/>
This was causing the crash. Following Layouts and controls are supported:
As per this official documentation.
I removed it and it worked fine. Hope it helps someone.
Be careful when using Vector drawables. On pre-Lollipop devices setting an icon through NotificationCompat.Builder
methods, like setSmallIcon
, will cause this crash. You'll get same crash if using Vector drawables in your custom view.
I was facing same issue in showing custom layout in notification and what i found is:
I was using ConstraintsLayout as a root layout of my custom notification, this is the mistake i was committing. As there are some limitations with constrain layout to be used in android.
Finally i changed my root layout to RelativeLayout and my notification showing perfectly. I have attached my view in below screenshot.
In my case, the problem was an inconsistency between the call
setShowActionsInCompactView(0)
And the .addAction
calls...The number of actions was different, hence the error
Don't use XML vector drawables for older android versions. It will crash the app. Use PNGs
For me the problem was on NotificationCompat.Builder.addAction
the icon i set for action not compatible with all devices . I use Andriod Studio -> File -> New -> Image Asset to generate the icon then it fine .
In my case In addAction icon(1st arg) were the problem. i changed it to png, it worked. then tried it with 0, that worked too.
精彩评论