【问题标题】:bigContentView and ContentView with RemoteViews in push notifications UI not working推送通知 UI 中带有 RemoteViews 的 bigContentView 和 ContentView 不起作用
【发布时间】:2016-01-06 06:57:46
【问题描述】:

我想要推送通知的自定义 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 文件:

expanded_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

请帮助我。提前致谢。

【问题讨论】:

    标签: android push-notification android-notifications android-custom-view android-remoteview


    【解决方案1】:

    尝试在 builder.build()

    之前添加这些行
    builder.setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多