I want my notification to ellipsize the text if it's too long, so I created a custom layout for it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/TextAppearance.StatusBar.EventContent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/header_text_margin"
>
<ImageView
android:id="@+id/ivNotificationIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="@dimen/header_text_margin"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ivNotificationIcon"
android:text="NotiTitle"
android:textStyle="bold"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toRightOf="@id/ivNotificationIcon"
android:ellipsize="end"
android:text="NotiText"
/>
</RelativeLayout>
However when I receive the notification I get the sound and vibration but nothing visual. Here's the receiver code, all worked well with standard notification (no xml):
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(!pm.isScreenOn()){
WakeLock w = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "Lock");
w.acquire(context.getResources().getInteger(R.integer.wake_lock_length));
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
notificationManager.notify(1, createNotification(context).build());
}else {
notificationManager.notify(1, createNotification(context).getNotification());
}
}
private RemoteViews createRemoteView(Context context){
RemoteViews notiLay = new RemoteViews(context.getPackageName(), R.layout.notification_alarm);
notiLay.setTextViewText(R.id.text, "TITLE");
notiLay.setTextViewText(R.id.title, context.getResources().getString(R.string.title));
notiLay.setImageViewResource(R.id.ivNotificationIcon, R.mipmap.ic_launcher);
return notiLay;
}
private Notification.Builder createNotification(Context context){
Notification.Builder builder = new Notification.Builder(context)
.setContent(createRemoteView(context));
Intent result = new Intent(context, MainActivity.class);
TaskStackBuilder stack = TaskStackBuilder.create(context);
stack.addParentStack(MainActivity.class);
stack.addNextIntent(result);
PendingIntent pending = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pending);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
builder.setLights(Color.GREEN, 3000, 3000);
builder.setVibrate(new long[] { 0, 1000 });
builder.setAutoCancel(true);
return builder;
}
}
You should add small icon for you notification.
builder.setSmallIcon(R.mipmap.ic_launcher);