Reputation: 2049
I am trying to post a notification with a custom view in the notification area from an IntentService
, and getting the Couldn't expand RemoteView
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?
Upvotes: 48
Views: 43685
Reputation: 98
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.
Upvotes: 0
Reputation: 860
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 .
Upvotes: 0
Reputation: 1612
Don't use XML vector drawables for older android versions. It will crash the app. Use PNGs
Upvotes: 0
Reputation: 2206
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.
Upvotes: 16
Reputation: 320
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
Upvotes: 3
Reputation: 1331
@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.
Upvotes: 1
Reputation: 632
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
Upvotes: 0
Reputation: 3926
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.
Upvotes: 0
Reputation: 4825
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.
Upvotes: 4
Reputation: 8390
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.
Upvotes: 0
Reputation: 107
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..
Upvotes: 4
Reputation: 363
In my case, i was able to fix this error by reducing the icon size i was providing into .setSmallIcon();
Upvotes: 8
Reputation: 4086
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 :)
Upvotes: 13
Reputation: 841
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.
Upvotes: 11
Reputation: 24211
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.
Upvotes: 7