2

I'm having trouble getting a notification with a custom layout to display. If I use the following (old) code:

notification = new Notification();
notification.icon = R.drawable.icon;
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();
notification.setLatestEventInfo(context, (CharSequence)currentTitle, (CharSequence)"", pendingIntent);

Everything works as expected and the notification shows up. However if I use the following (new) code:

contentView = new RemoteViews(context.getPackageName(), R.layout.service_notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.title, (CharSequence)currentTitle);
contentView.setTextViewText(R.id.text, (CharSequence)"");
contentView.setViewVisibility(R.id.pgStatus, View.GONE);

notification = new Notification();
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();

notification.contentView = contentView;
notification.contentIntent = pendingIntent;

The notification is never shown. I'm not given an error either so I'm not really sure where to look for an issue. Here is the service_notification layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:text="This is the title."
        style="@style/NotificationTitle" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        android:text="This is the status"
        style="@style/NotificationText" />
    <ProgressBar
        android:id="@+id/pgStatus"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/image" />
</RelativeLayout>

Also, I'm running this on the Android 2.2 emulator if that makes a difference.

Anyone have any ideas of what could be wrong or how I could go about tracking down the issue? I'm kind of at a loss since I don't have an error message to work off of. Thanks!

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147

1 Answers1

11

A status bar notification requires all of the following:

  • An icon for the status bar
  • A title and message, unless you define a custom notification layout
  • A PendingIntent, to be fired when the notification is selected

from Status Bar Notifications - Creating Notifications

You don't specify an icon, which means your notification is not valid.

(But yes, it's bad on the framework side that there is no proper error message/exception for this case)


Edit: Just tested this through and I can confirm the "invisible" notification from the comment on Android 2.2. It works on 2.3.3., seems like you stumbled over a bug that has been fixed in the meantime.

The problematic line is the following for me:

contentView.setViewVisibility(R.id.pgStatus, View.GONE);

If I comment that out, everything works. It also works when I add android:visibility="gone" to the ProgressBar in the XML layout, which has basically the same effect. But as soon as I call setViewVisibility() on the bar, everything breaks apart.

So I'd recommend the workaround from the bugtracker comments, wrap the ProgressBar into a LinearLayout and change the visibility of that instead.

Community
  • 1
  • 1
  • Hm. Well that's at least part of the problem. Now the ticker notification shows but when I drag down to see the current notifications it says there are none... even though my icon is clearly displayed in the top left corner. Thoughts? – Spencer Ruport Dec 16 '11 at 11:31
  • I have an icon(and all other requirements) and... nothing. The code is executed, no error results, and no notification appears :( – SteveCav May 03 '12 at 01:20