未显示具有自定义xml布局的Android通知

问题描述 投票:1回答:2

如果文本太长,我希望通知将其省略,因此我为其创建了自定义布局:

<?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;
  }
}
android android-notifications android-notification-bar
2个回答
1
投票

您应该为通知添加小图标。


0
投票

除了已编写的解决方案。通知未显示的另一个原因可能是因为您使用ConstraintLayout作为ViewGroup。

© www.soinside.com 2019 - 2024. All rights reserved.