【问题标题】:How to create a Custom Notification Layout in android?如何在android中创建自定义通知布局?
【发布时间】:2017-06-12 19:00:29
【问题描述】:

如何在android中使用通知样式在第一次显示通知中的全部内容或需要自定义布局??

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    在通知生成器上使用自定义 contentView

    要定义自定义通知布局,首先要实例化一个 扩充 XML 布局文件的 RemoteViews 对象。然后,而不是 调用 setContentTitle()、call setContent() 等方法。设置 自定义通知中的内容详细信息,使用中的方法 RemoteViews 设置视图子项的值:

    在单独的文件中为通知创建 XML 布局。你 可以使用任何你想要的文件名,但你必须使用扩展名 .xml 在您的应用中,使用RemoteViews 方法来定义通知的图标和文本。将此RemoteViews 对象放入您的 NotificationCompat.Builder 通过调用 setContent()。避免设置一个 您的 RemoteViews 对象上的背景可绘制,因为您的文本 颜色可能变得不可读。

    custom_push.xml 有我的自定义视图 R.id.image,R.id.text,R.id.title

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="64dp"
        android:padding="10dp" >
        <ImageView
            android:src="@mipmap/ic_launcher"
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="10dp" />
        <TextView
            android:textSize="13dp"
            android:textColor="#000"
            android:text="Testing"
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            />
        <TextView
            android:textSize="13dp"
            android:textColor="#000"
            android:text="Testing is awecome"
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:layout_below="@id/title"
             />
    </RelativeLayout>
    

    实例化一个 RemoteViews 对象并设置它,

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
    contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
    contentView.setTextViewText(R.id.title, "Custom notification");
    contentView.setTextViewText(R.id.text, "This is a custom layout");
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon)
    .setContent(contentView);
    
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(1, notification);
    

    检查:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle

    【讨论】:

    • @khaleel_jageer 是的,在给定的链接中有一个名为 Custom Notification Layouts 的提示,如果你没有得到它,请阅读
    • 只是想指出 fill_parent 已被弃用,从 API 8 开始应该使用 match_parent。见:developer.android.com/reference/android/view/…
    • notificationManager 在此代码示例中未定义
    • 自定义视图需要将样式设置为 DecoratedCustomViewStyle()
    【解决方案2】:

    我使用BitTextStyle() 在通知中添加突出显示的文本。

    return new NotificationCompat.Builder(context)
           .setSmallIcon(R.drawable.ic_mono)
           .setContentTitle(title)
           .setContentText(message)
           .setLargeIcon(icon)
           .setColor(ContextCompat.getColor(context, R.color.notification_color))
           .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
           .setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag"))
           .setShowWhen(true)
           .setAutoCancel(true);
    

    【讨论】:

      【解决方案3】:

      这段代码对我有用。

          private static RemoteViews contentView;
          private static Notification notification;
          private static NotificationManager notificationManager;
          private static final int NotificationID = 1005;
          private static NotificationCompat.Builder mBuilder;
      
          private void RunNotification() {
      
              notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");
      
              contentView = new RemoteViews(getPackageName(), R.layout.my_notification_layout);
              contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
              Intent switchIntent = new Intent(this, BackgroundService.switchButtonListener.class);
              PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 1020, switchIntent, 0);
              contentView.setOnClickPendingIntent(R.id.flashButton, pendingSwitchIntent);
      
              mBuilder.setSmallIcon(R.mipmap.newicon);
              mBuilder.setAutoCancel(false);
              mBuilder.setOngoing(true);
              mBuilder.setPriority(Notification.PRIORITY_HIGH);
              mBuilder.setOnlyAlertOnce(true);
              mBuilder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH;
              mBuilder.setContent(contentView);
      
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                  String channelId = "channel_id";
                  NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH);
                  channel.enableVibration(true);
                  channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                  notificationManager.createNotificationChannel(channel);
                  mBuilder.setChannelId(channelId);
              }
      
              notification = mBuilder.build();
              notificationManager.notify(NotificationID, notification);
          }
      

      这是我的通知布局

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:background="#e9ebe9">
      
          <ImageView
              android:id="@+id/flashButton"
              android:layout_width="180dp"
              android:layout_height="50dp"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:layout_marginRight="-20dp"
              android:src="@drawable/turnoff2" />
      
          <ImageView
              android:layout_width="100dp"
              android:layout_height="45dp"
              android:layout_alignParentLeft="true"
              android:layout_marginLeft="-10dp"
              android:layout_marginTop="5dp"
              android:layout_marginBottom="5dp"
              android:src="@mipmap/newicon" />
      
          <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerVertical="true"
              android:layout_marginLeft="80dp">
      
              <TextView
                  android:id="@+id/title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:text="Flashlight"
                  android:textColor="#000000"
                  android:textSize="13sp" />
      
              <TextView
                  android:id="@+id/charging"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/title"
                  android:layout_alignParentLeft="true"
                  android:layout_marginTop="3dp"
                  android:text="90% Charging"
                  android:textColor="#000000"
                  android:textSize="13sp" />
          </RelativeLayout>
      
      </RelativeLayout>
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        我猜你要找的是.setSubText()。 您指出的 Flipkart 通知绝对不是自定义视图。

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                .setSmallIcon(icon)
                                .setSubText("Limited Stocks, Don't Wait!") <-------
                                .setContentTitle("Custom Notification Title")
        notificationBuilder.notify(1, notificationBuilder.build());
        

        【讨论】:

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