如果文字太长,我希望我的通知会对文字进行椭圆化处理,因此我为其创建了自定义布局:
<?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>
然而,当我收到通知时,我得到声音和振动,但没有任何视觉效果。这是接收者代码,所有这些都适用于标准通知(无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;
}
}
答案 0 :(得分:1)
您应该为您添加小图标通知。
builder.setSmallIcon(R.mipmap.ic_launcher);
答案 1 :(得分:0)
除了已编写的解决方案。通知未显示的另一个原因可能是因为您使用ConstraintLayout作为ViewGroup。