0

我想要推送通知的自定义 UI。为此,我正在使用 RemoteViews。创建通知的代码:

 RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
    RemoteViews expandedNotificationView = new RemoteViews(context.getPackageName(), R.layout.expanded_custom_notification);
    notificationView.setTextViewText(R.id.notification_title, messagesToBeShown);
    expandedNotificationView.setTextViewText(R.id.expanded_notification_title, "Message Received in expanded ");
    expandedNotificationView.setTextViewText(R.id.expanded_notification_message, messagesToBeShown);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setContentIntent(pendingIntent);
    builder.setPriority(Notification.PRIORITY_MAX);
    Notification notification = builder.build();
    notification.contentView = notificationView;
   //        Notification notification = new             NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
   //                .setContentIntent(pendingIntent).setContent(notificationView)
   //                .setAutoCancel(true).build();
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        notification.bigContentView = expandedNotificationView;
    }
    notification.flags |= Notification.DEFAULT_LIGHTS |      Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(R.string.notification_number, notification);

用于自定义布局的 XML 文件:

expand_custom_notification.xml :

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
    android:id="@+id/expanded_notifiation_image"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<LinearLayout
    android:layout_width="80dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/expanded_notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />

    <TextView
        android:id="@+id/expanded_notification_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />
</LinearLayout>

custom_notification.xml :

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">

<ImageView
    android:id="@+id/notifiation_image"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/notification_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#fff" />

<Button
    android:id="@+id/mark_task_complete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:text="mark as complete" />

</LinearLayout>

这段代码的问题是:

  • 滑动时不展开
  • 行为未定义有时它显示 bigContentView,有时显示 ContentView

请帮我。提前致谢。

4

1 回答 1

0

尝试在builder.build()之前添加这些行

builder.setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
于 2016-06-21T21:08:01.913 回答