Roish Roish - 4 years ago 248
Android Question

Create custom big notification inflate

I am trying to create a simple big notification, but I can't find what I am doing wrong.
the notification even doesn't appear.

Edit: Ok, I found some problem but it is not fully working.
It was a problem with the View that I used to separate the lyaout(the simple gray line). I removed it and change the TextView to regular TextView and it is showing my layout now, but it's been cut, i cant see all the layout.
I use

.setCustomContentView(remoteViews)
to set the rmoteView. if I use
.setCustomBigContentView(remoteViews)
it show an empty notification, not with the layout.

So why
.setCustomBigContentView(remoteViews)
doesn't working?
And why I cant use my custom
TextView
?

this is my simple code:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_add_driver_layout);
remoteViews.setImageViewResource(R.id.notifAddDriverIcon, R.drawable.my_trips_new);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setCustomBigContentView(remoteViews)
.setAutoCancel(true);

notificationManager.notify(3, builder.build());


And this is my "notification_add_driver_layout" layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="140dp">

<ImageView
android:id="@+id/notifAddDriverIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
app:srcCompat="@drawable/my_trips_new" />

<TextView
android:id="@+id/notifAddDriverTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/notifAddDriverIcon"
android:layout_marginStart="21dp"
android:layout_toEndOf="@+id/notifAddDriverIcon"
android:text="Need to update driver details"
android:textSize="18sp" />

<TextView
android:id="@+id/notifAddDriverSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/notifAddDriverTitle"
android:layout_below="@+id/notifAddDriverTitle"
android:layout_marginTop="6dp"
android:text="From one place to another\n in sunday 05.02.2017 at 09:00" />

<!--<View-->
<!--android:id="@+id/separator"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="2dp"-->
<!--android:layout_alignStart="@+id/notifAddDriverSubtitle"-->
<!--android:layout_below="@+id/notifAddDriverSubtitle"-->
<!--android:layout_marginStart="12dp"-->
<!--android:layout_marginTop="19dp"-->
<!--android:background="@color/chat_view_call_stroke" />-->

<RelativeLayout
android:id="@+id/notifAddDriverButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:orientation="horizontal"
android:layout_below="@+id/notifAddDriverSubtitle"
android:layout_centerHorizontal="true">

<TextView
android:id="@+id/notifAddDriverText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="17dp"
android:layout_toEndOf="@+id/imageView4"
android:layout_weight="1"
android:text="update details"
android:textSize="18sp" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_weight="1"
app:srcCompat="@drawable/ic_reply" />
</RelativeLayout>
</RelativeLayout>


Thanks!

Answer Source

Required notification contents A Notification object must contain the following:

  1. A small icon, set by setSmallIcon()
  2. A title, set by setContentTitle()
  3. Detail text, set by setContentText()

you don't have 2 and 3.

check this

EDIT:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setCustomBigContentView(remoteViews)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
            .setAutoCancel(true);

And change your custom TextView to regularTextView`

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download