[英]Android notification with custom xml layout not showing
如果文本太長,我希望我的通知將文本橢圓化,因此我為它創建了一個自定義布局:
<?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;
}
}
您應該為通知添加小圖標。
builder.setSmallIcon(R.mipmap.ic_launcher);
除了已經編寫的解決方案。 未顯示通知的另一個原因可能是您使用 ConstraintLayout 作為您的 ViewGroup。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.